Default value for Long-term statistics (total_increasing) template sensor

Finally got around to try out the new Energy management module.

I got myself a Frient Electricity Meter (ZHEMI101) witch worked surprisingly good. The meter is connected through ZHA and presents two sensors smartenergy_metering and smartenergy_metering summation_delivered. The smartenergy_metering summation_delivered is a summation of consumed kwh (perfect to be used with the energy module). The sensor does however not fulfill the requirements to be used as a an energy sensor and therefore as a workaround I created a template sensor:

template:
  sensor:
    - name: "appartment Energy Consumption"
      state: "{{ states('sensor.frient_power_meter_smartenergy_metering_summation_delivered') | float / 1000 }}"
      unit_of_measurement: kWh
      state_class: total_increasing
      device_class: energy

This works great until I restart home assistant. This triggers the following log:

Template warning: ‘float’ got invalid input ‘unavailable’ when rendering template ‘{{ states(‘sensor.frient_power_meter_smartenergy_metering_summation_delivered’) | float / 1000 }}’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.1

and as the value is temporarily 0 this causes the energy calculation to reset and when the sensor updates all consumed energy logged by the sensor so far to be instantly reported causing a huge spike messing up my long term statistics.

What should I use as the default value for the template senor to avoid this behavior?

I am in the same boat as you, i am trying to add it to the energy dashboard but it doesnt show up in the drop down. Many different people have posted different qork arounds with zigbee2mqtt but I am using ZHA. If you get this working or someone manages to help it would be awesome.

After a modification to the template sensor it at least does not default to 0 causing a spike in the long term sensor but it still causes the warning in the log. looks like I will have to dig into this before the next release.

template:
  sensor:
    - name: "appartment Energy Consumption"
      state: "{{ states('sensor.frient_power_meter_smartenergy_metering_summation_delivered') | float /1000 or float(none) }}
      unit_of_measurement: kWh
      state_class: total_increasing
      device_class: energy
1 Like