InfluxDB do not take time, a value is active into account

Please excuse me if my request has already been answered or is ridiculously simple. InfluxDB is quite new to me, and maybe I’ve just been looking in the wrong place to describe my problem.

I have an issue where values are not being calculated correctly. I have a time period from 01:30 to 02:00. For the first 205 seconds, the value is 25, then for 1390 seconds, it’s -405, and for the last 205 seconds, it’s 25 again. I calculated that the average is -307.05, as approximately 77% of the time the value is “-405,” and only about 23% of the time, the value is “25.” ((25205)+(1390-405)+(25*205))/1800 = ~307.05

During this period, only 458 values were recorded. The value 25 was recorded every 2-3 seconds, while -405 was recorded only every 5-6 seconds. Since the value 25 was written much more frequently than the value -405, InfluxDB includes these values more heavily in its calculation. InfluxDB seems to simply calculate an average over all values, even though the value -405 was active for much longer, just not rewritten.

Is there no way for InfluxDB to take into account the time period during which a value was active, instead of simply calculating an average of all values?"

I hope this helps! If you have any further questions or need clarification, feel free to ask.

Additional Info:
I used the Query Builder which created:
from(bucket: “smart_metering”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “BAT”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> aggregateWindow(every: 30m, fn: mean, createEmpty: false)
|> yield(name: “mean”)

My InfluxDB Server is running on Docker Server. Image is downloaded from Docker Hub, Version 2.7.0

How about ingesting another aggregateWindow before the one you have with a smaller interval and another function?

from(bucket: “smart_metering”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “BAT”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> aggregateWindow(every: 10s, fn: last, createEmpty: false)
|> aggregateWindow(every: 30m, fn: mean, createEmpty: false)
|> yield(name: “mean”)

Thanks Eriktar,that helps a lot.

I also addes the “fill(usePrevious: true)” and createEmpty: true in the first aggregateWindow() function to create missing values, if the period between 2 values is even longer then 10s.

That work for me an now shows correct values.

from(bucket: “smart_metering”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “BAT”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> aggregateWindow(every: 10s, fn: mean, createEmpty: true)
|> fill(usePrevious: true)
|> aggregateWindow(every: 30m, fn: mean, createEmpty: false)
|> yield(name: “mean”)

1 Like