I have a switch, sensor, motor, … - they have ON, OFF status. I would like a graph to display(calculate) the ON, OFF duration of the device.
If Iam choosing an interval on Grafana I would like to see how much time was ON and OFF that specific device. I would like the correct duration value not just approximation, becuse I have all the data in DB. Is there any method, aggregation, any tool what can I use for such calculation ?
Is this posibbile with Grafana ? Do you have a working example? If not is there any plugin for that ? Maybe I need to use different DB then InfluxDb ?
Thx for the response.
Yes, I know that plugin, I did some test but it looks like its not working as expected. The calculated values are not correct. Do you have some working example ? Thx
The table structure is very simple, you can see above. I have timestamp and ON or timestamp and OFF.
Does not matter the power, just want a correct values. Do you have some sample, or example ?
Thx for the response.
Its not good for me, as I find out I do not have timestamp in series, Iam using InfluxDB.
The timestamp is there but I cannot use in in the query, I don’t know why.
Its show up when I list a value but I can not use it as: SELECT time FORM ‘state’
SELECT state, value, time FROM “state” WHERE (“entity_id” = ‘entrance’ AND “domain” = ‘binary_sensor’) AND time >= now() - 12h limit 2;
Hello. You could use group by clause to divide the time into 1 minute or 1 second slots and select the option to keep last value if there is no sample.
This is how I did it with Postgresql:
We have 2 essential transformations here. First we remove the time without states, this is in my case happens because the database is new. You might not need it or even wish to count these “empty” states. Then the important transformation is the group by states and the will count the number of rows of each states.
In the field settings set the unit to minutes(m).
You can of course groupby seconds in which case the unit will need to be seconds.
And obviously found the answer moments later. Changing to a graph from table format you need to ensure the Value options > Show = All values and NOT calculate.
In case anyone wants to do this same thing in Flux, there are a few ways that I know of:
If your machine or switch is always 0, 1, 0, 1, etc. then something like this:
from(bucket: "RetroEncabulator")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "BinaryData")
|> filter(fn: (r) => r["_field"] == "value")
|> elapsed(unit: 1s)
|> map(fn: (r) => ({ r with elapsedFloat: float(v: r.elapsed)/3600.0 })) // convert seconds to hours
|> filter(fn: (r) => r["_value"] == 0) // add a filter statement after the elapsed function to filter out only those whose _value = 0. This is because the “active” time (when the state 1 goes to state 0) is captured in those records where _value = 0. If you wanted to know the time when the phone is NOT active, you would set the filter where _value = 1.
|> aggregateWindow(every: 1d, fn: sum, column:"elapsedFloat")
|> yield(name: "sum")
If your machine or switch generates consecutive On or Off values (such as 1, 1, 0, 1, 0, 0, 1, 1, etc.), then something like this: