Utility meter suddently increase energy when shelly device is power down

Hi there,

I faced an issue with the utility_meter when the shelly em is powered down for a while. In that moment, the energy read by hass is 0 and then when the device is power up again recover the latest value:

This provoke that the utility_meter that reports de energy has a huge increment (in fact the total of energy fo the device):

I tried to fix it with this condition but without success:

sensor:
  - platform: template
    sensors:
      energia_cocina_wh:
        friendly_name: Energia cocina
        value_template: >
          {% if states('sensor.shellyem_c45bbe6b7459_channel_1_energy') != 'Unknown' %}
            {% set cocina_energy = (states('sensor.shellyem_c45bbe6b7459_channel_1_energy')|float * 1000) | round(0) %}
            {{ ([0, cocina_energy, 999999999] |sort) [1] }}
          {% endif %}
        unit_of_measurement: Wh

Has anyone face the same and fixed it?

Thanks for your support!

Found a solution,

instead of Unknown I should set unavailable that is the status of the sensor when the device is power down :slight_smile:

Your template returns no result if the shelly is unavailable. This is bad practice. Either supply an else case or…

Use an availability template. This is exactly what the availability_template option is for.

https://www.home-assistant.io/integrations/template/#availability_template

sensor:
  - platform: template
    sensors:
      energia_cocina_wh:
        friendly_name: Energia cocina
        value_template: >
            {% set cocina_energy = (states('sensor.shellyem_c45bbe6b7459_channel_1_energy')|float * 1000) | round(0) %}
            {{ ([0, cocina_energy, 999999999] |sort) [1] }}
        unit_of_measurement: Wh
        availability_template: "{{ states('sensor.shellyem_c45bbe6b7459_channel_1_energy')  not in ['unknown', 'unavailable'] }}"

I didn’t know about this, thanks so much!!

1 Like

From that section of the page:
This format still works but is no longer recommended - what is the modern format of adding availability template?

Scroll to the top of the page.

Found it, and have applied it successfully, e.g.:

template:
  - sensor:
      - name: "Exported energy"
        unit_of_measurement: 'kWh'
        device_class: energy
        state_class: total_increasing
        availability: >-
          {{ states('sensor.iammeter1_exportgrid') not in ['unavailable', 'unknown', 'none', 0] }}
        state: >-
          {{ states('sensor.iammeter1_exportgrid')|float }}

…and an example of my calculated ones:

  - sensor:
      - name: "Self used energy (daily)"
        unit_of_measurement: 'kWh'
        device_class: energy
        state_class: total_increasing
        availability: >-
          {{
            states('sensor.yield_energy_daily') not in ['unavailable', 'unknown', 'none'] 
              and 
            states('sensor.exported_energy_daily') not in ['unavailable', 'unknown', 'none']
          }}
        state: >-
          {{ 
            0 if states('sensor.yield_energy_daily')|float(0) == 0 
            else
            states('sensor.yield_energy_daily')|float - 
            states('sensor.exported_energy_daily')|float 
          }}
1 Like