I keep receiving errors on HA startup with this test example:
sensor:
- platform: template
sensors:
test_this_template:
value_template: >-
{% if is_state("input_boolean.test_boolean","on") -%}
{{ "red" }}
{%- else -%}
{{ "brown" }}
{%- endif %}
attribute_templates:
color_1: >-
{{ this.state + "_1" }}
color_2: >-
{{ this.attributes['color_1'] + "_2" }}
color_3: >-
{{ this.attributes.color_1 + "_3" }}
availability_template: >-
{{ true }}
These errors are observed only at HA startup; then the code seems to work properly, no errors are logged.
Log:
2022-05-10 16:55:26 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{% if this != none -%}
{%- if this.attributes['color_1'] != none -%}
{{ this.attributes['color_1'] + "_2" }}
{%- endif -%}
{%- endif %}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1859, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 926, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 3, in top-level template code
jinja2.exceptions.UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'color_1'
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 525, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 411, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'color_1'
2022-05-10 16:55:26 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{% if this.attributes.color_1 != none -%}
{{ this.attributes.color_1 + "_3" }}
{%- endif %}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1859, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 926, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 2, in top-level template code
jinja2.exceptions.UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'color_1'
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 525, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 411, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'color_1'
2022-05-10 16:55:26 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'color_1'') while processing template 'Template("{% if this != none -%}
{%- if this.attributes['color_1'] != none -%}
{{ this.attributes['color_1'] + "_2" }}
{%- endif -%}
{%- endif %}")' for attribute 'color_2' in entity 'sensor.test_this_template'
2022-05-10 16:55:26 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'color_1'') while processing template 'Template("{% if this.attributes.color_1 != none -%}
{{ this.attributes.color_1 + "_3" }}
{%- endif %}")' for attribute 'color_3' in entity 'sensor.test_this_template'
I corrected templates to intercept exceptions with no success:
color_2: >-
{% if this != none -%}
{%- if this.attributes['color_1'] != none -%}
{{ this.attributes['color_1'] + "_2" }}
{%- endif -%}
{%- endif %}
color_3: >-
{% if this.attributes.color_1 != none -%}
{{ this.attributes.color_1 + "_3" }}
{%- endif %}
Checks defined a bit differently for attributes for testing purpose.
I started experimenting with addressing attributes earlier, but did not check the log for presence of errors.