InfluxDB FluxQL query to show daily average over longer period

Viewed 17

I'm trying to get a Flux query that shows me what an average day looks like, over a given period. More specifically, I'm querying power usage data of my freezer, gathered and sent to InfluxDB by home assistant. As a very basic example, if my freezer would have used 100W during the first 15 days of a 30 day period, and 0W during the last 15 days, the daily average of that 30 day period would be 50W. What I want to see is a graph of a 24h time period, but with the average data of month.

I have this basic query which shows me a months worth of data:

from(bucket: "home_assistant/autogen")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["friendly_name"] == "Shelly plug S Freezer Power 0")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
  |> yield(name: "mean")

By playing with the aggregateWindow function, changing the every parameter to 1d will show me the average data per day over those 30 days, but that's not what I want.

1 Answers

If you want to have 30 days average you can take a look at the timeMovingAverage. It will return an average of the current value and all row values in the previous period (duration). It returns moving averages at a frequency defined by every parameter.

Related