I sometimes get errors in the logs, that I have no idea where are from. Many of them are from templates.
Quite often, I spend a long time trying to find out where that template is used. Either completely manually, or by mounting the config directory and searching in all files, which doesn’t always really help.
It would be great if a log like this:
Logger: homeassistant.helpers.event
Source: helpers/template.py:605
First occurred: 11:37:44 (13 occurrences)
Last logged: 14:15:22
Error while processing template: Template<template=({{ (states('sensor.home_power_consumption') | float / 1000 * states('sensor.electricity_price_total') | float) | round(2) }}) renders=2>
Error while processing template: Template<template=({% if is_state('sensor.oven_remaining_program_time') != 'unavailable' %} {{ states('sensor.oven_program_progress') | float / 60 }} minutes remaining {% elif is_state('sensor.oven_program_progress') != 'unavailable' %} {{ states('sensor.oven_program_progress') }}% complete {% else %} No duration info {% endif%}) renders=2>
Error while processing template: Template<template=({% if is_state('sensor.oven_remaining_program_time') != 'unavailable' %} {{ states('sensor.oven_program_progress') | float / 60 }} minutes remaining {% elif is_state('sensor.oven_program_progress') != 'unavailable' %} {{ states('sensor.oven_program_progress') }}% complete {% else %} No duration info {% endif %}) renders=2>
Error while processing template: Template<template=({% if is_state('sensor.oven_remaining_program_time') != 'unavailable' %} {{ states('sensor.oven_program_progress') | float / 60 }} minutes remaining {% elif is_state('sensor.oven_program_progress') != 'unavailable' %} {{ states('sensor.oven_program_progress') }}% complete {% else %} No duration info {% endif %}) renders=6>
Error while processing template: Template<template=({% if is_state('sensor.oven_remaining_program_time') != 'unavailable' %} {{ states('sensor.oven_program_progress') | float / 60 }} minutes remaining {% elif is_state('sensor.oven_program_progress') != 'unavailable' %} {{ states('sensor.oven_program_progress') }}% complete {% else %} No duration info {% endif %}) renders=10>
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 603, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2616, in _render_with_context
return template.render(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, 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/sandbox.py", line 327, in getattr
value = getattr(obj, attribute)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2648, in _fail_with_undefined_error
return super()._fail_with_undefined_error(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 "/usr/src/homeassistant/homeassistant/helpers/template.py", line 715, in async_render_to_info
render_info._result = self.async_render( # noqa: SLF001
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 605, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: None has no element 0
would show where the problematic code is. For instance, for the first one
(states('sensor.home_power_consumption') | float / 1000 * states('sensor.electricity_price_total') | float) | round(2)
I found the only mention of this in a file (which is not the log file) to be this in core.config_entries:
{
"data": {},
"disabled_by": null,
"domain": "template",
"entry_id": "41243d66de11bb465d2435e8b3fe9db0",
"minor_version": 1,
"options": {
"template_type": "sensor",
"name": "Current Electricity Cost per Hour",
"state": "{{ (states('sensor.home_power_consumption') | float / 1000 * states('sensor.electricity_price_total') | float) | round(2) }}",
"unit_of_measurement": "DKK/h",
"state_class": "measurement"
},
which doesn’t really tell me where to look for it. By chance, I finally found it hidden away somewhere in a card in the dashboard. But it sometimes takes a long time to find this.
Wouldn’t it be possible to have the log error state that it is e.g., on a specific dashboard or view and even card number, or give the entity_id
of the template sensor/helper?