Template warning: 'float' got invalid input 'unknown' when rendering template

hi,
I have a sensor that monitors the consumption of two pzem:

    consumo_totale_watts:
      friendly_name: "Consumo totale Watts"
      value_template: "{{ states('sensor.pzem_016_energy_power') | float + states('sensor.pzem_016_2_energy_power') | float }}"
      unit_of_measurement: 'W'

in the logs i can see:

**Template warning: 'float' got invalid input 'unknown' when rendering template '{{ states('sensor.pzem_016_energy_power') | float + states('sensor.pzem_016_2_energy_power') | float }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1**

could you advise me how to eliminate the error?


thank you so much in advance for your help
2 Likes

The value of one of the two sensors (or both) was unknown.

The float filter cannot convert the value “unknown” to a number.

When it cannot convert the value, it can report a default value.

In all previous versions of Home Assistant, the default value was zero.

Starting in version 2021.10.0, you should specify the default value. If you don’t, and float cannot convert the value, it will be reported as a warning message and Home Assistant uses zero as a default value.

Starting in version 2021.12.0, you must specify the default value. If you don’t, and float cannot convert the value, it will be reported as an error message and the template will fail.

Change it to this:

      value_template: "{{ states('sensor.pzem_016_energy_power') | float(0) + states('sensor.pzem_016_2_energy_power') | float(0) }}"

You should also investigate why the sensor value was unknown.


For more information about specifying default values for filters, refer to the following:

9 Likes

Thank you ! warning disappeared, maybe it’s the fault of the pzem sensor with sonoff

What can I do if that does not work and the warning still appears. Please see my code here:

Currently 
                  {% if states('sensor.people_at_home') | float(0) == 0 %} 
                    nobody is 
                  {% elif states('sensor.people_at_home') | float(0) == 1 %} 
                    {{ (states('sensor.people_at_home') | float(0)) | round(0) }} person is 
                  {% else %} 
                    {{ (states('sensor.people_at_home') | float(0)) | round(0) }} persons are 
                  {% endif %} 
                  at home."
{% set qty = states('sensor.people_at_home') | int(0) %}
Currently {{ 'nobody' if qty == 0 else qty }} {{ 'is' if qty == 0 else 'persons are' if qty > 1 else 'person is' }} at home.
1 Like

I think {{ (states('sensor.people_at_home') | default(0) | float(0)) | round(0) }} should do it.

The inclusion of default in that template serves no purpose. The default filter is meant to provide a default value only if the supplied value is undefined. The result of the states function is never undefined. If sensor.people_at_home doesn’t exist, or its value is unavailable or unknown, then the states function will report something and that’s meaningful to the default (i.e. it’s defined).

You can easily prove it for yourself by copy-pasting this into the Template Editor:

{{ states('sensor.xyz') | default(0)  }}

The result won’t be 0.

2 Likes

I got a similar case where I already added the (0) to the float, but when reloading command_line sensors HA is still complaining:

        value_template: >-
          {% if is_state('sensor.ha_supervisor_updates', 'on') or is_state('sensor.ha_core_update', 'on') or is_state('sensor.haos_update', 'on') or is_state('sensor.updater_supervisor', 'True') or (states('sensor.ha_supervisor_updates') | float(0) > 0) or is_state('sensor.updater_hacs', 'True') or is_state('sensor.deconz_firmware_update_available', 'true') or (states('sensor.nextcloud_system_apps_num_updates_available') | float(0) > 0) or is_state('binary_sensor.updater_1_host', 'on') or is_state('binary_sensor.updater_3_host', 'on') or is_state('binary_sensor.updater_pihole', 'on') or is_state('binary_sensor.fritz_box_firmware_update', 'on') or is_state('binary_sensor.updater_nextcloud', 'on') %}
            True
          {% else %}
            False
          {% endif %}
Logger: homeassistant.helpers.template
Source: helpers/template.py:1291
First occurred: 15. Januar 2022, 15:46:20 (38 occurrences)
Last logged: 01:49:04

Template warning: 'float' got invalid input 'unknown' when rendering template '{% if is_state('sensor.ha_supervisor_updates', 'on') or is_state('sensor.ha_core_update', 'on') or is_state('sensor.hass_os_update', 'on') or is_state('sensor.updater_supervisor', 'True') or (states('sensor.ha_supervisor_updates') | float > 0) or is_state('sensor.updater_hacs', 'True') or is_state('sensor.deconz_firmware_update_available', 'true') or (states('sensor.nextcloud_system_apps_num_updates_available') | float > 0) or is_state('binary_sensor.updater_1_host', 'on') or is_state('binary_sensor.updater_3_host', 'on') or is_state('binary_sensor.updater_pihole', 'on') or is_state('binary_sensor.fritz_box_firmware_update', 'on') or is_state('binary_sensor.updater_nextcloud', 'on') %} True {% else %} False {% endif %}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
Template warning: 'float' got invalid input 'unknown' when rendering template '{% if is_state('sensor.ha_supervisor_updates', 'on') or is_state('sensor.ha_core_update', 'on') or is_state('sensor.haos_update', 'on') or is_state('sensor.updater_supervisor', 'True') or (states('sensor.ha_supervisor_updates') | float > 0) or is_state('sensor.updater_hacs', 'True') or is_state('sensor.deconz_firmware_update_available', 'true') or (states('sensor.nextcloud_system_apps_num_updates_available') | float > 0) or is_state('binary_sensor.updater_1_host', 'on') or is_state('binary_sensor.updater_3_host', 'on') or is_state('binary_sensor.updater_pihole', 'on') or is_state('binary_sensor.fritz_box_firmware_update', 'on') or is_state('binary_sensor.updater_nextcloud', 'on') %} True {% else %} False {% endif %}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

What does HA see what I don’t?

Do I maybe need to restart HA? Because the one finding seems to be some kind of caching issue, as I renamed the sensor.hass_os_update to sensor.haos_update recently (no restart after that).

The documentation in HA still does not match this new behavior.


This behavior also is not documented in JinJa web site (the engine HA supposidly is using for functions and filters):
https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.float

I just ran into this issue yesterday and wasted a good day trying to figure out what was wrong because the documentation is at least 1/2 year out of date.
I have reported this documentation issue on HA Document Feedback page.
Sigh

3 Likes

so what’s the verdict on this if the document is bad?

how do we use float now?

The documentation matches the new behavior. You have to supply a default if your templates fail to convert the string to a float.

if this is the case… I have a question about syntax (possibly my template sensor is bad?)

2022-05-25 11:07:37 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ ((states('sensor.sauna_target_temperature') | float(default)) * 9 / 5) + 32 }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1859, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 926, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'default' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 525, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 411, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'default' is undefined
2022-05-25 11:07:37 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'default' is undefined') while processing template 'Template("{{ ((states('sensor.sauna_target_temperature') | float(default)) * 9 / 5) + 32 }}")' for attribute '_attr_native_value' in entity 'sensor.huum_target_temperature'

This is what throws the exception:

- sensor:
  - name: "HUUM Target Temperature"
    unit_of_measurement: "F"
    state_class: measurement
    device_class: temperature
    state: "{{ ((states('sensor.sauna_target_temperature') | float(default)) * 9 / 5) + 32 }}"

This does not… however, it doesn’t follow the document (the above does)

- sensor:
  - name: "HUUM Target Temperature"
    unit_of_measurement: "F"
    state_class: measurement
    device_class: temperature
    state: "{{ ((states('sensor.sauna_target_temperature') | float(0)) * 9 / 5) + 32 }}"

That’s correct… default is the variable name. You aren’t supposed to copy the docs exactly, you’re suppose to supply a default.

1 Like

thanks for the clarification.
may have been stuck in my head from the previous version.

Thanks for the clarification, for me as well.

But the solution “float(0)” does not give the desired result in my case.
For short:
I have a gas counter realized with a reed sensor, which triggers a counter on a Wemos D1 Mini, flashed with Tasmota. This counter performes perfect since a couple of weeks already. However, as one reed sensor impuls represents 0.01 m³ gas, I calculated the counter value with this construct:

sensor:
  - platform: template
    sensors:
      gasmota_counter_c1_3:
        friendly_name: "Gas-Zähler"
        unit_of_measurement: "m³"
        device_class: gas
        unique_id: uniqueid__sensor_gasmota_counter_c1_3
        value_template: "{{ states('sensor.gasmota_counter_c1') | float * 0.01 }}"

This works fine, but produces the error mentioned in the topic. After simply replacing float by float(0) the error message is gone.
… but…
When restarting HA (which I do once per night), the first value for m³ results as a 0 (zero). Thus, the next value will be as expected and the difference in the energy dashboard is a full scale value, which is a total mess.

How exactly is the trick, that it works exactly as my original version, but without the error message?

1 Like

You can check in the value_template if the value is ‘floatable’ (a float number) and above 0.
This way you skip ‘unknown’ states.

  value_template: >
     {% if states('sensor.gasmota_counter_c1') | float(0) > 0 %}
        {{ states('sensor.gasmota_counter_c1') | float(0) * 0.01 }}
     {% endif %}
1 Like

This is not, what I expected. I thought about defining a default value for float(default), which could map the current value.
Anyway, your solution skips the initial zero and works fine. Pragmatic, and working as it should.

Thanks, bertreb!

what can i do here?

  sensors:
    buro_dect200_1_current_power_w:
      value_template: "{{state_attr('switch.buro_dect200_1', 'current_power_w') | round(2)}}"
      friendly_name: "Leistung"
      unit_of_measurement: 'W'

Error:

  • TemplateError(‘ValueError: Template error: round got invalid input ‘None’ when rendering template ‘{{state_attr(‘switch.buro_dect200_1’, ‘total_consumption’) | round(2)}}’ but no default was specified’) while processing template ‘Template(“{{state_attr(‘switch.buro_dect200_1’, ‘total_consumption’) | round(2)}}”)’ for attribute ‘_attr_native_value’ in entity ‘sensor.buro_dect200_1_total_consumption’

Supply a default to round

i hope this work:

value_template: "{{ state_attr('switch.buro_dect200_1', 'current_power_w') | default(0) | round(2) }}"