Hoping someone can help me with an error that has started appearing on my Home Assistant logs please? I have a feeling looking at the history of the sensor that this has been caused by either my ESP device becoming unavailable momentarily for some reason (although it had no Wi-Fi drop) or the template sensor becoming unavailable, as I’ve never had this before and the device has been running for several months error free and hasn’t been updated. It looks like an ‘unavailable’ scenario that I just haven’t catered for with my template sensor, so can someone help explain what I would need to do to prevent these errors if the same scenario occurs again please, either with the template sensor or the device becoming unavailable?
The error is:
Logger: homeassistant.helpers.event
Source: helpers/template.py:460
First occurred: 14 March 2023 at 19:46:51 (4 occurrences)
Last logged: 14 March 2023 at 19:48:53
Error while processing template: Template("{% if states.sensor.oil_level.state | float(0) > 100 %} FULL {% elif states.sensor.oil_level.state | float(0) > 75 %} GOOD {% elif states.sensor.oil_level.state | float(0) > 50 %} OK {% elif states.sensor.oil_level.state | float(0) > 25 %} LOW {% elif states.sensor.oil_level.state | float(0) > 0 %} WARNING {% else %} ERROR {% endif %}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 458, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2007, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1771, in forgiving_float_filter
return float(value)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'state'
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 576, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 460, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'state'
and my template sensor configuration is:
- name: "Oil Status"
state: >
{% if states.sensor.oil_level.state | float(0) > 100 %}
FULL
{% elif states.sensor.oil_level.state | float(0) > 75 %}
GOOD
{% elif states.sensor.oil_level.state | float(0) > 50 %}
OK
{% elif states.sensor.oil_level.state | float(0) > 25 %}
LOW
{% elif states.sensor.oil_level.state | float(0) > 0 %}
WARNING
{% else %}
ERROR
{% endif %}