# Analysis to calculate the variable difference between 24 hours

**URL:** https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627
**Category:** Analysis and Actions
**Created:** [June 30, 2024, 9:33am UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627 "2024-06-30T09:33:32Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![fafa.fsb](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@fafa.fsb](https://community.tago.io/u/fafa.fsb)
#### Post date: [June 30, 2024, 9:33am UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/1 "2024-06-30T09:33:32Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [July 1, 2024, 7:14pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/2 "2024-07-01T19:14:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fafa.fsb](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@fafa.fsb](https://community.tago.io/u/fafa.fsb)
#### Post date: [July 1, 2024, 8:10pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/3 "2024-07-01T20:10:17Z")

</div>

I would like to calculate the difference between the current value and the value from 24 hours ago

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [July 2, 2024, 8:39pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/4 "2024-07-02T20:39:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fafa.fsb](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@fafa.fsb](https://community.tago.io/u/fafa.fsb)
#### Post date: [July 4, 2024, 3:56pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/5 "2024-07-04T15:56:45Z")

</div>

What am I doing wrong that the code doesn’t work?

 ![](https://canada1.discourse-cdn.com/flex034/uploads/tagoio1/original/2X/a/add674716cb0b9942d7212a0e66abbc3ddf99414.png)

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [July 4, 2024, 7:56pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/6 "2024-07-04T19:56:51Z")

</div>

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](https://help.tago.io/portal/en/kb/articles/183-access-management).

---

<div class="post-metadata">

### Author: ![fafa.fsb](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@fafa.fsb](https://community.tago.io/u/fafa.fsb)
#### Post date: [July 4, 2024, 9:09pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/7 "2024-07-04T21:09:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [July 4, 2024, 9:26pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/8 "2024-07-04T21:26:33Z")

</div>

Use this new example, it should work.

Please refer to this documentation to assist you in locating the device token: [Device Token Guide](https://help.tago.io/portal/en/kb/articles/4-device-token)

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

---

<div class="post-metadata">

### Author: ![fafa.fsb](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@fafa.fsb](https://community.tago.io/u/fafa.fsb)
#### Post date: [July 5, 2024, 5:48am UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/9 "2024-07-05T05:48:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [July 9, 2024, 2:26pm UTC](https://community.tago.io/t/analysis-to-calculate-the-variable-difference-between-24-hours/1627/10 "2024-07-09T14:26:56Z")

</div>

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](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.
