I have a power meter that measures instantaneous consumption in Watts and writes to Prometheus every minute. I’m trying to make a Grafana panel showing total energy consumed in the visible time period in Watt-hours. I tried avg_over_time(sensor[1h])
but this is not working. It creates the graph shown here, which goes up and down. I would expect the energy consumption graph to always go up. I also tried playing with different range values, and sum_over_time
and I can’t find a way to get an always increasing graph.
maybe something like this?
Thanks, but it looks like that uses InfluxDB and I don’t know how to translate it to Prometheus.
Do you know if your metric is counter or gauge? Counter would show you the total energy consumed from the time the metric was started being scraped (but if your metric resets to 0 it would reset too).
If you need to see total energy consumed during the period in Grafana time picker, you can do the following:
- if your
sensor
metric is a counter, you can calculate increase on it with [$__range] lookbehind window. - if your
sensor
metric is a countersum_over_time(<metric>[$__range])
should suffice.
Anyway, I don’t think you could ever receive a plot that is only going upward. What if your metric source restarts? Then all the metrics will drop to 0, so the plot would drop.
If you don’t mind, for such a case, I’d recommend more of a stat chart - you’ll get one value that would show how much energy you’ve consumed from now to .
The Watts sensor is a gauge, measuring instantaneous power consumption.
I want to see how much energy is consumed each day over a time range of 30 days. A stat panel shows only a single number, so I’d have to do some work and change my time range to write that down for each day — not ideal, but that would at least be a start. But I can’t get that to work either. Below are a few things I’ve tried and the resulting stat panel, with a 24h time range.
avg_over_time(hass_sensor_power_w{entity="sensor.consumption"}[$__interval])
avg_over_time(hass_sensor_power_w{entity="sensor.consumption"}[$__range])
avg_over_time(hass_sensor_power_w{entity="sensor.consumption"}[$__rate_interval])
sum_over_time(hass_sensor_power_w{entity="sensor.consumption"}[$__interval])
sum_over_time(hass_sensor_power_w{entity="sensor.consumption"}[$__range])
sum_over_time(hass_sensor_power_w{entity="sensor.consumption"}[$__rate_interval])
Here is an image showing what I want. I found this online made by someone else, but they used influx which supports GROUP BY
. I’m using Prometheus, hoping this can be done either with PromQL or Grafana transformations.
The good news is there is an answer to make your graphs look like the last sample you provided even if you are using Prometheus (promql). Here is how I do it:
The “Relative time” variable is not necessary, I prefer this graph populate to a different range than the date/time picker. The division of the em_emeters_power is a function of your scrape time to get an hour’s worth of current or power (power in this case) so you count watthours properly. In my case, my shelly-em scrape_interval is 10s so take the number of seconds in an hour (3600) and divide it by 10s to get 360. The faster your scrape_time, the more accurate this will be since each scrape is an instantaneous reading and not an average. The end result is this:
Notice the time picker is set to the last 2 days but “Var” is set to 1 so the “Relative time” is set to 1M/M using $var. The dates at the bottom in this case are messy but hovering over each daily metric shows the number I want to see. One more thing worthy of note: Since the query is from 00:00 to 00:00, the date reflects the end date of the metrics period so. In other words, the date displayed is for the metrics leading up to that time or the day before.