What happened?
I have some values but these are not right. Because i can’t do it with the aggregateWindow function because the value are only written in db when the value actually changes.
What did you expect to happen?
Some function to calculate the kwh over time for all values per day
2023-02-21T15:01:40.000Z | 3015
2023-02-21T15:01:50.000Z | 2945
these are two values and the time to the next value is not static.
I would appreciate help in calculating the values over a day so that each value is calculated with the previous value and time.
i need a method to sum up all values from one day and calculate every value with the time to the previous value to get KW/h maybe its better to convert the watts to kw and the minutes or seconds to hours and than sum all calculated values to KW/h per day. I hope I have explained it understandably
import "date"
from(bucket: "iobroker/global")
|> range(start: date.truncate(t: v.timeRangeStart, unit: 1d), stop: v.timeRangeStop) // date.truncate with unit: 1d is used in order to get whole starting day (e.g. 2023-02-15T00:00:00.000Z till now() ). If you use only v.timeRangeStart you will get time range e.g. 2023-02-15T20:04:15.683Z till now()
|> filter(fn: (r) => r["_measurement"] == "totalPV")
|> filter(fn: (r) => r["_field"] == "value")
|> elapsed(unit: 1s) // this function calculates time difference between subsequent data points and drops the first row (because there is no previous time to derive the elapsed time from)
|> map(fn: (r) => ({ r with _value: (float(v: r._value) / 1000.0) * (float(v: r.elapsed) / 3600.0) })) // formula
|> aggregateWindow(every: 1d, fn: sum, createEmpty: false)
|> yield(name: "result")
Notice: Instead of using v.timeRangeStart I used date.truncate(t: v.timeRangeStart, unit: 1d) because I think that seeing value changes for first day of time range would be confusing. This way only value for last day updates when new data point arrives (i.e at dashboard refresh).