Displaying cumulative energy data

Hey!

I’m trying to visualize my energy data using InfluxDB 2.0 (and its built-in UI, which I believe uses Chronograf), however I can’t make it work. Example data:

2020-01-01: 100 kWh
2020-01-02: 120 kWh
2020-01-03: 125 kWh
2020-01-04: 134 kWh
2020-01-05: 156 kWh

So I would like a graph that just displays today’s usage minus the previous day’s usage, so I get:

2020-01-01: 100 kWh [100-0] (no previous measurement; default to zero)
2020-01-02: 20 kWh  [120-100]
2020-01-03: 5 kWh   [125-200]
2020-01-04: 9 kWh   [134-125]
2020-01-05: 22 kWh  [156-134]

I’ve been reading a lot about integral functions but I just can’t get anything to work. I just started using InfluxDB yesterday using the Flux language, but everything I find seems to be written in some kind of SQL.

Any suggestion would be great! Thanks.

Hey Louis,

Out of curiosity, where are you “looking”? Just random google searches or ?? Obviously, we want to make your search experience better so that you can find the answer to this. So a few hints for us here would be helpful. Thanks!

Have a peek at the official documentation. In particular,

and

I’m not sure you need integral in this case, perhaps a simple difference between points will suffice?

Also, if you aren’t sure about shaping your data, I’d strongly suggest reviewing line protocol and the elements which determine a particular series: Handle duplicate data points when writing to InfluxDB | InfluxDB OSS 2.0 Documentation

and this is pretty helpful as well:

In terms of the specifics, I used the array.from function to dynamically build the sample data you supplied – since I don’t have access to what you’ve written in. I added deviceID as a tag to uniquely identify the series. I’m assuming you have more than one device suppling data? Then, I just used the difference function to yield the result.

import "experimental/array"

values = [
  { _time: 2020-01-01T00:00:00Z, _field: "kWh", _value: 100, deviceId: "12345"},
  { _time: 2020-01-02T00:00:00Z, _field: "kWh", _value: 120, deviceId: "12345"},
  { _time: 2020-01-03T00:00:00Z, _field: "kWh", _value: 125, deviceId: "12345"},
  { _time: 2020-01-04T00:00:00Z, _field: "kWh", _value: 134, deviceId: "12345"},
  { _time: 2020-01-05T00:00:00Z, _field: "kWh", _value: 156, deviceId: "12345"}
]

 array.from(rows: values)
  |> difference(nonNegative: false, keepFirst: true, columns: ["_value"])

Result:

table _time _field _value deviceId
0 2020-01-01T00:00:00Z kWh 12345
0 2020-01-02T00:00:00Z kWh 20 12345
0 2020-01-03T00:00:00Z kWh 5 12345
0 2020-01-04T00:00:00Z kWh 9 12345
0 2020-01-05T00:00:00Z kWh 22 12345

You’ll notice that the first value is null because there is no difference available. However, you can eliminate this by setting keepFirst: false (or just removing keepFirst as false is the default.

Does that help?

1 Like