Analysis to calculate the variable difference between 24 hours

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.

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);

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.

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);

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.