I have been tearing my hair out the last couple of weeks while trying to set up a nice Energy Dashboard in Home Assistant. When I thought everything was working fine, I suddenly got consumption values and cost multiplied by a, seemingly, random number.

I now realise that it only happened with sensors that had been defined through a template helper where I used float(0) in a calculation.
I have a template sensor defined as:
{{states('sensor.tasmota_index_toutes_heures')| float(0) / 1000}}
When sensor.tasmota_index_toutes_heures becomes unavailable, like in my case when I restart Home Assistant, the value of the template sensor goes to 0, due to the default value in float(0). When the sensor becomes available again, the template jumps back to its original value, and that change is seen as consumption. Actually quite logic behaviour, but it’s causing problems with my energy consumption tracking.
To solve this problem, the template sensor has to return “none”, instead of 0:
{% if states('sensor.tasmota_index_toutes_heures') in ('unknown','unavailable') %}
{{ None }}
{% else %}
{{states('sensor.tasmota_index_toutes_heures')| float(0) / 1000}}
{% endif %}
Another solution would be to define the template sensor in YAML:
- name: My template sensor
unit_of_measurement: m³
device_class: water
state_class: total_increasing
state: {{states('sensor.tasmota_index_toutes_heures')| float(0) / 1000}}
availability: {{ has_value('sensor.tasmota_index_toutes_heures') }}
2 thoughts on “Energy dashboard – float(0)”