Split cumulative data measurements in hourly / daily chunks


sorry if this has been answered before, I tried to search, but I’m a newbie with InfluxDB2 and Flux.

I receive cumulative kwH readings from my house energy meter which gets the total power consumption in kWh every 2 minutes.In a simple historical graph this looks like this:

My goal is to split the readings into timed chunks, e.g. to have a bar chart which shows how much kWh were consumed over the last day for every hour (1 bar per hour). So basically I need kWH_total reading at the end of hour - kWH_Total reading at the beginning of the hour.

From reading the forums here I understood the spread function does what I am looking for. I created the following flux script which does the hourly splits, but does not provide me the mentioned difference but probably the count of measures in this timeframe. Any idea what I’m doing wrong?

edit: ok, I just learned there are no Bar Charts in InfluxDB2 and I tried to misuse a Histogram which is probably not possible. Will try in Grafana

This is the flux script I created so far:

from(bucket: “OpenHab”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “kWH_Total” )
|> aggregateWindow(every: v.windowPeriod, fn: spread, createEmpty: false)
|> keep(columns: [“_time”, “_measurement”])
|> sort(columns: [“_time”])

Hello @M_W and welcome to the InfluxDB forum.

Using Bar Charts in Grafana with the time selector set to Last 7 Days, try a query like this (obviously I cannot test this, but hopefully it’ll get you close):

from(bucket: "OpenHab")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "kWH_Total")
  |> aggregateWindow(every: 1d, fn: max)
  |> difference()
  |> yield(name: "Total Usage Per Day")

and to get per hour, simply change this |> aggregateWindow(every: 1h, fn: max)

Thankyou for showing this, I have also set the “v.timeRangeStart” to -7d and then I can change the time selector for the page and this panel stays at 7 days.