TemplateError

Hi

I try to combine the power consumption of various outlets into 1 entity with the code below

total_power_usage:
  unique_id: sensor_totaal_vermogen_netwerkapparatuur
  friendly_name: 'Totaal_vermogen_netwerkapparatuur'
  icon_template: mdi:lightning-bolt
  unit_of_measurement: "W"
  value_template: "{{ (states('sensor.wcd_netwerkapparatuur_power')|float + states('sensor.wcd_nas_power')|float + states('sensor.wcd_backup_server_power')|float) }}"

This works fine, but still I get the error messages in my log file

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:356
First occurred: 16:16:15 (1 occurrences)
Last logged: 16:16:15

TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('sensor.wcd_netwerkapparatuur_power')|float + states('sensor.wcd_nas_power')|float + states('sensor.wcd_backup_server_power')|float) }}' but no default was specified') while processing template 'Template("{{ (states('sensor.wcd_netwerkapparatuur_power')|float + states('sensor.wcd_nas_power')|float + states('sensor.wcd_backup_server_power')|float) }}")' for attribute '_attr_native_value' in entity 'sensor.total_power_usage'
Logger: homeassistant.helpers.event
Source: helpers/template.py:422
First occurred: 16:16:15 (1 occurrences)
Last logged: 16:16:15

Error while processing template: Template("{{ (states('sensor.wcd_netwerkapparatuur_power')|float + states('sensor.wcd_nas_power')|float + states('sensor.wcd_backup_server_power')|float) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1683, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 420, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1915, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1686, in forgiving_float_filter
    raise_no_default("float", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1394, in raise_no_default
    raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('sensor.wcd_netwerkapparatuur_power')|float + states('sensor.wcd_nas_power')|float + states('sensor.wcd_backup_server_power')|float) }}' but no default was specified

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 536, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 422, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('sensor.wcd_netwerkapparatuur_power')|float + states('sensor.wcd_nas_power')|float + states('sensor.wcd_backup_server_power')|float) }}' but no default was specified

Any ideas what goes wrong?

Try this:

total_power_usage:
  unique_id: sensor_totaal_vermogen_netwerkapparatuur
  friendly_name: 'Totaal_vermogen_netwerkapparatuur'
  icon_template: mdi:lightning-bolt
  unit_of_measurement: "W"
  value_template: "{{ (states('sensor.wcd_netwerkapparatuur_power')|float(0) + states('sensor.wcd_nas_power')|float(0) + states('sensor.wcd_backup_server_power')|float(0)) }}"

If that works better I will come back with more suggestions to your sensor.

Thanks Edward!
That brings the sollution for me :+1:

Didn’t notice the (0) to the float

1 Like

What I’ve done here?

  1. Converted your sensor to the new template model
  2. Added device_class so it would be more similar to your other power sensors
  3. Broke down the template into multiple lines to improve readability.
  • you can now change name and entity_id via UI.
template:
  - sensor:
    - name: Total power usage
      unique_id: sensor_totaal_vermogen_netwerkapparatuur
      icon: mdi:lightning-bolt
      unit_of_measurement: "W"
      device_class: power
      state: >-
        {{  0
        + states('sensor.wcd_netwerkapparatuur_power') | float(0)
        + states('sensor.wcd_nas_power') | float(0)
        + states('sensor.wcd_backup_server_power') | float(0)
        }}

That defines a default value in case that sensor is not returning a valid number when converting to float. So, if your sensor is “unknown”, it will use the default, in this case, 0.

By the way, you should try to understand why that sensor is returning “unknown”.