I am trying to create a state-based template sensor which makes use of the previous value. However, it seems this.state isn’t work at all. Consider this toy example:
{% if this is not none %}
{{ this.state }}
{% else %}
{{ states('sensor.temp_livingroom_raw') }}
{% endif %}
It renders “unavailable” and the log shows the following error:
2025-03-02 10:49:01.478 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('AttributeError: 'NoneType' object has no attribute 'lower'') while processing template 'Template<template=({% if this is not none %}
{{ this.state }}
{% else %}
{{ states('sensor.temp_livingroom_raw') }}
{% endif %}) renders=4>' for attribute '_attr_native_value' in entity 'None'
Also any other usages of this.state lead to similar errors, even if the template has already rendered a value previously.
Note, I am not using the template editor of the dev tools but the actual editor of an already existing template sensor.
the entity doesn’t exist because you haven’t created it yet, so you’re going to get errors. When you first create a template entity using this.state, you need to protect against it completely not existing.
e.g.
{% set entity_id = 'sensor.temp_livingroom_raw' %}
{% if entity_id | has_value %}
{{ states(entity_id) }}
{% else %}
{{ this.state if this is not none and this.state is defined else None }}
{% endif %}
Your code example works but I don’t understand the “the entity doesn’t exist” part. The entity does already exist because I have created with with {{ states('sensor.temp_livingroom_raw') }}intially. After that I tried to change the template to the one in my first post.
Anyway, what I actually do want is this:
{% set newValue = states('sensor.temp_livingroom_raw') %}
{% set oldValue = this.state if this is not none and this.state is defined else newValue %}
{% if newValue in ('unavailable', 'unknown') %}
{{ oldValue }}
{% elif oldValue in ('unavailable', 'unknown') %}
{{ newValue }}
{% elif ((newValue | float) - (oldValue | float)) | abs >= 0.4 %}
{{ oldValue }}
{% else %}
{{ newValue }}
{% endif %}
It uses the new value of the sensor unless the new value differs from the previous value (which is this.state) by more than 0.3. In this case it sticks with the previous value. However, this also leads to the same 'NoneType' object has no attribute 'lower' error (which given the template code is highly confusing since no lower is used anywhere).
Rationale behind this: I have a few temperature sensors which have a bug which lets their temperature jump by 0.4 degrees for about 30s and then it goes back to normal. I know about the built-in outlier filter but that doesn’t do what I want which is simply ignoring these 0.4 degree jumps.
That does not create the entity. That creates a variable that references state of an entity that’s grabbed from the state machine. If the state machine doesn’t have that entity, you will get unavailable as the stored result.
Your entity is created as soon as you click OK on this screen. And the entity will only be created if your template returns a valid result. (which yours does not).
You still have white sections there in that graph. So your template wasn’t correct for those points in time. If the template resolves an error, it typically removes it’s listeners and stops updating.
this.state will only work when the state exists in the state machine (temp_livingroom_test). Once that goes unavailable, your template needs to be able to adapt to that from this.state.