Water counter "difference" 1 when no consumption

Hi all!
I have a water counter that sends the total amount of liters (since it was installed) to InfluxDB, so I can see an always rising graph.
I wanted to see the water consumption per minute and…ChatGPT gave me this code

from(bucket: "HomeAutomation")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Watering")
  |> filter(fn: (r) => r["_field"] == "water_counter")
  |> difference(columns: ["_value"])
  |> aggregateWindow(every: 1m, fn: sum, createEmpty: false)
  |> fill(column: "_value", value: 0.0)

that seems about right, except that when there is no consumption the line stays at 1 instead of 0.
Why?
Thanks!

ChatGPT’s output is flawed. You would use fill() in conjunction with createEmpty: true (the default).

I don’t know what’s wrong in your case but you could also write it like this:

from(bucket: "HomeAutomation")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Watering")
  |> filter(fn: (r) => r["_field"] == "water_counter")
  |> aggregateWindow(every: 1m, fn: last)
  |> fill(usePrevious: true) // optional, depends on what you want
  |> difference(columns: ["_value"])
1 Like

Thanks a lot!!!
That really helps!