Here’s an explanation how to deal with utility meter counter in mini graph card using the group_by: interval feature, which has not been explained so far.
This is an example about monitoring energy consumption with counters created by the utility meter integration which resets the particular 4 sensors every hour, day, week, month once they are created for
- kWh_hourly
- kWh_daily
- kWh_weekly
- kWh_monthly
ATTENTION: each sensor counts up until the end of its intervall. Then it’ll be resetted to 0
kWh_daily shows you the consumption of each day + current day so far, same for hourly.
Here is a graph of such a kWh_weekly sensor (code follows below) on Sat. 8 pm:
- You can see steadily growing bars
- till the counter will be resetted (end of 7th day) and the next bar will be tiny again, just the consumption of first day of the week.
- the marked last bar shows a value of 2.38 kWh which represents the current weekly counter till now, so 5 days and 20 hours right now (8 pm means last 4 hours of saturday + 24 hours of sunday are missing in that 2.38 kWh figure for a full week)
Here is the card and then the key lines you have to take for:
type: 'custom:mini-graph-card'
name: COUCH
smoothing: false
hours_to_show: 336
points_per_hour: 1
line_width: 2
line_color: orange
animate: true
height: 80
entities:
- sensor.kwh_2_3_weekly
font_size: 70
show:
graph: bar
points: true
icon: false
labels: true
name: true
group_by: date
aggregate_func: max
decimals: 3
lower_bound: 0
upper_bound: 3
Now the explanation of the key lines:
-
hours_to_show: 336
means a 14 day history (14 x 24 h = 336 h)
-
points_per_hour: 1
means that at the end 1 value will be used for each hour
-
group_by: date
means that all the hourly values of 1 day will be grouped by date or per day in 1 bar
-
aggregate_func: max: 1
is the key for a chart based on a counter cause this will force the chart to use ONLY the biggest one of the 24 hourly values of a day which for a counter always is that of the last hour.
With these parameters you’ll get 1 bar for each day that will increase day by day till the end of sunday when that weekly reset will happen as seen above in the consumption that was 2.38 kWh (ATTENTION: weekly counter, not daily) on Saturday 8 pm missing the figures of the 4 hours of Saturday evening and full Sunday.
But I wanted to see only 2 figures for each of the past 2 weeks and NOT 14 daily bars of which only the 2 sunday bars show the consumption of the week.
And here at this point I stumbled a lot and could not really get any further cause I could use the consumption ‘group_by: hour
’ which is usefull for an hourly sensor to see hourly bars.
With the ‘group_by: date
’ option I got daily bars from the sensors but there is NO SUCH OPTION FOR group_by: WEEK / MONTH
The only further option (beyond group_by: hour / date) called ‘interval’ was not explained on github nor here or somewhere else cause I had searched for ‘group_by: interval’
Below you see the mini graph card we wanted to get: 1 bar for each week
The 2.392 kWh means the weekly consumption of the current week Sat. 8 pm, so the consumption of tonight (4 hours) and sunday are missing.
But how do we get these 2 bars instead of the previous 14 bars from the weekly kWh counter shown below?
With no such ‘group_by: week
’ option we need to use group_by: interval
But how and where is the interval defined ?
Not so easy cause the only definition you can use indirectly is offered in this line
points_per_hour: 1
But 1 point every hour would mean a lot of values where we are looking for what ?
The consumption of 1 week = 7 days = 168 hours
Here it gets tougher cause the only kind of interval figure we want is the 1 week consumption or 1 figure after 168 hours or every 168th hour !
And this ‘kind of interval’ can be assigned indirectly to points_per_hour:
We do not need 1 or more points per hour but every 168 th hour which means in terms of points_per_hours to divide 1 by 168 or 1 / 168.
But unfortunatelly you can not assign it this way: points_per_hour: 1/168
You have to do the calculation 1 / 168 first and put the result into the line:
points_per_hour: 0.00595238095
In total you only have to change these 2 lines in the example code from above
points_per_hour: 0.00595238095
and
group_by: interval
NEXT CHAPTER: MONTHLY FIGURES instead of weekly
If you do not want the weekly figure but the monthly one then you first have to change the sensor from weekly to monthly which in my case would mean for example
- sensor.kwh_0_4_monthly
instead of
- sensor.kwh_0_3_weekly
Then you have to calculate the new intervall by 1 / ( days in this month x 24 h) or for july: 1 / (31 x 24) = 1 / 744 = 0.00134408602
points_per_hour: 0.00134408602
And then there is a final point to adopt cause you want to see the figures of the last few months which means you have to replace hours_to_show: from 336 hours ( past 2 weeks) with 3 months x 744 h or 2.232 hours which has to be changed here
hours_to_show: 2232
Once you have done that you should get 3 bars if you have already recorded the power consumption long enough: 2.232 hours have to be recorded to show 3 monthly figures.
FINAL CHAPTER: DAILY FIGURES OF LAST 2 WEEKS
Just to make it clear: If you’d only like to see the daily consumption instead of the weekly figures mentioned before you would not use the weekly counter sensor but the daily counter sensor or here in my particular case
- sensor.kwh_0_2_daily
instead of
- sensor.kwh_0_3_weekly
After this change the initially code from the top would show you this 14 bar graph of nice daily consumption figures for the last 2 weeks or 336 hours which you can compare with each other easily. If you click on such bar you will see the daily figure.
Now in my case here this 0.321 kWh stands for the consumption of the current day which gives you an orientation about consumption in the days before relatively to today or you hover over an other bar or click on a bar.
This could be easier of cause but I guess now it should be a lot easier to adopt the mini graph to show all those energy / water / money counters (bars / lines) and of cause everyone searching for ‘group_by: intervall’ will find a solution