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.
To create a graph showing total energy consumption (in Watt-hours) in Grafana, you need to properly accumulate the instantaneous power data over time. Since your power meter provides data in Watts, you can calculate energy (in Watt-hours) by integrating the power over time.
Here’s how you can do it:
-
Use
rate()
: Therate()
function helps in calculating the change in power over time, but it’s often used with counters. Since your power data is in Watts and not a counter, this might not be directly applicable. -
Use
increase()
orsum_over_time()
: To get the total energy consumed over time, you can use theincrease()
orsum_over_time()
functions. The formula would look something like this:sum_over_time(sensor[1h]) * 60
This will sum the power consumption over each minute and multiply it by 60 to get Watt-hours.
-
Avoid Averaging: Since energy should always increase, avoid using
avg_over_time()
as it averages the data over time, which causes the graph to fluctuate. Instead, focus on summing or increasing the values.
You may need to adjust the time window and functions depending on the data’s characteristics, but using sum_over_time()
should give you the continuously increasing graph you’re looking for.