Template crashing with extensive but meaningless error

Hello,

I recently upgraded to the 2025.1 release from one over a year older, hoping that Jinja error messages had improved. But, at least in this instance, they haven’t, and I was hoping if anyone has some wisdom about how to get started.

Usually, I would handle errors of this sort by focusing on a recent change I made, and could usually figure out the problem by inspection or use of the Template editor. The problem with the latter approach is that for templates written to react to triggers and other stateful things, setting up the context in the Template editor is a pain that can be error-prone.

In this case, I can’t correlate the errors with a recent change, or even with similarly timed event triggers. And the error message doesn’t appear to actually reference any of my own code.

Does anyone have any suggestions about what type of runtime condition might trigger something like this? I’m in the process of shutting down parts of my YAML code to isolate the source of the problem, but I figured at minimum if there is some wisdom here I’d like to know it for future reference.

The error from the log is below. Thanks to anyone who tries to figure this out.

2025-01-21 11:54:00.285 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 643, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 2745, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/srv/hass/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 3, in top-level template code
  File "/srv/hass/lib/python3.13/site-packages/jinja2/sandbox.py", line 293, in getitem
    return obj[argument]
           ~~~^^^^^^^^^^
jinja2.exceptions.UndefinedError: None has no element 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/components/template/coordinator.py", line 115, in _handle_triggered_with_script
    if script_result := await self._script.async_run(run_variables, script_context):
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 1801, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 464, in async_run
    await self._async_step(log_exceptions=False)
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 528, in _async_step
    self._handle_exception(
    ~~~~~~~~~~~~~~~~~~~~~~^
        ex, continue_on_error, self._log_exceptions or log_exceptions
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 558, in _handle_exception
    raise exception
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 526, in _async_step
    await getattr(self, handler)()
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 1209, in _async_variables_step
    self._variables = self._action[CONF_VARIABLES].async_render(
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self._hass, self._variables, render_as_defaults=False
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script_variables.py", line 62, in async_render
    rendered_variables[key] = template.render_complex(
                              ~~~~~~~~~~~~~~~~~~~~~~~^
        value, rendered_variables, limited
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 266, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 645, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: None has no element 0
2025-01-21 11:54:00.288 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 643, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 2745, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/srv/hass/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 3, in top-level template code
  File "/srv/hass/lib/python3.13/site-packages/jinja2/sandbox.py", line 293, in getitem
    return obj[argument]
           ~~~^^^^^^^^^^
jinja2.exceptions.UndefinedError: None has no element 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/components/template/coordinator.py", line 115, in _handle_triggered_with_script
    if script_result := await self._script.async_run(run_variables, script_context):
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 1801, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 464, in async_run
    await self._async_step(log_exceptions=False)
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 528, in _async_step
    self._handle_exception(
    ~~~~~~~~~~~~~~~~~~~~~~^
        ex, continue_on_error, self._log_exceptions or log_exceptions
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 558, in _handle_exception
    raise exception
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 526, in _async_step
    await getattr(self, handler)()
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script.py", line 1209, in _async_variables_step
    self._variables = self._action[CONF_VARIABLES].async_render(
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self._hass, self._variables, render_as_defaults=False
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/script_variables.py", line 62, in async_render
    rendered_variables[key] = template.render_complex(
                              ~~~~~~~~~~~~~~~~~~~~~~~^
        value, rendered_variables, limited
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 266, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/hass/lib/python3.13/site-packages/homeassistant/helpers/template.py", line 645, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: None has no element 0

Hello dominic,

Well you didn’t share any code, but look where you are referencing a list called ‘None’.

How to help us help you - or How to ask a good question.

Thanks for your response.

I can’t post code because … believe me, you don’t want to look at 168 YAML files:

(hass) hass@hapi:~/.homeassistant$ find DT_yaml/ -iname \*.yaml  | wc -l
168

I suspect that this isn’t about a list literally called None, since I would never use that as a label for a variable. It feels like what’s happening is that somewhere, there is a derefernce of element 0 of a variable which doesn’t actually contain a list but is of type none. If you think I’m wrong, though, I would like to hear your thinking since I’d really like to chase this down.

Thanks.

Somewhere, something expected to receive a non-empty list but got a null object (none). It was going to get the zeroth element from the list but it can’t do anything when given nothing.

Thanks. That was my theory, too. Are you aware of any way to figure out where, though? The big problem here for me is to figure out where to look :frowning:

The error is about that. It was told to look for element 0 of the variable called None.

You don’t have to share your YAML, that’s fine.

I’m afraid there isn’t much more we can do for you then however.

Look for where you have typed the word None with a capitol letter and start there…

Yeah. I really wasn’t trying to get anyone to debug things for me. I’m just trying to figure out where to look.

As noted by another poster above, I really don’t think this is about a variable called None. That would have made it really easy for me to find–I wish.

As far as I can tell, the error message provides no clue regarding what exactly is responsible for causing the failure.

Finding the source will be laborious; search through every template you’ve created.

Thanks. In the process. Good-ol’ “printf debugging” :rofl:

I see “script” mentioned in that long-winded error message so you might want to start with your scripts first. Maybe it’s a template in a variable you defined within a script.

I’d be willing to bet this is related to the attribute change.

Attributes that didn’t exist before are now defined, therefore aren’t getting filtered. And then you’re assuming it’s a list, when it’s none.

That’s a very astute theory. I do use attributes quite a bit. For the most part, I know to generate a blank list or dictionary when the attribute is undefined, but somewhere–SOMEWHERE :face_with_symbols_over_mouth:–I must have forgotten.

When I figure it out, I’ll post it here for everyone’s amusement.

No no, you are miss understanding what I’m saying.

There was a change a while back, where attributes that were formerly missing when not in use are now present but set to None.

I.e. using is defined previously would return false, now returns true.

This was a change that occurred outside templates but affected templates.

1 Like

Ohhh. Wow, thank you. So it is absolutely no surprise that this started happening when I upgraded from 2023.12 to 2025.1 – I assume the change happened at some point during that window.

That really helps me focus my search. Thanks petro.

A very wide window. :wink:

Indeed. Every time I’ve lapsed in regular updates, I’ve regretted it for one reason or another.

I’ve tracked this down. First, for anyone who’s curious, I’ll explain it (@petro was spot-on). Second, a word of caution–there is something very strange with the errors were reported, beyond their impermeability.

The problem: yes, my code was dereferencing a value directly obtained from state_attr without checking it. At the time I wrote it, it seemed safe because these were attributes that had always been provided by my thermostat integration, but that changed when I updated HA to a new version. Moral of the story: it’s never safe to assume.

The bigger problem: the Python errors were not spit out contemporaneously with the code that failed. They weren’t delivered until the start of the next clock minute (i.e., the next time seconds were at 00 or 01). So in addition to the errors not being very useful in identifying the offending code, they also thwarted my attempt to isolate the problem by focusing on the Logbook and the log, and my own debug messages that I was adding to the code. Because it seemed reasonable when I set out to find this that the error would happen at the time of the crash. I guess, again, never safe to assume.

(Worse still, having observed that the errors primarily happened right at the start of a clock minute, I started my search by focusing on time_pattern triggers. And that was a waste of time.)

Could this delay in error reporting be a bug?

Thanks to all who tried to help. I appreciate it.

Was it in a script?

Or automation or Template Sensor or what exactly?

It was in the action block of a trigger-based template.

Here’s the offending code:

 {% if 'temp' is not in cur_max_reading.keys() or trigger.event.data.message > cur_max_reading['temp'] %}
     {{ 
         dict ({
                 'area'              : area,
                 'temp'              : trigger.event.data.message,
                 'weather'           : state_attr("weather.thermostat", "forecast")[0]['condition'],
                 'hvac_state'        : states('climate.thermostat'),
                 'hvac_target_temp'  : state_attr('climate.thermostat', 'temperature'),
                 'outdoor_temp'      : state_attr("weather.thermostat", "forecast")[0]['temperature'],
                 'timestring'        : invoke_timestring
               })
     }}

 {% else %}
   {{ none }}
 {% endif %}

The weather.thermostat entity provided by Ecobee no longer has a “forecast” attribute.

Interestingly enough, when I run this test case (in a contrived automation), I get a much friendlier error.

The test case:

- event:        TestCaseEvent
  event_data:
    weather           : " {{ state_attr('weather.thermostat', 'forecast')[0]['condition'] }}"
    hvac_state        : " {{ states('climate.thermostat') }} "
    hvac_target_temp  : " {{ state_attr('climate.thermostat', 'temperature') }} "
    outdoor_temp      : " {{ state_attr('weather.thermostat', 'forecast')[0]['temperature'] }} "

And the error message in the log:

2025-01-21 15:22:00.780 ERROR (MainThread) [homeassistant.components.automation.dt_ltscene_update_scenes_2] DT-ltscene_update-scenes: Error rendering event data template: UndefinedError: None has no element 0

Here, the error message identifies the automation by its entity_id, which makes it infinitely more helpful.

I suspect that the problem is that the offending code at the top is within a dict() call, which is probably handled in large part by Jinja’s Python subsystem and at that layer of code might lack the context.

To the extent improvements can be made here to identify offending code in some way, shape, or form, even if it’s just a filename/line number corresponding to the first line of the template in a YAML file, it would be so helpful.

And one more clarification: @petro’s comment helped focus me on state_attr calls, and got me to the right place, but this issue is actually distinct, I think. I expect that the same thing would have happened before the attribute change he noted, since I was dereferencing something that didn’t exist.

I think petro may have been referring to this situation:

Some entity attributes used to wink in and out of existence. For example, a light entity’s brightness attribute existed only when it was on. When off the attribute didn’t exist. This was changed so that now the brightness attribute always exists.

The state_attr function has always returned a null object (none) if the supplied entity_id and/or attribute name didn’t exist.

1 Like