Template sensor value drops to 0

I have a template energy sensor. very simple (see config bellow). I have noticed a strange behavior. sometimes the value drops to 0 literally for one reading, that’s of course further screws up my utility meters.
2020-12-11_09-29-39

- platform: template
  sensors:
    energy_total_usage:
      friendly_name: "Total energy usage"
      unit_of_measurement: Wh
      value_template: >-
        {{ (states('sensor.energija_a_faze')|float + states('sensor.energija_b_faze')|float + states('sensor.energija_c_faze')|float) }}

the sensors used in this template gets data from ESPHome integration. I thought maybe in some cases I got 0 from integration. but if I check this sensors, everything looks like ok, there are no zeros recorded.

Any ideas?

Most likely because one or all sensors are unavailable.

What do they display as state at that time?

That’s exactly why. If a sensor state is unknown or unavailable then |float will convert it to 0.

Try this:

      value_template: >-
        {% if states('sensor.energija_a_faze')|float + states('sensor.energija_b_faze')|float + states('sensor.energija_c_faze')|float == 0 %}
          {{ states('sensor.energy_total_usage') }}
        {% else %}
          {{ (states('sensor.energija_a_faze')|float + states('sensor.energija_b_faze')|float + states('sensor.energija_c_faze')|float) }}
        {% endif %}

The other option is to use an availability template:

- platform: template
  sensors:
    energy_total_usage:
      friendly_name: "Total energy usage"
      unit_of_measurement: Wh
      value_template: >-
        {{ (states('sensor.energija_a_faze')|float + states('sensor.energija_b_faze')|float + states('sensor.energija_c_faze')|float) }}
      availability_template: >-
        {{ states('sensor.energija_a_faze') not in ['unknown', 'unavailable'] and states('sensor.energija_b_faze') not in ['unknown', 'unavailable'] and states('sensor.energija_c_faze') not in ['unknown', 'unavailable']  }}
1 Like

What if it actually is 0?

Isn’t it better to if != unknown?

I just edited my post. See the second option.

Though I don’t think any of the sensors it is composed of should ever be zero. They should only ever increase. However one of them may become available before the rest so this would be better:

      value_template: >-
        {% if states('sensor.energija_a_faze')|float == 0 or states('sensor.energija_b_faze')|float == 0 or states('sensor.energija_c_faze')|float == 0 %}
          {{ states('sensor.energy_total_usage') }}
        {% else %}
          {{ (states('sensor.energija_a_faze')|float + states('sensor.energija_b_faze')|float + states('sensor.energija_c_faze')|float) }}
        {% endif %}

I’d still go with the availability template.

1 Like

Thanks, man. I have checked the data archive, and I see that, indeed, all composing sensors were unavailable at this time. Probably because ESP has lost wifi connection or were some issues with Modbus. anyway this is another story.

I like your solution with the availability template. And will try this way.

I think it would be good to have quality value for analog sensors :slight_smile: We have our own remote control system where we also have qualities like GOOD, OLD, UNK, HI, LOW, etc :wink: very convenient.

Sensors actually can be 0 if I decide to reset or change the meter or meter overflow :wink:

Could it have been after a restart of home assistant?

The template platform takes a small amount of time to set up.

This is interesting. For anyone else doing this same setup with utility meter, I’d suggest using the value_template method…

I had this same issue and used first the availability template to fix it. Now I’ve found that while unavailable, the utility meter loses all the energy used during that time period.

For example, say I lost my kWh sensor for 1 hour. The sensor was at 6 kWh, then went unavailable, but came back at 7 kWh 1 hour later. The utility meter will continue increasing from 6 kWh, completely ignoring the 1 kWh increase that happened while unavailable.

I’m going to try the value_template hack of setting the template sensor to it’s own state when the original sensors go unavailable, as I think that would allow the utility meter to “catch up” and be accurate again.

1 Like