Influxdb 2.0 Flux - How to return 0 instead null

Viewed 875

I want to count amount of values that are greater than specific value. Data:

enter image description here

from(bucket: "bucket name")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r._value > 35)
    |> count()

If there are no values in the processing data range that are greater the specified value than the influx returns nothing (no data).

1 Answers

Solution with a little trick... Instead of filter() and count() - need to use map() and sum()

from(bucket: "bucket name")
     |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
     |> map(fn: (r) => ({ r with _value: if r._value 35 then 1 else 0 }))
     |> sum()

Related