Create a "running integral" of values from another measurement

In InfluxDB 2, I have my battery charge / discharge power (in Watt) as follows:

As you can see, the battery supplied power from 20:00 yesterday to ~7:15 today, then a few short bursts during the morning, and was charged again (drain <0) between 13:00 and 15:00.

I would like to add the battery power (in Wh) to this graph. To do this I would have to calculate a “running integral” from the start of the graph to each point in time, summing up the area below the graph. I know that the drain or charge can be off by a constant (as all integrals can) but I can live with that.

I have found the integral() function but this always returns only a single value.

How do I create a “running integral” which returns just as many datapoints as the input function?

Thank you!
Jens

Came to ask the same question. The only solution I can think of is to map a field with a period integral (per record), and then run a cumulativeSum() on that field.

This is my sloppy solution:

data = from(bucket: "Iotabucket2")
  |> range(start: -2h, stop: -115m)
  |> filter(fn: (r) => r["ct"] == "AC3")
wattS = data
  |> aggregateWindow(fn: mean, every: 1m)
  |> yield(name:"Watts")
kWh = data
  |> aggregateWindow(fn: integral, every: 1m)
  |> cumulativeSum()
  |> map(fn: (r) => ({r with _value: r._value / 60000.0, _field: "kWh", units: "kWh"}))
  |> yield(name: "kWh")
1 Like