2

An influx 2 database stores incrementing values from an mechanical counter (gas counter). The goal is to build a query to get the consumption over a certain interval, e.g. one day. With SQL I would group the data in the desired interval and than calculate max() - min() +1 for that interval. What is the preferred way to do that with the flux query language?

from(bucket: "Energy")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "gas")
|> filter(fn: (r) => r["kind"] == "count")
|> aggregateWindow(every: 24h, fn: difference, createEmpty: false)
|> yield(name: "interval")

does not work, error @5:6-5:69: missing required argument column (argument fn)

Matt Summersgill
  • 4,054
  • 18
  • 47
AndiR
  • 179
  • 10
  • possible duplicate of https://stackoverflow.com/questions/69214037/influxdb-flux-query-with-custom-window-aggregate-function – AndiR Feb 07 '22 at 17:31

2 Answers2

3

The solution is to examine difference() before aggregateWindow and as aggregate function to use sum.

from(bucket: "Energy")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "gas")
  |> filter(fn: (r) => r["_field"] == "count")
  |> difference()
  |> aggregateWindow(every: 1h, fn: sum, createEmpty: false)
  |> yield(name: "consumption")
AndiR
  • 179
  • 10
0

I have just done a similar thing with an electricity counter where I wanted each window to be the difference between the last and first value of the window. Rather than taking the sum of the difference between every point in the window I used the difference between the last value of each window.

from(bucket: "Energy")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "gas")
  |> filter(fn: (r) => r["kind"] == "count")
  |> aggregateWindow(every: 24h, fn: last)
  |> difference()
  |> yield(name: "consumption")
Adam
  • 96
  • 6