Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adamf83 opened this issue Apr 6, 2024 · 1 comment

Comments

@adamf83
Copy link

adamf83 commented Apr 6, 2024

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
Copy link

home-assistant bot commented Apr 6, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant