Help with a query, sum or count values

Hi
I need some help creating a query in fluxdb2

I can get 2 values back at “_value” and that is TRUE or FALSE.
What I would like is a query that only sums the value TRUE (sum) (per day).

so that I have a sum of all times the value is set to TRUE in a day.

raw data:

When I execute this query, it counts the state values, but I want to exclude the FALSE ones.

from(bucket: “home_assistant”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “binary_sensor.deurbel_ringing_sensor”)
|> filter(fn: (r) => r[“_field”] == “state”)
|> count()
|> yield(name: “count”)

anyone can help? thanks in advance!!

Just filter by r._value == "True":

from(bucket: "home_assistant")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "binary_sensor.deurbel_ringing_sensor")
    |> filter(fn: (r) => r["_field"] == "state" and r["_value"] == "True")
    |> count()
    |> yield(name: "count")

@anon13751922 Ok, you want to use aggregateWindow() to window your data into specified time intervals and return the count for each window:

from(bucket: "home_assistant")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "binary_sensor.deurbel_ringing_sensor")
    |> filter(fn: (r) => r["_field"] == "state" and r["_value"] == "True")
    |> aggregateWindow(every: v.windowPeriod, fn: count)
    |> yield(name: "count")

Aggregate functions like count() actually drop the _time column because the functions doesn’t know what timestamp to use for the aggregate value. But using aggregateWindow() will solve that for you.

1 Like

great! thank you! looks like its working :slight_smile:

1 Like