This.state in template sensor not working at all

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.

You should post your whole code

1 Like

This is the whole code.

What do you want to achieve with this? And where did you got this example? Does not seem correct for me.

This is a toy example that only demonstrates that something seems to be wrong with this.state.

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.

Or asked differently, what is wrong with this template:

{{ this.state if this is not none and this.state is defined and this.state is not none else 0 }}

I would expect it to either render 0 or the previous value. But again it causes the same weird error message instead.

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).

I did click OK and hence the entity was created with a valid value:

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.

The white sections were phases where I tried to use this.state but it didn’t work.
What about my previous question/example?

That should always be zero. And it has nothing to update itself.

Correct. But it isn’t instead it causes the same error as the others. Could there be some bug in the templating logic? Did you try it yourself?


grafik

submit and let the entity get created, there’s oddness with the preview when using this. Again, the entity does not exist at that point.

Nope, doesn’t work.

is there a different error in your logs now, after you clicked ok? That’s when it will resolve the template. That and at restart.

grafik

reload template entities

Same error after reloading all template entities. I also tried it in a different HA installation, same error there.