Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Hi,

whats wrong in my template?

Template warning: ‘float’ got invalid input ‘unavailable’ when rendering template ‘{% set total = states(‘sensor.growattmodbus_output_power’) | float %} {% set production = states(‘sensor.power_production’) | float * 1000 %} {% set consumption = states(‘sensor.power_consumption’) | float * 1000 %} {{ [ total - production -consumption, 0 ] | max | round(0) }}’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2021.12

my sensor: yaml:

      solar_percent:
        value_template: "{{ ((states('sensor.growattmodbus_output_power')|float * 100) / ((states('sensor.power_consumption')|float * 1000) + states('sensor.growattmodbus_output_power')|float))|round(1) }}"
        unit_of_measurement: '%'
        friendly_name: Current Solar Percentage Realtime

In other words, what should I add/change?

See here:

Or if you’re in a hurry, replace float with float(0).

2 Likes
  • TemplateError(‘ZeroDivisionError: float division by zero’) while processing template ‘Template("{{ ((states(‘sensor.growattmodbus_output_power’)|float(0) * 100) / ((states(‘sensor.power_consumption’)|float(0) * 1000) + states(‘sensor.growattmodbus_output_power’)|float(0)))|round(1) }}")’ for attribute ‘_attr_native_value’ in entity ‘sensor.solar_percent’
  • TemplateError(‘ZeroDivisionError: float division by zero’) while processing template ‘Template("{{ ((states(‘sensor.pzem016modbus_w’)|float(0) * 100) / ((states(‘sensor.power_consumption’)|float(0) * 1000) + states(‘sensor.pzem016modbus_w’)|float(0)))|round(1) }}")’ for attribute ‘_attr_native_value’ in entity ‘sensor.solar_percent_pzem’

Grrrrr

You were in a hurry.

Seems ((states(‘sensor.power_consumption’)|float(0) * 1000) + states(‘sensor.growattmodbus_output_power’)|float(0))) returned 0, and dividing by 0 is not allowed.

use an availability_template so no divides by 0 happen.

How do I do that?

     solar_percent:
        value_template: "{{ ((states('sensor.growattmodbus_output_power')|float(0) * 100) / ((states('sensor.power_consumption')|float(1) * 1000) + states('sensor.growattmodbus_output_power')|float(1)))|round(1) }}"
        unit_of_measurement: '%'
        friendly_name: Current Solar Percentage Realtime
        availability_template: "{{ ((states('sensor.power_consumption')|float(0) * 1000) + states('sensor.growattmodbus_output_power')|float(0))) != 0}}"

Why float(1) here and there?

And I get
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['solar_percent']['availability_template']. Got "{{ ((states('sensor.power_consumption')|float(0) * 1000) + states('sensor.growattmodbus_output_power')|float(0))) != 0}}". (See ?, line ?).

To avoid dividing by 0. the availability_template will prohibit the sensor to have an invalid value, but for now the value_template is still processed (but not used if availability is false). There is a PR to fix this.

So this error is there, but should not be? The code is correct but homeassistant is not? So I should wait to code like this and wait for an update ?

Missed a (

      solar_percent:
        value_template: "{{ ((states('sensor.growattmodbus_output_power')|float(0) * 100) / ((states('sensor.power_consumption')|float(1) * 1000) + states('sensor.growattmodbus_output_power')|float(1)))|round(1) }}"
        unit_of_measurement: '%'
        friendly_name: Current Solar Percentage Realtime
        availability_template: "{{ ((states('sensor.power_consumption')|float(0) * 1000) + (states('sensor.growattmodbus_output_power')|float(0))) != 0}}"

Yes, now parsing well, what’s the PR for? Will it not succeed now?

the PR is for not evaluating value_template (and not using it) when availability is false