Historical Data From Xively

Is there any code examples for capturing the historical data from an xively datastream? I can get as far as jsondecode and finding the “datastream” but after that comes an array with the “id” (which i need) and then later is another array for “datapoints”. It gets deeper and deeper… :slight_smile:

It depends what you’re looking for.

If you’re trying to get every single data point you’ve published, you should realize that you can only request interval of 6 hours at a time.

Take a look at the historical data API documentation (if you haven’t already).

Here’s a really quick example (note: I wrote this in the forum editor… there are bound to be some errors) of grabbing 6 hours worth of interval data, and cycling through it:

`function getStreamData(feedID, streamName) {
// add code to set start/end properly…
// im coding this in my forums editor, so not going to do that…
local resp = http.get(“https://api.xively.com/v2/feeds/"+feedID+"?end=2014-07-18T21:23:00Z&duration=6hours&interval=0”).sendsync();

if (resp.statuscode != 200) {
    server.log("error");
    return null;
}

local data = http.jsondecode(resp.body);
local dataStreams = data.datastreams;

// grab the stream
local stream = null;
foreach(s in dataStreams) {
    if (s.id == streamName) {
        stream = s;
    }
}
if (stream == null) {
    server.log("Stream not found");
    return null;
}

// do something with the data points:
if (!("datapoints" in stream)) {
    server.log("No datapoints");
    return null;
}

return stream.datapoints;

};

data <- getStreamData(feedID, streamName);
foreach(datapoint in data) {
server.log(datapoint.at + " - " + data.point.value);
}`

Got it working. Thanks!

near the end of your example:

data.point.value should be datapoint.value

Working on this over the weekend, I really want 4 of the data streams from the same xively account. I am just trying for 2 right now. I made Stream1, Stream2, etc… global so the routine doesn’t return the stream. Is that ok? I am not getting what I think should be the last 6 hrs of data. There are a lot of 0.0 in the data I get back but there aren’t any on my xively page. Not sure it this is getting the data I want. I am logging temperatures every 5 minutes to xively.
`function getStreamData(feedID){

stream1 <- null;
stream2 <- null;
stream3 <- null;
stream4 <- null;

T1SafeName <- “T1”;
T2SafeName <- “T2”;
T3SafeName <- “T3”;
T4SafeName <- “T4”;

local trigger_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".json?duration=6hours&interval=0";
local req = http.get(trigger_url, {"X-ApiKey":API_KEY, "User-Agent":"xively-Imp-Lib/1.0"});     //add headers
local res = req.sendsync();         //send request
//server.log(res.body);
local data = http.jsondecode(res.body);
local dataStreams = data.datastreams;


foreach(s in dataStreams) {
    if (s.id == T1SafeName) {
        server.log("xiv " + s.id);
        stream1 = s;
    }
    if (s.id == T3SafeName) {
        server.log("xiv " + s.id);
        stream3 = s;
    }
}

return;

};`

From studying the json, it is only sending back the last 12 readings, x 5 = 1hr. I don’t have the end date in there.

{"id":"T1","current_value":"36.9","at":"2014-07-21T19:41:25.151304Z","max_value":"78.5","min_value":"0.0","tags":["Not Used"],"datapoints":[{"value":"42.6","at":"2014-07-21T13:45:50.939898Z"} ,{"value":"35.8","at":"2014-07-21T13:50:50.940381Z"} ,{"value":"39.4","at":"2014-07-21T13:55:50.940347Z"} ,{"value":"52.6","at":"2014-07-21T14:00:50.942403Z"} ,{"value":"43.9","at":"2014-07-21T14:05:50.940916Z"} ,{"value":"39.4","at":"2014-07-21T14:10:50.940244Z"} ,{"value":"35.7","at":"2014-07-21T14:15:50.941378Z"} ,{"value":"35.4","at":"2014-07-21T14:20:50.946456Z"} ,{"value":"37.4","at":"2014-07-21T14:25:50.941339Z"} ,{"value":"40.2","at":"2014-07-21T14:30:50.944428Z"} ,{"value":"41.8","at":"2014-07-21T14:35:50.944053Z"} ,{"value":"43.1","at":"2014-07-21T14:40:50.944249Z"} ,{"value":"47.5","at":"2014-07-21T14:45:50.944466Z"} ]} ,{"id":"T2","current_value":"0.0","at":"2014-07-21T19:41:25.151304Z","max_value":"106.4","min_value":"-26.3","tags":["Temperature Outside Garage Window"],"datapoints":[{"value":"0.0","at":"2014-07-21T13:45:50.939898Z"} ,{"value":"0.0","at":"2014-07-21T13:50:50.940381Z"} ,{"value":"0.0","at":"2014-07-21T13:55:50.940347Z"} ,{"value":"0.0","at":"2014-07-21T14:00:50.942403Z"} ,{"value":"0.0","at":"2014-07-21T14:05:50.940916Z"} ,{"value":"0.0","at":"2014-07-21T14:10:50.940244Z"} ,{"value":"0.0","at":"2014-07-21T14:15:50.941378Z"} ,{"value":"0.0","at":"2014-07-21T14:20:50.946456Z"} ,{"value":"0.0","at":"2014-07-21T14:25:50.941339Z"} ,{"value":"0.0","at":"2014-07-21T14:30:50.944428Z"} ,{"value":"0.0","at":"2014-07-21T14:35:50.944053Z"} ,{"value":"0.0","at":"2014-07-21T14:40:50.944249Z"} ]} ,{"id":"T3","current_value":"-0.6","at":"2014-07-21T19:41:25.151304Z","max_value":"76.2","min_value":"-16.3","unit":{"symbol":"°","label":"F"} ,"datapoints":[{"value":"-10.8","at":"2014-07-21T13:45:50.939898Z"} ,{"value":"-11.3","at":"2014-07-21T13:50:50.940381Z"} ,{"value":"-9.5","at":"2014-07-21T13:55:50.940347Z"} ,{"value":"-13.8","at":"2014-07-21T14:00:50.942403Z"} ,{"value":"-14.8","at":"2014-07-21T14:05:50.940916Z"} ,{"value":"-13.6","at":"2014-07-21T14:10:50.940244Z"} ,{"value":"-10.6","at":"2014-07-21T14:15:50.941378Z"} ,{"value":"-10.8","at":"2014-07-21T14:20:50.946456Z"} ,{"value":"-13.3","at":"2014-07-21T14:25:50.941339Z"} ,{"value":"-13.1","at":"2014-07-21T14:30:50.944428Z"} ,{"value":"-12.7","at":"2014-07-21T14:35:50.944053Z"} ,{"value":"-12.4","at":"2014-07-21T14:40:50.944249Z"} ,{"value":"-11.3","at":"2014-07-21T14:45:50.944466Z"} ]}

I’ll keep udating my progress. I had to add this to the xively query to get more than 100 readings…

local trigger_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".json?duration=6hours&interval=0&limit=600