TemplateError: UndefinedError: 'None' has no attribute 'state' - help please

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 %}

add the availability option:

availability: >
          {{ states('sensor.oil_level') not in ['unknown', 'unavailable', 'None'] }}

Thanks @CChris ! So now with the availability option added to my template sensor, the template sensor will only be available providing it’s state isn’t in either ‘unknown’, ‘unavailable’ or ‘None’. if it is in one of these state, the template sensor becomes unavailable, rather than generating an error by being in an undefined state. Hopefully I’ve understood that correctly? I can make the actual device go offline, but when I do this, the sensor shows a state of 0.0. Is that the expected result, or should I be seeing ‘unavailable’ in this scenario, since adding the availability option to the template sensor?

Sorry, I didn’t get everything…

I can make the actual device go offline, but when I do this, the sensor shows a state of 0.0. Is that the expected result, or should I be seeing ‘unavailable’ in this scenario,

Do you mean the source sensor is showing 0.0 ?
Or your template?

If the Source is showing 0.0 - then your sensor will also show this information, since 0 is a valid value.

If your source sensor bekomes Unavailable, Unknown or “none” - then your template should also become unavailable.
This is to prevent unexpected behaviour if you are using the templates for long term statistics and your source becomes unavailable.

In this case, it could be, that the template was returning 0 - and messed up your statistics.

So if I manually set the state of my source sensor to ‘None’ using the ‘set state’ function in developer tools,

image

I can see that the state of my template sensor does indeed change to ‘unavailable’

image

During previous testing, It was the source sensor that was showing 0.0, but like you say, as this is a valid value, I was seeing a valid value for my template sensor - which I have defined as ‘ERROR’ in my template, which confused things a little :slight_smile:

I was also typing in ‘none’ when setting the state of the source sensor, instead of ‘None’, which was causing a valid template sensor state of ‘ERROR’ to come back. Typing in ‘None’ results in the correct template sensor state of ‘unavailable’ now so I’m happy this is now working as expected. Thanks again for your help :slight_smile: