Energy template sensor: glitches around unavailability of sources

Hi

I am trying to track energy use by device category. I have created a bunch of template sensors summing individual devices, but every so often I have a glitch and the sensor report 100s of kWh energy use.

For a simple example:


    # Sum of the energy use of all the electric heaters
    - name: Heating Electric All Energy
      icon: mdi:flash
      device_class: energy
      unit_of_measurement: "kWh"
      state_class: total_increasing
      unique_id: "489y59ryeuuenueyne6yg2h958472y"
      state: >
        {% set total = states('sensor.plug_shower_grzejnik_electric_consumption_kwh')|float(0)
            + states('sensor.plug_bath_grzejnik_electric_consumption_kwh')|float(0) %}
          {{total}}

Where I am summing the energy consumption of reported by two zwave smart plugs.

I have tracked the glitches to zwave server upgrades: I guess the source sensors become unavailable and then, when they reappear, the total sensor state is added into the template sensor as new energy use in that cycle.

What’s the best way of avoiding such behaviour?

Add an availability template.

    - name: Heating Electric All Energy
      icon: mdi:flash
      device_class: energy
      unit_of_measurement: "kWh"
      state_class: total_increasing
      unique_id: "489y59ryeuuenueyne6yg2h958472y"
      state: >
        {% set total = states('sensor.plug_shower_grzejnik_electric_consumption_kwh')|float(0)
            + states('sensor.plug_bath_grzejnik_electric_consumption_kwh')|float(0) %}
          {{total}}
      availability: "{{ states('sensor.plug_shower_grzejnik_electric_consumption_kwh')|is_number and states('sensor.plug_bath_grzejnik_electric_consumption_kwh')|is_number }}"

How simple.

Thanks!

1 Like