sqlinfluxdbinfluxql

Obtain duration when certain variable value InfluxQL


I am using InfluxQL language to extract data from InfluxDB. I want to get the duration when I have a variable with True value.

I have this on my database:

time running

2022-09-16 09:00:00 False

2022-09-16 09:05:00 False

2022-09-16 09:10:00 True

2022-09-16 09:40:00 True

2022-09-16 10:00:00 False

2022-09-16 12:00:00 True

2022-09-16 12:10:00 False

I want to get this result:

Duration True: 1h

1h = 50 min (09:10-10:00) + 10 min (12:00-12:10)

Can someone give me an advice with an InfluxQL query?

Thank you!


Solution

  • It is feasible to report the total duration for a given state in InfluxDB but only in Flux not InfluxQL (easily). You could try following steps:

    1. Enable Flux in v1.8 with the configuration change here

    2. Sample Flux could be:

      from(bucket: "yourDatabaseName/autogen") |> range(start: 2022-09-16T00:00:00Z, stop: 2022-09-16T23:59:59Z) |> filter(fn: (r) => r._measurement == "yourMeasurementName") |> stateDuration(fn: (r) => r._value == true, column: "state", unit: 1m)

    Again it's still not possible to do it yet in InfluxQL though the community has been waiting for this for a while. See more details here.