As far as I know, you should then simply be able to calculate the sum for “diff_in_seconds” in Grafana and simply replace the hard-coded values (like “a.time”) with the appropriate variables.
Unfortunately I don’t have the possibility to test it in Grafana myself at the moment, but I hope it gives you an idea.
Greetings
…::edit::…
So it seems our Chromecast has played ~74 hour of media since September
(At least as far as sensor data was recorded)
Sorry for the newbie question, but what do you mean with the variable instead of a.time? I tried to paste the code into Grafana but got this error: “Found no column named time”
but now I get this error: “pq: operator does not exist: date >= boolean”
sorry for all the questions, this all completely new to me!
I grab the music genres from last.fm and I think they are user generated, and almost all music that is from Germany and has something to do with rock is categorised there as Krautrock
I have just tried it in Grafana.
I have no problems. The code is
SELECT
$__timeGroupAlias("time",$__interval)
, b.metric
, SUM(b.diff_in_seconds)/3600 AS hours_in_state
FROM (
SELECT
a.state AS metric
, a.time
, EXTRACT(EPOCH FROM (LEAD(a.time) OVER(ORDER BY a.time ASC) - a.time)) AS diff_in_seconds
FROM ltss AS a
WHERE $__timeFilter(a.time)
AND a.entity_id = 'media_player.chromecast_wohnzimmer'
ORDER BY a.time
) AS b
GROUP BY 1,2
ORDER BY 1,2
Don’t forget to set the Calculation to “Total” if you are using a gauge or pie chart. Otherwise it will try to display the individual values of the specified time intervals.
Because the query already returns decimal values for hours.
Grafana does not know how to convert them. If you remove the “/3600” and thus get the seconds, you can also use your time format:
To exclude certain states, you can simply add a NOT IN function. For example like this:
SELECT
$__timeGroupAlias("time",$__interval)
, b.metric
, SUM(b.diff_in_seconds) AS hours_in_state
FROM (
SELECT
a.state AS metric
, a.time
, EXTRACT(EPOCH FROM (LEAD(a.time) OVER(ORDER BY a.time ASC) - a.time)) AS diff_in_seconds
FROM ltss AS a
WHERE $__timeFilter(a.time)
AND a.entity_id = 'media_player.chromecast_wohnzimmer'
ORDER BY a.time
) AS b
WHERE b.metric NOT IN ('off', 'unavailable')
GROUP BY 1,2
ORDER BY 1,2