What do I need to know about context
? The documentation I can find (user, developer) is not exactly exhaustive. Is there some other documentation somewhere, or a thread here that explains its mysteries? (So far only found a single sorta relevant thread through search.)
Reason I am asking is that I am working on a blueprint/automation that can trigger on several attribute changes of a media_player
. Here’s the short of it:
- trigger: state
entity_id: !input media_player
attribute: media_title
- trigger: state
entity_id: !input media_player
attribute: media_artist
- trigger: state
entity_id: !input media_player
attribute: media_duration
These may or may not change simultaneously, which unfortunately means that up to three triggers may fire at the same time. For reasons my automation is and has to be in mode: restart
.
So I started inspecting the trigger
variable in traces. Turns out that for these simultaneous attribute updates, trigger.from_state
and trigger.to_state
were exactly identical – all the way down to the values of last_*
and context
. My understanding of context
is limited, but my interpretation is that this means they were in fact triggered by one and the same update to this media player’s state object.
Looking at the corresponding values of these automation runs’ respective value for this
, those all differ. Not a single value between them are the same.
However upon looking really closely, I eventually noticed something interesting: Sometimes this.context.parent_id
was the same as trigger.to_state.context.id
, and sometimes it differs. Looking at cases where all three attributes were triggered simultaneously, on two of them these context values were a match, and on one they were not.
So my conclusion is that to prevent the same state object from triggering multiple simultaneous runs of my automation, I should add this condition:
- condition: "{{ this.context.parent_id != trigger.to_state.context.id }}"
But I don’t really know why. And I want to know! How does this.context.parent_id
work? Why does its value match trigger.to_state.context.id
on two of these simultaneous automation runs, but not on the third? Is the intention actually this specific use-case?!