Ok, then write up an issue against the template integration.
Thanks, see this.state not usable in templates · Issue #139642 · home-assistant/core · GitHub for reference.
Why would the this
object (not the state, but the entire object) ever equal none
? What is the purpose of the template?
Are you wanting to use the previous state if the sensor is unavailable? You’d want this:
{% if has_value('sensor.temp_livingroom_raw') %}
{{ states('sensor.temp_livingroom_raw') }}
{% else %}
{{ this.state }}
{% endif %}
The examples provided are only toy example to demonstrate that something is seriously wrong with this.state
. I described my intent in a previous post.
The only thing wrong with this
this is the preview in the UI. If setup correctly with proper checks after the entity is generated, everything will work as expected. Meaning, if you create these in YAML, everything will work as expected.
There is definitely a bug with UI generated template entities using this
.
You created that all from the UI. Many many many people are using this.state
and it would be a wider bug if it didn’t work via yaml. It largely stems from the fact that the template doesn’t resolve properly and then attempts to reference itself without being able to because of the issue outlined above.
Sorry, I didn’t read your previous post properly.
So you are saying it’s not only an issue with the preview in the UI but creating a template using this.state
in the UI in general? If I created the template sensor via configuration.yaml
it should work?
Correct, it should work, assuming it can get past initial setup. Which may require you to have an if statement that resolves to a real value with something that will update it.
Again, the problem is because you access this.state
before the entity is instantiated in the system. I.e. Your code must go a different if statement path first during startup, and it can’t touch this.state
.
I tried
{% if now().minute < 54 %}
0
{% else %}
{% set oldValue = this.state | float %}
{% set newValue = states('sensor.temp_livingroom_raw') | float %}
{{ newValue - oldValue }}
{% endif %}
via the UI and it also didn’t work. Until 17:53 the sensor reported 0 and at 17:54 it turned unavailable again. So it seems this.state
really only works via YAML.
Did it create the entity or were you looking at the preview before you hit submit/ok? The preview is 100% broken with this.state because the entity does not exist at this time.
I have created the entity, i.e. hit OK, and then waited until the condition became false
. This is how its graph looks:
The blue lines are when the condition is
true
, i.e. the template returns 0. Once the condition becomes false
and this.state
is accessed it turns unavailable hence the white gaps.
And, are there errors in the logs? I’m trying to be helpful here but you keep omitting information every time you’re trying to prove your point. I manage the template integration, I need straight answers in order to replicate and fix these issues.
e.g. A proper template using this.state
that resolves to an answer will work.
This will be 0 every even minute and 2 every odd minute. All done via the UI and will never be unavailable.
{% if not now().minute % 2 %}
0
{% else %}
{{ this.state | int(0) + 1 }}
{% endif %}
This shows you that this.state does work via the UI as long as the entity exists and can update.
So from my standpoint the only issue is that the preview does not work when the entity does not exist and your code path hits this.state
.