Get last values from multiple devices in a single request
Often times, you might need to query the last value of one or more variables, across many devices. For example, when:
- Doing a script to compute an aggregation for multiple devices (i.e. "Average of temperatures in the fifth floor", or "Count of machines where vibration is between a range").
- Doing an ETL process to insert aggregated data into another system for further analysis.
- Building a devices table or map view in your own web app.
In such cases, the Variables endpoint in our API v2.0 is a great option, as it allows for the use of filters. These can used to query specific variables from devices filtered by:
- Individual device labels
- Device group
- Device type
- Devices where a property value matches a criteria
See examples below.
Using individual device labels
Get only 3 fields from the Variable object, then filter results where label=<variable-label>
, for the device labels given as a comma-separated list:
Method | URL |
---|---|
GET | https://industrial.ubidots.com/api/v2.0/variables/?fields=device,label,lastValue&label=<variable-label>&device__label__in=<device-label-1>,<device-label-2>,<device-label-3> |
Response
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6331c6aac590d801bbb446c4",
"id": "6331c6aac590d801bbb446c4",
"label": "07dbcccb4f3891d0df2d2cb0ae23fe37",
"name": "City of Westminster",
"createdAt": "2022-09-26T15:35:06.068177Z"
},
"lastValue": {
"value": 1013,
"timestamp": 1672962303000,
"context": {},
"created_at": 1672962305229
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6049251d0ff4c3276f76fcbc",
"id": "6049251d0ff4c3276f76fcbc",
"label": "aa05bbd986f208d399743dd8c14ae56c",
"name": "Austin",
"createdAt": "2021-03-10T19:59:25.461116Z"
},
"lastValue": {
"value": 1022,
"timestamp": 1672961758000,
"context": {},
"created_at": 1672961760088
}
}
]
}
Using device groups
Request
Method | URL |
---|---|
GET | https://industrial.ubidots.com/api/v2.0/variables/?fields=device,label,lastValue&label=pressure&device__deviceGroup__label=weather |
Response
{
"count": 7,
"next": null,
"previous": null,
"results": [
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6331c6aac590d801bbb446c4",
"id": "6331c6aac590d801bbb446c4",
"label": "07dbcccb4f3891d0df2d2cb0ae23fe37",
"name": "City of Westminster",
"createdAt": "2022-09-26T15:35:06.068177Z"
},
"lastValue": {
"value": 1013,
"timestamp": 1672962303000,
"context": {},
"created_at": 1672962305229
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/62b202839649f3000a49048e",
"id": "62b202839649f3000a49048e",
"label": "8283a15646edd513cd4383f087910c2b",
"name": "Menlo Park",
"createdAt": "2022-06-21T17:40:19.300973Z"
},
"lastValue": {
"value": 1014,
"timestamp": 1672962262000,
"context": {},
"created_at": 1672962264201
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6272d0cf144554000a5b7932",
"id": "6272d0cf144554000a5b7932",
"label": "ef67f39f63a952c33f55e0135dd3939c",
"name": "Barrancabermeja",
"createdAt": "2022-05-04T19:15:27.263911Z"
},
"lastValue": {
"value": 1005,
"timestamp": 1651691749000,
"context": {},
"created_at": 1651691751082
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/604925a50ff4c32b89fb1c54",
"id": "604925a50ff4c32b89fb1c54",
"label": "862ea70b9c60f5c4ff4eff901723094b",
"name": "Boston",
"createdAt": "2021-03-10T20:01:41.878541Z"
},
"lastValue": {
"value": 1016,
"timestamp": 1657236448000,
"context": {},
"created_at": 1657236449496
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6049254a0ff4c3286361a10e",
"id": "6049254a0ff4c3286361a10e",
"label": "a6bc8da0e285470b50671f0b450e06c4",
"name": "Fort Lauderdale",
"createdAt": "2021-03-10T20:00:10.409204Z"
},
"lastValue": {
"value": 1020,
"timestamp": 1672961758000,
"context": {},
"created_at": 1672961759776
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6049251d0ff4c3276f76fcbc",
"id": "6049251d0ff4c3276f76fcbc",
"label": "aa05bbd986f208d399743dd8c14ae56c",
"name": "Austin",
"createdAt": "2021-03-10T19:59:25.461116Z"
},
"lastValue": {
"value": 1022,
"timestamp": 1672961758000,
"context": {},
"created_at": 1672961760088
}
},
{
"label": "pressure",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/604924ef4763e7161935af7b",
"id": "604924ef4763e7161935af7b",
"label": "31b24cede34944fbc43cb8432f956734",
"name": "San Francisco",
"createdAt": "2021-03-10T19:58:39.280681Z"
},
"lastValue": {
"value": 1014,
"timestamp": 1672961758000,
"context": {},
"created_at": 1672961759604
}
}
]
}
Using device types
Request
Method | URL |
---|---|
GET | https://industrial.ubidots.com/api/v2.0/variables/?fields=device,label,lastValue&label=nh3&device__deviceType=air-pollution |
Response
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"label": "nh3",
"device": {
"url": "https://industrial.ubidots.com/api/v2.0/devices/6272d0b9390bf5000c80d74e",
"id": "6272d0b9390bf5000c80d74e",
"label": "weather-station-abc",
"name": "Station ABC",
"createdAt": "2022-05-04T19:15:05.154481Z"
},
"lastValue": {
"value": 1.31,
"timestamp": 1656522000000,
"context": {},
"created_at": 1656520678579
}
}
]
}