DIY-Viking

If you want something done right…

Energy dashboard – float(0)

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') }}

Honi

2 thoughts on “Energy dashboard – float(0)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
WordPress Appliance - Powered by TurnKey Linux