core: Error while editing templates in lovelace cards

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

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 19

Most upvoted comments

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.

bump

I think there are two issues:

  • The card either evaluates the template to frequent during editing, or one can argue that template errors shouldn’t be logged at all while editing a card (like the template tab in dev tools)
  • when the template integration chokes in many errors, the time stamp of these errors seem wrong, causing errors to get logged as new errors long after they occurred (long after the template code is changed and even after a restart) The last issue is really confusing.

@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.