Hi all,

I struggling to get my logic in order here on how to do a calculation hope someone could provide some guidance.

**Data:**

I have a electricity consumption sensor that is logging from a MQTT topic into influx. It logs on change of value so frequently example data set:

```
time value
2021-12-20T16:28:41.416861389Z 543.4
2021-12-20T16:28:42.500538983Z 561.73
2021-12-20T16:29:03.368917573Z 588.96
2021-12-20T16:29:12.506015646Z 570.27
2021-12-20T16:29:19.342193073Z 560.56
2021-12-20T16:29:42.511684809Z 568.24
2021-12-20T16:30:12.518064796Z 548.66
2021-12-20T16:30:38.255127611Z 620.5
2021-12-20T16:30:41.254842087Z 584.67
2021-12-20T16:30:42.25937102Z 553.28
2021-12-20T16:30:42.545895232Z 553.28
2021-12-20T16:30:43.257105821Z 584.77
2021-12-20T16:30:50.256368831Z 551.96
2021-12-20T16:30:52.254963946Z 607.65
2021-12-20T16:30:54.254355539Z 560.69
2021-12-20T16:31:12.530639541Z 555.44
2021-12-20T16:31:42.538221108Z 573.09
2021-12-20T16:32:12.542957505Z 547.66
2021-12-20T16:32:42.546313695Z 545.18
2021-12-20T16:33:04.047610857Z 593.03
2021-12-20T16:33:06.049397424Z 546.23
2021-12-20T16:33:12.551816826Z 550.16
```

The value is provided in Watts.

**The Question I am struggling to get my mind around:**

I am visualizing this data in grafana but looking at doing the calculation at the data query layer. What I am trying to get to is :

- I want to get the total spend to date on this particular sensor. I thought let me do a sum of all values, gives me the total Wattage but that wont work because its not consuming that amount every time it logs, its logging the change between the last value and the current value.

So how would I break this down, 1 KW (kilowatt) is 1000 (watts) so I can calculate a kilowatt and the price of the KW (KW * Price Of a KW unit) but how would I calculate the amount spent todate based on this usage information stored?

At the end of the day I am trying to calculate since the sensor has been logging what have I paid in electricity utilization

I would appreciate any guidance - been racking my brain on this one but cant seem to nail down one path to go down?