Some integrations always tend to be a bit slow with setting up during startup leading to templates being evaluated before entities used in that templates provide all their attributes.
In my case it’s mostly the knx integration that isn’t able to gather all states on startup before HA wants to evaluate some templates using cover positions for template binary sensors. Depending on how the sensors are defined, I get errors or warnings on startup.
For example:
binary_sensor:
- platform: template
sensors:
eg_entertainment_kinomodus:
friendly_name: EG Entertainment Kinomodus
value_template: "{{ not is_state('sensor.beamer_status','standby') and state_attr('cover.eg_wohnen','current_position') < 100 }}"
leads to an error like this on most startups:
2021-11-16 16:50:01 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ not is_state('sensor.beamer_status','standby') and state_attr('cover.eg_wohnen','current_position')|default(100) < 100 }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'NoneType' and 'int'
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 514, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: '<' not supported between instances of 'NoneType' and 'int'
2021-11-16 16:50:01 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: '<' not supported between instances of 'NoneType' and 'int'') while processing template 'Template("{{ not is_state('sensor.beamer_status','standby') and state_attr('cover.eg_wohnen','current_position')|default(100) < 100 }}")' for attribute '_state' in entity 'binary_sensor.eg_entertainment_kinomodus'
I also tried using a default filter for state_attr(), but that doesn’t prevent any errors:
binary_sensor:
- platform: template
sensors:
eg_entertainment_kinomodus:
friendly_name: EG Entertainment Kinomodus
value_template: "{{ not is_state('sensor.beamer_status','standby') and state_attr('cover.eg_wohnen','current_position')|default(100) < 100 }}"
Defining the binary sensor as a trigger based template instead doesn’t help much either:
template:
- trigger:
- platform: state
entity_id: sensor.beamer_status
- platform: numeric_state
entity_id: cover.eg_wohnen
attribute: current_position
below: 100
binary_sensor:
- name: EG Entertainment Kinomodus
state: "{{ not is_state('sensor.beamer_status','standby') and state_attr('cover.eg_wohnen','current_position')|default(100) < 100 }}"
This just “replaces” the error with a (not so scary looking… ) warning:
2021-11-16 17:11:41 WARNING (MainThread) [homeassistant.components.homeassistant.triggers.numeric_state] Error initializing 'Trigger Update Coordinator' trigger: In 'numeric_state' condition: attribute 'current_position' (of entity cover.eg_wohnen) does not exist
I really can’t blame the author of the knx integration for this behavior, as the integration is gathering states of all knx entities as fast as possible, i.e. flooding the knx bus with read requests on startup. But for the amount of knx entities in my house it’s unavoidable that this takes some time.
Is there any way to prevent these warnings?