Can anyone help me understand the correct way to refer to the state value of a sensor when setting an attribute? I have been using states('sensor.thing') as part of the attribute template but moved over to using this.state after reading posts to this forum. At least one of my instances is now returning the wrong value (as if this.state does not exist). As an example I’ve got this:
this.state == "Light OK" is never true.
Of course I can’t test this in Developer Tools as this.state doesn’t exist there. Can anyone help me improve my understanding please?
Didn’t mean to be rude. My phone didn’t show me you response before I replied. Sorry.
I must admit this is a pain because there are several instances where I do some complex processing in the state value which I reuse in setting attributes. I thought I was clever avoiding repeated code. Ho hum.
Thanks for your help.
Now I’m unclear again. If I’m setting an attribute of a trigger sensor, am I “in the attributes templates”? See the code I posted at the start for what I was doing.
So my original question stands: as you can see from the code I’m using this.state in setting an attrubute, but it is not returning the current value of the sensor. Am I right in thinking I can’t do any testing using this.state in dev tools?
I can’t see anything obviously wrong. You don’t have to put plain string results in templates, but it shouldn’t cause any issues either. Also if the + in the first state template {{+ 'Dark within etc... was supposed to be for concatenation, it isn’t doing anything.
- trigger:
- platform: time_pattern
minutes: "/5"
sensor:
- name: rnli_dclass
unique_id: rnli_dclass
state: >
{% if states('sensor.sun_solar_elevation')|float < -6 %}
ILB in darkness conditions
{% elif as_timestamp(states('sensor.sun_next_dusk'),0) < as_timestamp(now(),0)+2*3600 %}
{{ 'Dark within (h:m) ' ~ (as_timestamp(states('sensor.sun_next_dusk'),0) - as_timestamp(now(),0))|timestamp_custom("%H:%M",true)}}
{% else %}
Light OK
{% endif %}
attributes:
wind: >
{% if this.state == "Light OK" %}
{% if state_attr('sensor.wind_display','gusts')|float(0) > 21 %}
Wind over F5
{% else %}
Wind OK
{% endif %}
{% else %}
{% if state_attr('sensor.wind_display','gusts')|float(0) > 16 %}
Wind over F4
{% else %}
Wind OK
{% endif %}
{% endif %}
Thanks for your help again Tom.
I’ll tidy up as you suggest but as this.state isn’t working and in this instance the test I’m avoiding replicating is so simple, I’ll go with the suggestion of repeating states('sensor.sun_solar_elevation')|float < -6. I’ll look at my other instances of referring to the sensor’s value at some time & see if they are misbehaving.
Immediately after putting this ino configuration.yaml i get
Test
2023-10-11 16:51:00.186171+01:00 friendly_name: Test
copy_of_state: unknown
A few minutes later:
[sensor.test](http://192.168.0.87:8123/developer-tools/state#)
Test 2023-10-11 16:52:00.187728+01:00 friendly_name: Test copy_of_state: 2023-10-11 16:51:00.186171+01:00
So it does indeed return the previous state despite being in the attributes section…