Do not start when another automation ends

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.

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.

Use the context of the trigger then. This is not simple. You have to test for three things. Here’s an excellent explanation:

I think that’s exactly it! I will test it. Thank you very much!

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?

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 }}'

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 }}

That template condition is not correct. Mine is. See https://www.home-assistant.io/docs/scripts/conditions/#template-condition-shorthand-notation

:man_facepalming:

Of course you are right. Now it works perfectly.