home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
70.11k stars 29.16k forks source link

Error while processing template - jinja2.exceptions.UndefinedError: 'None' has no attribute 'state' #115054

Closed adamf83 closed 2 weeks ago

adamf83 commented 3 months ago

The problem

On my dashboard, I have a Mushroom Chip that within it's content has a template to work out how many lights are switched on at any given time and display the number.

{% set entities = [states['light.bathroom_light'],states['light.hall_light'],states['light.kitchen_lights'],states['light.living_room_lights'],states['light.bedroom_light'],states['light.adams_bedroom_lamp_light'],states['light.bedroom_lamps'],states['light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light'], states['light.essentials_a19_a60']] %} {{ entities | selectattr('state','eq','on') | list | count }} lights on

Since upgrading to 2024.4.0 (and trying 2024.4.1 to see if it was fixed) this seems to have stopped working, producing the following error:

Error while processing template: Template<template=({% set entities = [states['light.bathroom_light'],states['light.hall_light'],states['light.kitchen_lights'],states['light.living_room_lights'],states['light.bedroom_light'],states['light.adams_bedroom_lamp_light'],states['light.bedroom_lamps'],states['light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light'], states['light.essentials_a19_a60']] %} {{ entities | selectattr('state','eq','on') | list | count }} lights on) renders=42>

However, under Developer Tools -> Template when I paste the template into the editor, it is working correctly and displaying the number of lights that are on.

What version of Home Assistant Core has the issue?

core-2024.4.1

What was the last working version of Home Assistant Core?

core-2024.3.3

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/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/local/lib/python3.12/site-packages/jinja2/async_utils.py", line 45, in wrapper
    return normal_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/filters.py", line 1338, in sync_do_list
    return list(value)
           ^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/filters.py", line 1779, in select_or_reject
    if func(item):
       ^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/filters.py", line 1764, in <lambda>
    return lambda item: modfunc(func(transfunc(item)))
                                ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/filters.py", line 1759, in func
    return context.environment.call_test(name, item, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 589, in call_test
    return self._filter_test_common(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 545, in _filter_test_common
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'None' has no attribute 'state'

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 712, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'state'

Additional information

No response

home-assistant[bot] commented 3 months ago

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `template` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign template` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

issue-triage-workflows[bot] commented 3 weeks ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.