Trigger-based binary sensor: icon is set in opposite way - BUG?

Anyone may have an answer?

It’s referencing the state in the state machine, which is the previous state. Use the trigger object instead.

Previous - because this is a trigger-based sensor?
Why “this.state” refers to the current state in non-trigger-based sensors, and here it refers to the previous state?
I remind you that this is an “icon” attribute which is supposed to be set dependingly on some template - and in my example it must be set dependingly on “this.state” - which is a CURRENT state.

Because trigger based templates resolve at the exact same moment, where non-trigger based ones resolve when they resolve.

Not getting your last post.
Can an attribute of a trigger-based sensor change independently on a defined trigger - i.e. a sensors state is resolved after a trigger, an attribute is resolved at any other time when it’s template is changed?
Or attributes & a state are only changed right after trigger?

Trigger based template entities resolve all fields (templates) at the moment the trigger occurs.

Non-trigger template entities have listeners for each field (template) and they each update independently. IIRC there’s a priority order as well that’s applied.

i.e. for all attributes too?imeans - attributes may not be updated on theor own ways asynchronously?

For all templates in the template entity.

I see. I need to review my trigger-based sensors.
Thank your for explanations.

So, using “this” in trigger-based sensors always refers to the previous state.
Then this must be described in Docs.

This always references the previous state, it just happens to work in non-trigger based template entities with attributes because the attributes resolve after the state does. I.e. if you use this.state in your state template, it’s going to be the previous state.

In my tests with non-trigger-based sensors, if I use this for an “icon” or any other attribute - it refers to the CURRENT state. At least these attributes are set properly.

Again, that’s only because templates resolve in the following order: availability → state → everything else. And because these each have their own listener, the state machine is updated during that process.

2 Likes

FWIW, this.state contains the previous state value and a common usage is to report a sensor’s previous state if its current state is not something you want (like undefined, non-numeric, zero, etc).

Examples


This would be useful somewhere in the documentation (I learned it empirically):

Just do a test real quick, you’ll see that I’m right. Put this.state inside the state: template (not an attribute template). It will always be the previous state.

Then probably this must be better explained in Docs here:


Will try to propose my corrections.

I’d have to look at the code again to give the real order. Off the top of my head, I know availability → state are the first 2. The order is buried in the code and I don’t have access to an IDE with the code at the moment.

Here I agree, noticed some day - explained it myself like “cannot refer to myself since not updated yet”.
But for attributes I believed that I can safely use “this”.
Now this is explained by that “availability → state → everything else” sequence.

As for trigger-based sensors - I was sure that only states are updated after trigger, and all attributes are updated like other template sensors…
OK, need to review some my sensors. Thanks a lot!

This is the only case when I use “this” in a state definition, like:

state: >-
  {% set VALUE_FROM_DEVICE = state('sensor.some_device') -%}
  {%- if VALUE_FROM_DEVICE in ['unavailable','unknown'] -%}
  {{ states(this.entity_id) }}
  {%- else -%}
  {{ VALUE_FROM_DEVICE }}
  {%- endif %}

Just like in the examples I posted.

1 Like