fafa.fsb
(fafa.fsb)
June 30, 2024, 9:33am
1
Is there an analysis example that calculates the difference between a variable over the last day or another time period?
Beispiel: Gewicht
07.00 Uhr 10 kg
08.00 Uhr 11 Kg
09.00 Uhr 12 kg
etc…
Gibt es eine Möglichkeit, die Differenz zu dem zu berechnen, was ich vor 24 Stunden hatte?
Ich freue mich über die Hilfe von jedem!
Hi fafa.fsb,
Regarding your inquiry about calculating the difference between a variable over the last 24 hours, we would like to understand more about the specific type of calculation you are looking to perform. Could you please provide additional context or details on the calculation method you have in mind?
Your example of tracking weight at different times is helpful, but we need more information to provide a precise solution. Are you looking to calculate the difference between the current value and the value exactly 24 hours ago, or are you interested in a different type of comparison?
Your additional input will help us assist you more effectively.
fafa.fsb
(fafa.fsb)
July 1, 2024, 8:10pm
3
I would like to calculate the difference between the current value and the value from 24 hours ago
Here is an example code:
const binData = await Resources.devices.getDeviceData(“your-device-id”, { variables: “your-variable”, start_date: `1 day`, qty: 9999 });
if (binData.length < 2) {
console.log(“Not enough data to calculate the difference.”);
return;
}
const firstData = binData.at(0)?.value;
const lastData = binData.at(-1)?.value;
const difference = firstData - lastData;
console.info(difference);
fafa.fsb
(fafa.fsb)
July 4, 2024, 3:56pm
5
What am I doing wrong that the code doesn’t work?
You need to insert your code into a function, use the Hello Word snippet as an example.
Don’t forget to give your analysis permission to execute the getDeviceData query in Access Management .
fafa.fsb
(fafa.fsb)
July 4, 2024, 9:09pm
7
Thank you very much for your help but I can’t get the code to work sory I have absolutely no experience
I always get the same error as in the photo above no matter what I do
Use this new example, it should work.
Please refer to this documentation to assist you in locating the device token: Device Token Guide
const { Analysis, Device } = require(“@tago-io /sdk”);
async function myAnalysis() {
const devices = new Device({ token: “your-device-token” });
const variables = await devices.getData({ variables: “your-variable-name”, start_date: `1 day`, qty: 9999 });
if (variables.length < 2) {
console.info(“Not enough data to calculate the difference.”);
return;
}
const firstData = variables.at(0)?.value;
const lastData = variables.at(-1)?.value;
const difference = firstData - lastData;
console.info(difference);
}
Analysis.use(myAnalysis);
fafa.fsb
(fafa.fsb)
July 5, 2024, 5:48am
9
Thank you very much, it works
Is it possible to display the received value in the dashboard
and thank you very much, you helped me a lot
Yes, you can, but first you have to take the result and save it in a new variable, here’s the documentation for guidance: https://js.sdk.tago.io/classes/_internal_.Devices.html#sendDeviceData
Once you have saved you can display the value of the variable on the dashboard.