Template sensors this morning started calculating zero energy intermittently, causing energy usage to go haywire

For some reason my two template energy sensors have stopped working correctly. For some reason they sometimes calculate to 0 which is really strange. This makes HA believe that the usage goes from 0 - whatever they have actually used, several times a day… Giving me a fantastic energy usage. This is from today… The water heater has used close to 3.000 kWh… LOL

template:
  - sensor:
      - unique_id: varmvattentotalenergy003
        name: "Varmvatten Total Energy"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: >
          {{ states("sensor.varmvatten_gra_energy") |float + states("sensor.varmvatten_svart_energy") | float + states("sensor.varmvatten_brun_energy") | float }}
      - unique_id: varmvattentotalpower001
        name: "Varmvatten Total Power"
        unit_of_measurement: "W"
        device_class: "power"
        state_class: "measurement"
        state: >
          {{ states("sensor.varmvatten_gra_power") |float + states("sensor.varmvatten_svart_power") | float + states("sensor.varmvatten_brun_power") | float }}
      - unique_id: halltotalpower001
        name: "Häll Total Power"
        unit_of_measurement: "W"
        device_class: "power"
        state_class: "measurement"
        state: >
          {{ states("sensor.hall_vanster_power") |float + states("sensor.hall_hoger_power") | float }}
      - unique_id: halltotalenergy001
        name: "Häll Total Energy"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: >
          {{ states("sensor.hall_vanster_energy") |float + states("sensor.hall_hoger_energy") | float }}

There is my sensors setup. They have been working just fine since I set them up… It is basically 2-fase appliances where I add the individual usage to one sum for the whole appliance.

The underlying “base” sensors are reporting the correct value to HA, and the correct values are recorded and displayed on an individual basis. It is only these sums that are wrong.

That’s how the composite sensor reports. Obviously something wrong there. I have restarted HA, didn’t help unfortunately.

Any ideas would be greatly appreciated.

Check your logs for template errors.

You are not supplying default values for your float filters.

You also need an availability template for when any of the sensors are unavailable to prevent erroneous output. Search the forum there are many post about this.

1 Like

Thanks @tom_l

Alright, this appears to be working, One question though, Is the preffered way to handle “unavailability”? Not do anything at all (like in this code) or to return “Unavailable”… What would be considered best practis and work best with HA?


{% if states('sensor.varmvatten_svart_power')|float(-99999) > -99999 
    and states('sensor.varmvatten_gra_power')|float(-99999) > -99999
    and states('sensor.varmvatten_brun_power')|float(-99999) > -99999 -%}
  {{ 
    states('sensor.varmvatten_svart_power')|float(0) 
    + states('sensor.varmvatten_gra_power')|float(0) 
    + states('sensor.varmvatten_brun_power')|float(0)
  }}
{%- endif %}

I guess I could add an else statement with that, but how would HA handle it?

{%- else -%}
  Unavailable

No. Just use your existing state template but supply default values like this

state: >
  {{ 
    states('sensor.varmvatten_svart_power')|float(0) 
    + states('sensor.varmvatten_gra_power')|float(0) 
    + states('sensor.varmvatten_brun_power')|float(0)
  }}

Then add an availability template like this:

availability: >
  {{ 
    states('sensor.varmvatten_svart_power')|is_number
    and states('sensor.varmvatten_gra_power')|is_number
    and states('sensor.varmvatten_brun_power')|is_number
  }}
1 Like

Thank you again @tom_l I’m forever grateful. It appears data is now reported correctly.

As a note to anyone else finding this post in the future: Since both energy sensors are (I believe correctly) set up as state_class: "total_increasing" I had to do some clean up of bad data in the database. I found this article https://developers.home-assistant.io/docs/core/entity/sensor/#state_class_total_increasing very helpful in understanding the behavior of the total_increasing state_class and why I got the data I got (TL;DR any decrease in state/value is interpreted as a reset of the devices measurement, ie a meter is reset to 0 on the device, or perhaps exchanged for a new device). I found this post useful https://community.home-assistant.io/t/how-to-fix-statistics-data-e-g-energy-data/360966 in verifying what needed to be done (had mostly figured that out but nice to see others coming to the same conclusion)

However: That last article does not (as far as I can tell) handle the statistics_short_term database table. And from my experience you will need to do the same for this table as for the long term statistics database table. I.e adjusting all values from after the time of your error. From my experience it appears HA Puts a snapshot into statistics_short_term every 5 minutes, and into statistics every hour and crucially the creation of the hourly statistics data is based on an aggregation of statistic_short_term data. So: Live – every 5 min → statistics_short_term – every hour → statistics. I cannot find any documentation on this (though I’m sure it exists) and I might very well be wrong, in which case I gladly stand corrected. Please comment if you know.

Finally I also removed any erroneous values from the states table. Just to make sure the graphs looked good everywhere.

Again thanks for helping me out @tom_l