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