Calculate duration when measurements are non-zero

Hi,

I have done some searching but haven’t found a suitable solution yet.

I would like to count the time where a defined measurement returns non-zero values in Flux.

My usecase is: My desktop PC is connected to a smart plug that constantly measures the consumed energy. When the desktop PC is off, it measures 0W and stores it periodically into an InfluxDB. When it is on, it stores the actual power consumption (e.g. 120 W) periodically as well.

Let’s say I have a time window of 24 hours and within this window there are two periods of 60 minutes each where the power consumption is >0. Thus, I would like to have the value 120 (minutes) returned to display it as my online time.

I am able to display the current consumption in Grafana like this:

from(bucket: "iobroker")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "TS011F_plug_1.load_power")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)

Any help is appreciated.

I can’t help you with Flux specifically (I haven’t moved on from InfluxQL yet),
however I think what you want to do in general is to simply count the number
of non-zero samples you have in the 24 hour time window, and then multiply
this by the sampling interval of your power meter.

So, if you take a power sample every minute, and your computer was on for two
1-hour periods, you would have 60+60 = 120 non-zero samples.

If your sampling is only every 5 minutes, then you would have 12+12 = 24, and
24*5 = 120 non-zero samples.

If your sampling is every 5 seconds, then you would have 1260 + 1260 = 1440,
and 1440 * (5/60) = 120 non-zero samples.

I hope that helps to point you in a helpful direction.

Antony.

Thank you for the reply, but I don’t think it will work as there may be storing outages, e.g. network problems, donwtimes etc that will affect the number of non-zero samples.

Hm, I don’t think any query can help you to reconstruct missing data :frowning:

Antony.

Reconstructing data is not necessary. Each “online window” has a predecessor point that is 0W, then x points that are >0W and a sucessor point that is again 0W. x is variable.

The challenge is to add the duration of all these windows.