Utility_meter from template adds value on restart

Hi All,

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 :slight_smile: )

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

Thanks for any help :slight_smile:

No ideas at all? :frowning:

(Sorry to bump :slight_smile: )

Hi @mk-maddin

Did you find a solution for this? I got the same problem here!

Unfortunately not - still in the same issue :frowning:

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…

Hello Maddin,

by now I found that for me the solution was this availability template. The problem occurs when a sensor is not available after the restart.

I added similar lines to my template sensors and since than the problem is completely gone.

I changed to MariaDB as well but that was not the solution for me.

If you like I can send you the yamls of the sensors, though I just used try and error as I ain’t a coder-guy. So no warranty on anything.

Regards
Philipp

Hey,

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)

- sensor:
    - name: "Total Energy Use"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"
      state: >
        {{
        states('sensor.shelly_em3_channel_a_energy')| float(0) +
        states('sensor.shelly_em3_channel_b_energy')| float(0) +
        states('sensor.shelly_em3_channel_c_energy')| float(0)
        }}
      availability: >-
        {{
        [ states('sensor.shelly_em3_channel_a_energy'),
        states('sensor.shelly_em3_channel_b_energy'),
        states('sensor.shelly_em3_channel_c_energy'),
        ] | map('is_number') | min
        }}

- sensor:
    - name: "Total Energy Returned"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"
      state: >
        {{
        states('sensor.shelly_em3_channel_a_energy_returned')| float(0) +
        states('sensor.shelly_em3_channel_b_energy_returned')| float(0) +
        states('sensor.shelly_em3_channel_c_energy_returned')| float(0)
        }}
      availability: >-
        {{
        [ states('sensor.shelly_em3_channel_a_energy_returned'),
        states('sensor.shelly_em3_channel_b_energy_returned'),
        states('sensor.shelly_em3_channel_b_energy_returned'),
        ] | map('is_number') | min
        }}

Please remember I’m no coder. I just made this with copy/paste.

Regards
Philipp