I have a utility_meter which is configured with a template sensor as source.
The template sensor sums up the “total_consumption” attribute of the three sensors from a Shelly 3PM.
(So the template sensor sumps up 3 kWh values).
The issue I have is that whenever I restart home assistant, it seems the value of my template sensor is added again to the utility_meter…
As an example my template sensor has a state of 5.
I restart HA - after this my utility_meter has a value of 5, too.
I restart HA again - my utility_meter has a value of 10.
I restart HA again - my utility_meter has a value of 15…etc.
(Of course the adding depents on the template sensor value after restart - but for simplicity I assumed in the example it is always 5 )
I already tried to set an offset of 2 minutes for the utility_meter (because I thought maybe the template is 0 after restart and then updates to its actual value which causes the utility_meter to add the value), but it seems the offset does not affect an initialization after restart…
Here is my configuration running on Home Assistant 2022.5.1
utility_meter:
shelly_zaehler_waermepumpe_daily_consumption_total:
name: shelly_zaehler_waermepumpe_daily_consumption_total
source: sensor.shelly_zaehler_waermepumpe_energy_consumption_total
cycle: daily
template:
- sensor:
- name: shelly_zaehler_waermepumpe_energy_consumption_total
unique_id: shelly_zaehler_waermepumpe_energy_consumption_total
state: >-
{% set s = 0 | float %}
{% set s = s + state_attr('sensor.shelly_zaehler_waermepumpe_current_consumption','total_consumption') | regex_replace('(^[0-9]+[.]*[0-9]*)( kWh$)','\\1') | float %}
{% set s = s + state_attr('sensor.shelly_zaehler_waermepumpe_current_consumption_2','total_consumption') | regex_replace('(^[0-9]+[.]*[0-9]*)( kWh$)','\\1') | float %}
{% set s = s + state_attr('sensor.shelly_zaehler_waermepumpe_current_consumption_3','total_consumption') | regex_replace('(^[0-9]+[.]*[0-9]*)( kWh$)','\\1') | float %}
{{ s | float | round(2) }}
availability: >-
{{ states('sensor.shelly_zaehler_waermepumpe_current_consumption') not in ['unknown','unavailable'] and states('sensor.shelly_zaehler_waermepumpe_current_consumption_2') not in ['unknown','unavailable'] and states('sensor.shelly_zaehler_waermepumpe_current_consumption_3') not in ['unknown','unavailable'] }}
device_class: energy
unit_of_measurement: kWh
icon: mdi:meter-electric-outline
How are you using the utility meter? Are you just displaying that on the Energy dashboard?
Why don’t you use a helper instead of creating the utility_meter from yaml?
If you add state_class: measurement to your energy sensor you will be able to add that sensor directly to the Energy dashboard, no?
Something like this:
template:
- sensor:
- name: shelly_zaehler_waermepumpe_energy_consumption_total
unique_id: shelly_zaehler_waermepumpe_energy_consumption_total
state: >-
{% set s = 0 | float %}
{% set s = s + state_attr('sensor.shelly_zaehler_waermepumpe_current_consumption','total_consumption') | regex_replace('(^[0-9]+[.]*[0-9]*)( kWh$)','\\1') | float %}
{% set s = s + state_attr('sensor.shelly_zaehler_waermepumpe_current_consumption_2','total_consumption') | regex_replace('(^[0-9]+[.]*[0-9]*)( kWh$)','\\1') | float %}
{% set s = s + state_attr('sensor.shelly_zaehler_waermepumpe_current_consumption_3','total_consumption') | regex_replace('(^[0-9]+[.]*[0-9]*)( kWh$)','\\1') | float %}
{{ s | float | round(2) }}
availability: >-
{{ states('sensor.shelly_zaehler_waermepumpe_current_consumption') not in ['unknown','unavailable'] and states('sensor.shelly_zaehler_waermepumpe_current_consumption_2') not in ['unknown','unavailable'] and states('sensor.shelly_zaehler_waermepumpe_current_consumption_3') not in ['unknown','unavailable'] }}
device_class: energy
state_class: measurement
unit_of_measurement: kWh
icon: mdi:meter-electric-outline
The utility meter is used in combination of direct sensor from my heatpump to calculate the efficency.
Looking at the big blue bars these happen when I restart…
thanks for that feedback.
If you say “…the solution was the availability template”, do you mean to ensure it works “correctly” (so unavailable whenever one of the entities influencing the template sensor state is unavailable, the whole sensor gets unavailable). OR do you mean the key was to ensure your template sensor was always shown as “available” even when one of the entities was unavailable?
Hi. As far as I understand the sensor waits counting until all influencing entities are in a state that is “is_number”. I also checked that my sensors are
total_increasing
or
total
corresponding to their use case.
From the moment I added this I had no strange values (especially after restarts) any more.
Here’s an example for the template sensors of my shelly em3 (use on your own risk only)