Figuhr
February 18, 2023, 3:38pm
1
Hello,
I have an automation that starts a Sonos player among other things. Another automation responds to ‘play’ or ‘pause’ on the same device. But now this should not happen when ‘play’ happens because of an automation.
Is it possible to implement something like this? I had already thought of passing a variable to the second automation, which can be used there as an exception. I would not like to use an boolean helper.
WallyR
(Wally)
February 18, 2023, 4:23pm
2
You need a boolean helper.
Set it in the first to on and if the second see it as on, then set it to off and stop execution.
tom_l
February 18, 2023, 4:52pm
3
Use the context of the trigger then. This is not simple. You have to test for three things. Here’s an excellent explanation:
This nugget was very useful, thank you! With this I can now differentiate the 3 ways a device’s state was changed.
[Screenshot 2022-06-21 081430]
Here is a sample implementation using the choose action:
trigger:
- platform: state
entity_id:
- switch.office
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.to_state.context.id != none }}'
- condition: template
value_template: '{{ trigge…
Figuhr
February 19, 2023, 11:00am
4
I think that’s exactly it! I will test it. Thank you very much!
Figuhr
March 5, 2023, 9:58am
5
Tested today, thrilled today!
This approach is insane!
Just FYI, when a device is triggered by a third-party app (Alexa, Sonos), it is also interpreted as a “Physical Device”.
Could this automation be shortened with Jinja?
tom_l
March 5, 2023, 12:35pm
6
You can condense the conditions from this:
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.to_state.context.id != none }}'
- condition: template
value_template: '{{ trigger.to_state.context.parent_id == none }}'
- condition: template
value_template: '{{ trigger.to_state.context.user_id == none }}'
To this (template shorthand):
- choose:
- conditions:
- '{{ trigger.to_state.context.id != none }}'
- '{{ trigger.to_state.context.parent_id == none }}'
- '{{ trigger.to_state.context.user_id == none }}'
Or this:
- choose:
- conditions:
- '{{ trigger.to_state.context.id != none and trigger.to_state.context.parent_id == none and trigger.to_state.context.user_id == none }}'
Figuhr
March 5, 2023, 2:29pm
7
I like the middle version the best. Concise but clear.
That’s how I successfully implemented it (for whatever reason the formatting had to be different).
condition: template
value_template: |
- {{ trigger.to_state.context.id != none }}
- {{ trigger.to_state.context.parent_id == none }}
- {{ trigger.to_state.context.user_id != none }}
The result = Message: “Triggered by Physical Device.”
However, I do not get a result on the following (Triggered by HA user interface):
condition: template
value_template: |
- {{ trigger.to_state.context.id != none }}
- {{ trigger.to_state.context.parent_id == none }}
- {{ trigger.to_state.context.user_id != none }}
or (Triggered by HA automation):
condition: template
value_template: |
- {{ trigger.to_state.context.id != none }}
- {{ trigger.to_state.context.parent_id != none }}
- {{ trigger.to_state.context.user_id == none }}
tom_l
March 5, 2023, 3:28pm
8
Figuhr
March 6, 2023, 3:14pm
9
Of course you are right. Now it works perfectly.