Ha 2024.1.1 - 'dict object' has no attribute 'unavailable'

Hi All,

Since HA 2024.1.0 and 2024.1.1 I see this error. With 2023.12.5 or below no issues. What can this be? A issue in platform: template? (see below the error message)

2024-01-05 18:52:15.793 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% set speed = {'off': 0, 'low': 33, 'medium': 66, 'high': 100} %} {{speed [states('sensor.fan_speed')] | float(0) }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 567, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2305, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/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 1984, in forgiving_float_filter
    return float(value)
           ^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'unavailable'

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 693, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'dict object' has no attribute 'unavailable'
2024-01-05 18:52:15.796 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'dict object' has no attribute 'unavailable'') while processing template 'Template<template=({% set speed = {'off': 0, 'low': 33, 'medium': 66, 'high': 100} %} {{speed [states('sensor.fan_speed')] | float(0) }}) renders=4>' for attribute '_percentage' in entity 'fan.mechanical_ventilation'

Only what I had all the time was this when I restarted HA:

2024-01-05 18:52:17.478 ERROR (MainThread) [pyhap.characteristic] SecuritySystemCurrentState: value=0 is an invalid value.
2024-01-05 18:52:17.479 ERROR (MainThread) [pyhap.characteristic] SecuritySystemTargetState: value=0 is an invalid value.

Template config:


- platform: template
  fans:
    mechanical_ventilation:
      friendly_name: "Mechanische afzuiging"

      value_template: >
        {{ "off" if states('sensor.fan_speed') == 'off' else "on" }}

      percentage_template: >
        {% set speed = {'off': 0, 'low': 33, 'medium': 66, 'high': 100} %}
        {{speed [states('sensor.fan_speed')] | int() }}

      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.fan_high
      turn_off:
        service: switch.turn_on
        data:
          entity_id: switch.fan_standby
      set_percentage:
        service: switch.turn_on
        data_template:
          entity_id: >
            {% set id_mapp = {0: 'switch.fan_standby', 33:'switch.fan_low', 66:'switch.fan_medium', 100:'switch.fan_high'} %}
            {{id_mapp[percentage]}}
      speed_count: 2

Most likely, sensor.fan_speed is not available, and unavailable has no entry in the speed dictionary.

Change this:

        {{speed [states('sensor.fan_speed')] | int() }}`

To this:

        {{ speed.get(states('sensor.fan_speed'), 0) }}

Your version fails to return a valid value if the sensor’s state cannot be found in the speed dictionary. For example if it’s unavailable or unknown.

The version I have suggested will return 0 in that situation.

The final int() filter isn’t needed because the reported value is already an integer.

1 Like

thanks. This did the trick!

1 Like