How to retrieve sensor data using the API
A very common use case when using our API is to get the values of specific sensors. Often we need the current value of a sensor. Other times we need the historical values over a period of time.
To retrieve data for a given sensor, we first need to find the id of this sensor. One way to do this is to go to the sensor in the ZENSIE dashboard (Sensors > click on the sensor you are interested in) and look at its URL in the browser:
Another option would be to get all the sensors from an organization using an API call (see this page for an example).
For doing any API call, it’s necessary to generate and use an API key. This can be generated once and used always after that (Go to Account Settings > Developer).
Here are a couple of examples to request data from a temperature/humidity sensor with the unique id “39555cf5-3081-400c-96cf-ff8d9cdea173”. We are using the following API endpoint:
We need to make a GET request, passing the sensor id, and date/time interval. For example, in this case we request the average values per day from the 1st to the 4rd of November of 2017:
curl -XGET \ -H "Content-type: application/json" \ -H 'Authorization: <API key>' \ "https://api.30mhz.com/api/stats/check/39555cf5-3081-400c-96cf-ff8d9cdea173/from/2017-11-01T00:00:00Z/until/2017-11-04T00:00:00Z?statisticType=averages&intervalSize=1d"
This could return for example:
dsf
{ "1509494400000": { "sht25.temp": 18.601995752298404, "sht25.humidity": 59.27995778537517 }, "1509580800000": { "sht25.temp": 18.30961109134886, "sht25.humidity": 58.49782640139262 }, "1509667200000": { "sht25.temp": 17.93818057510588, "sht25.humidity": 58.11836816999647 } }
This shows the average temperature and humidity for each day, the key in each case being the timestamp.
We could also request the minimum or maximum values for a period by using statisticType=mins or statisticTypes=maxs respectively.
We could change the intervalSize and request it per hour, with intervalSize=1h, or per minute (1m), 5 minutes (5m), 5 days (5d), monthly (M), etc.
Similarly, if we would want the current value for a sensor, we could use the following API endpoint:
And we could call it for the sensor of the example in this way:
curl -XGET \ -H "Content-type: application/json" \ -H 'Authorization: <API key>' "https://api.30mhz.com/api/stats/check/56aac51f-5966-47da-8824-eaf36c8e4737"
We would get something similar to this:
{ "checkId": "56aac51f-5966-47da-8824-eaf36c8e4737", "timestamp": "2017-11-21T13:10:45Z", "lastRecordedStats": { "sht25.temp": "16.79", "sht25.humidity": "62.93" }, "json": null, "code": null, "message": null, "matchCondition": null }
where the current values are returned in lastRecordedStats property.
All the API endpoints to get data for a sensor or a set of sensors can be found in the Swagger documentation.