Calculate operating hours

Hello all,

I would like to calculate in flux, operating hours.
I have current values, if they go above a value, the time should be calculated in hours.

How do I do this?

from(bucket: "VH_Messwerte")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["topic"] == "Energie/VT100m/Strom/L1" or r["topic"] == "Energie/VT100m/Strom/L2" or r["topic"] == "Energie/VT100m/Strom/L3")
  |> filter(fn: (r) => r._value  > 4000)
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)

  |> elapsed(unit: 1s)
    |>group()

Hello @matze1708,
I’d use a map() function to assign a level based on a threshold.
Then I’d use stateDuration() to calculate the duration of the “above a value” time.

Thanks for the answer,

is this the right approach?

Maybe you have an example for me?

from(bucket: "VH_Messwerte")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["topic"] == "Energie/VT100m/Wirkleistung/Gesamt" 
        or r["topic"] == "Energie/VT100m/Wirkleistung/L1" 
        or r["topic"] == "Energie/VT100m/Wirkleistung/L2" 
        or r["topic"] == "Energie/VT100m/Wirkleistung/L3")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> yield(name: "last")

  |> map(
        fn: (r) => ({r with
            level: if r._value >= 4000.0 then
            stateDuration(fn: (r) => r._value < 15),
        }),
    )

or an different solution:

from(bucket: "VH_Messwerte")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["topic"] == "Energie/VT100m/Wirkleistung/Gesamt" 
        or r["topic"] == "Energie/VT100m/Wirkleistung/L1" 
        or r["topic"] == "Energie/VT100m/Wirkleistung/L2" 
        or r["topic"] == "Energie/VT100m/Wirkleistung/L3")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)

 |> stateDuration(fn: (r) => r._value > 2000, column: "Betriebsstunden_Strahler", unit: 1m)