What is the problem? Utility meter sensor doesn't work properly

Hello,
I have an odd problem with an utility meter sensor that tracks my kWh. I have some Shelly EM that track some “sections” of my house and a value template to sum every section to have the total W and kWh of the house.
If one (or more) Shelly EM goes offline or reboots or turn off, the value template increase its value with an abnormal error.

I had some abnormal increase previously. The total should be aroud 250 kWh for the reference.

template:
  - sensor:
      - name: "Assorbimento Villa"
        unit_of_measurement: 'W'
        state: >
          {{ states("sensor.assorbimento_casa")|int(0) +
          states("sensor.assorbimento_location")|int(0) +
          states("sensor.assorbimento_rosso")|int(0) +
          states("sensor.assorbimento_verde")|int(0) }}
        device_class: power
        
      - name: "Consumo Villa"
        unit_of_measurement: 'kWh'
        state: >
          {{ (states("sensor.contatore_casa")|float(0) +
          states("sensor.contatore_location")|float(0) +
          states("sensor.contatore_rosso")|float(0) +
          states("sensor.contatore_verde")|float(0))|round(3) }}
        device_class: energy

and the utility meter:

utility_meter:
  contatore_villa:
    source: sensor.consumo_villa
    cycle: monthly
homeassistant:
  customize:
    sensor.contatore_villa:
      state_class: total_increasing
      friendly_name: Contatore Villa

any idea?

You need to add an availability template to prevent the sensors reporting 0 (from the float filter default) when they are unavailable.

      - name: "Consumo Villa"
        unit_of_measurement: 'kWh'
        state: >
          {{ (states("sensor.contatore_casa")|float(0) +
          states("sensor.contatore_location")|float(0) +
          states("sensor.contatore_rosso")|float(0) +
          states("sensor.contatore_verde")|float(0))|round(3) }}
        availability: >
          {{ states("sensor.contatore_casa")|is_number and
          states("sensor.contatore_location")|is_number and
          states("sensor.contatore_rosso")|is_number and
          states("sensor.contatore_verde")|is_number }}
        device_class: energy

Total → 0 → Total, adds the whole total to the utility meter.
Total → unavailable → Total, does nothing to the utility meter total.

1 Like

Thank you very much for your reply. I thought that adding (0) was ok for unavailable too. I will try to understand more about it, now I am a bit confused :slight_smile:
I will implement your code and report.

Thanks again

It always a good idea to supply defaults. The issue occurs because of the way the utility meter behaves:

To avoid this we replace the template sensor output with unavailable using the availability template when the source sensors are unavailable.

Got it. Could you please explain me one more thing? The first 2 sensors are 2 inputs in the first Shelly EM, the 3rd and 4th are another Shelly EM. What happens if just one out of two Shelly is offline? I think that, since there is an “and”, the total sensor will be unavailable because 2 sensors are not number. Is it a problem?

All of the sensors have to be available for the template to output a result. This prevents similar jumps in the utility meter total (though not as big) if one sensor becomes unavailable momentarily.

1 Like

It works perfectly. Thanks!

I faced a similar problem. I have a zigbee electricity meter with energy sensor in Wh, I also have a sensor with the conversion of these Wh to kWh and I also have a utility meter sensor in which the above-mentioned conversion sensor (Wh to kWh) is used as a source, and this utility meter sensor is calibrated with my physical electricity meter.

If my zigbee meter is unavailable or I reload templates and etc., the utility meter always adds the already accumulated last value from the sensor in Wh, and I always have 6650+ extra kWh.

My template looks like this and it solved the problem:

      - name: electricity_input_wh_to_kwh
        state: "{{ (states('sensor.corridor_input_electricity_meter_energy_l2') | float(0) / 1000) | round(2) }}"
        unit_of_measurement: "kWh"
        availability: "{{ states('sensor.corridor_input_electricity_meter_energy_l2') | is_number }}"

Thanks Tom! :+1: @tom_l

1 Like