How can I get Today, yesterday and last month (last day) data from device?
You mean using analysis or the Restful API?
Basically, in both methods, you need to use the end_date filter for your query.
End date accepts both an ISO-8601 format or a relative date string.
Restful API (documentation):
curl --location --request GET 'https://api.tago.io/data?variable=temperature&end_date=1%20day' \
--header 'device-token: b4766ce8-7d31-cdbb-548d-c0bcb71fbf8f'
The query above is using relative date of 1 day. It will get only data for yesterday in this specific case. You can add qty=1 querystring to receive only 1 data.
Analysis (documentation):
const device = new Device({ token: "your-device-token" });
const result = await device.getData({ variables: ["temperature"], end_date: "1 day" });
Same as the Restful API example, but to be used inside an Analysis.
Notice that you must import the Device from the sdk adding the line const { Device } = require("@tago-io/sdk") to your script.
Hi,
How can I query data for any specific date like :
const data = await device.getData({
variables: [your_variable],
start_date: “2022-06-13”
});
or
const data = await device.getData({
variables: [your_variable],
start_date: “2022-05-31”
});
Isn’t seem to be working, Sometimes I get data of future date also. Lets say I want to fetch data of 06/15/2022 but in result I also get data of 06/15/2022. Please help.
Hi Milind,
You want to use end_date instead of start_date.
start_date will set the query to get data as from the date.
end_date will set the query to get data until the date
Hi there guys,
Is there any way to get data from API or SDK without using “Device-Token”? Is there any way to control the permission of each device token? My concerning is that I Cannot control the privileges from this token, and then a third party user which has the knowledge of this device token can delete data using Tago API.
Hi Ricardo,
Device token is required to authenticate in the TagoIO API. But you have some ways to resolve the device token; the most common is using the SDK Utils function getDevice.
Code example:
async function example(context, scope) {
const environment = Utils.envToJson(context.environment);
if (!environment) {
throw new Error("Missing environment variables");
}
const account = new Account({ token: environment.account_token });
// Getting up to 10,000 devices to resolve the token
const devices = await account.devices.list({
page: 1,
fields: ["id", "name", "bucket", "tags"],
filter: {
tags: [{ key: "device_type", value: "meter" }],
},
amount: 10000,
});
for (const dev of devices) {
const device = await Utils.getDevice(account, dev.id);
const data = await device.getData(/* ... */);
// do something with data
}
}
NOTE: You need a profile token to give permission for the analysis to access your devices.