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
69.73k stars 28.88k forks source link

Error while editing templates in lovelace cards #100605

Open erkr opened 9 months ago

erkr commented 9 months ago

The problem

During startup I have a few template errors. It is not the error itself that concerns me (I understand the attribute list was still empty during the call). But I can't find where it is coming from. The groups exist and are old-style groups configured in groups.yaml. the errors:

Error while processing template: Template<template=({{ state_attr('group.temperatures_binnen','entity_id') | join(', ') }}) renders=2>
Error while processing template: Template<template=({{ state_attr('group.temperatures_buiten','entity_id') | join(', ') }}) renders=2>
Error while processing template: Template<template=({{ state_attr('group.temperatures_koelkasten','entity_id') | join(', ') }}) renders=2>

As far I'm aware of I don't have templates with this code (I check my automation, scripts, templates and Lovelace cards). So can it be that this happens when HA e.g. renders the default lovelace view? If so, it is a bug in HA rendering groups that are not yet initialized. Or the template filtering of empty attribute lists has changed since core 2023.9 I added the full log below.

What version of Home Assistant Core has the issue?

core 2023.9.x

What was the last working version of Home Assistant Core?

core 2023.8.x

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Don't know, would start with group

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

The group definitions (old style in groups.yaml):

Temperatures_buiten:
  name: Temperatuur buiten
  entities:
    - sensor.rf433_voorraam_temperature
    - sensor.rf433_tuinhuis_temperature
    - sensor.compensation_sensor_serre_sensor_temperature
    - sensor.weather_forecast_temp

Temperatures_binnen:
  name: Temperatuur binnen
  entities:
    - sensor.lumi_lumi_weather_85afe307_temperature
    - sensor.lumi_lumi_weather_d504e207_temperature
    - sensor.rf433_bijkeuken_temperature
    - sensor.rf433_overloop_temperature
    - sensor.lumi_lumi_weather_8199e406_temperature
    - sensor.tz2000_a476raq2_ts0201_a0e5c2fe_temperature
    - sensor.voorzolder_temperature
    - sensor.wijnkast_temperature

Temperatures_koelkasten:
  name: Temperatuur koelingen
  entities:
    - sensor.lumi_keuken_koelkast_temperature
    - sensor.lumi_bijkeuken_koelkast_temperature
    - sensor.lumi_serre_koelkast_temperature

Anything in the logs that might be useful for us?

Logger: homeassistant.helpers.event
Source: helpers/template.py:570
First occurred: 17:10:41 (3 occurrences)
Last logged: 17:10:41

Error while processing template: Template<template=({{ state_attr('group.temperatures_binnen','entity_id') | join(', ') }}) renders=2>
Error while processing template: Template<template=({{ state_attr('group.temperatures_buiten','entity_id') | join(', ') }}) renders=2>
Error while processing template: Template<template=({{ state_attr('group.temperatures_koelkasten','entity_id') | join(', ') }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 568, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2198, 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/local/lib/python3.11/site-packages/jinja2/async_utils.py", line 45, in wrapper
    return normal_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/filters.py", line 571, in sync_do_join
    return str(d).join(map(str, value))
                       ^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable

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 694, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 570, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: 'NoneType' object is not iterable

Additional information

Its the first log statement directly after the standard warning for custom integrations. None of these custom integrations is using these groups. I have more groups defined. No errors for the others

home-assistant[bot] commented 9 months ago

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

Code owner commands Code owners of `group` 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 group` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


group documentation group source (message by IssueLinks)

erkr commented 9 months ago

Update: I got more templates errors, always related to state_attr().

It seems that state_attr slightly changed since 2023.9.x. It now can return none instead of empty strings or empty lists.

So I fixed that by adding or [], when a list should be returned:

{% set entities  = state_attr('group.myalerts','entity_id') or [] %} 

Or adding or '', when a string should be returned:

{% set entities  = state_attr('group.myalerts','entity_id') or '' %} 

Then I discovered a second issue that explains the errors for templates I don't have anymore! During startup 'older' versions of the templates in the cards where still evaluated. After a few restart these errors where gone.

erkr commented 9 months ago

Update: This is a more general problem with templates in UI cards. While editing these templates, the code is temporary invalid.

Since core 2023.9 this results in a huge pile of template errors that are bloating the log.

This biggest issue is that these errors continue to emerge up to hours after finishing the template. Even after a restart!

This is very confusion as you get errors for template code that is not there anymore!

I changed the title of this issue accordingly

SteffenM-DE commented 8 months ago

Hi,

I get errors from templates in the log but these templates are deletet already (I search the RAW dashboard code and all files textbase). After some restarts sometimes the errors a gone but sometimes the errors are still there for days.

Any idea or news?

Steffen

erkr commented 8 months ago

@SteffenM-DE Yes that is exactly the issue I raised here. For some reason the errors logged from the template integration get seriously delayed when there are many errors.

SteffenM-DE commented 8 months ago

@erkr I test many things, reboot full server, saving again ore something but I could not recognize patterns. Sometimes the log shows the error in a template while creating this part an never using it really. This stays in log for many reboots and suddenly it's gone.

erkr commented 8 months ago

I think there are two issues:

issue-triage-workflows[bot] commented 5 months 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.

erkr commented 5 months ago

Bump

geek78 commented 4 months ago

Hi same issue for me. Very old templates and errors are still there

issue-triage-workflows[bot] commented 1 month 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.

erkr commented 1 month ago

bump