I get an error for a template sensor: “Error while processing template”, but at at startup only. How to avoid the error at startup?
The code is executed fine in the Dev tools / template editor.
The detailed error mentions as_timestamp() - got invalid input ‘unknown’ - as the origin.
The sensor definition:
- sensor:
- name: "Unavailable Zigbee devices info"
unique_id: "ff0fb012-8a84-452d-88d0-9c9a4a6a7e03"
icon: mdi:counter
availability: "true"
unit_of_measurement: "cm"
state: 0
attributes:
unavailable_device_names: >-
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %}
{% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id))) > ((24 | int) * 60 * 60))) %}
{% set result.sensors = result.sensors + ['- ' + state.name | regex_replace(find=' Last seen', replace='')] %}
{% endif %}
{% endfor %}
{{ result.sensors | join('\n') | truncate(254, True) }}
device_list_length: >-
{%- set hash_value=state_attr('sensor.unavailable_zigbee_devices_info', 'unavailable_device_names') | join('\n') | length %}
{{ hash_value }}
The log file entry:
Logger: homeassistant.helpers.event
Source: helpers/template.py:646
First occurred: 19:46:18 (2 occurrences)
Last logged: 19:46:18
Error while processing template: Template<template=({% set result = namespace(sensors=[]) %} {% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %} {% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id))) > ((24 | int) * 60 * 60))) %} {% set result.sensors = result.sensors + ['- ' + state.name | regex_replace(find=' Last seen', replace='')] %} {% endif %} {% endfor %} {{ result.sensors | join('\n') | truncate(254, True) }}) renders=2>
Error while processing template: Template<template=({%- set hash_value=state_attr('sensor.unavailable_zigbee_devices_info', 'unavailable_device_names') | join('\n') | length %} {{ hash_value }}) renders=2>
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2206, in forgiving_as_timestamp
return dt_util.as_timestamp(value)
~~~~~~~~~~~~~~~~~~~~^^^^^^^
File "/usr/src/homeassistant/homeassistant/util/dt.py", line 155, in as_timestamp
raise ValueError("not a valid date/time.")
ValueError: not a valid date/time.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 644, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2969, in _render_with_context
return template.render(**kwargs)
~~~~~~~~~~~~~~~^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
self.environment.handle_exception()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 2, in top-level template code
File "/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py", line 401, in call
return __context.call(__obj, *args, **kwargs)
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2209, in forgiving_as_timestamp
raise_no_default("as_timestamp", value)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1972, in raise_no_default
raise ValueError(
...<2 lines>...
)
ValueError: Template error: as_timestamp got invalid input 'unknown' when rendering template '{% set result = namespace(sensors=[]) %} {% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %}
{% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id))) > ((24 | int) * 60 * 60))) %}
{% set result.sensors = result.sensors + ['- ' + state.name | regex_replace(find=' Last seen', replace='')] %}
{% endif %}
{% endfor %} {{ result.sensors | join('\n') | truncate(254, True) }}' but no default was specified
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 761, in async_render_to_info
render_info._result = self.async_render( # noqa: SLF001
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
variables, strict=strict, log_fn=log_fn, **kwargs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 646, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: as_timestamp got invalid input 'unknown' when rendering template '{% set result = namespace(sensors=[]) %} {% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %}
{% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id))) > ((24 | int) * 60 * 60))) %}
{% set result.sensors = result.sensors + ['- ' + state.name | regex_replace(find=' Last seen', replace='')] %}
{% endif %}
{% endfor %} {{ result.sensors | join('\n') | truncate(254, True) }}' but no default was specified