Do automation actions run in sequence or parallel by default?

Hello, I have a pretty basic automation that happens when my alarm is triggered.


      # Set max volume on 3 devices (nest minis)
      - action: media_player.volume_set
        metadata: {}
        data:
          volume_level: 1
        target:
          device_id:
            - 123
            - 234
            - 345

      # Play my burglar alarm mp3 on each device
      - action: media_player.play_media
        target:
          entity_id: media_player.123
        data:
          media_content_id: media-source://media_source/local/audio/burglar-alarm.mp3
          media_content_type: audio/mpeg
        metadata:
          title: burglar-alarm.mp3
          thumbnail: null
          media_class: music
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://media_source
            - media_content_type: ""
              media_content_id: media-source://media_source/local/audio

      - action: media_player.play_media
        target:
          entity_id: media_player.234
        data:
          media_content_id: media-source://media_source/local/audio/burglar-alarm.mp3
          media_content_type: audio/mpeg
        metadata:
          title: burglar-alarm.mp3
          thumbnail: null
          media_class: music
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://media_source
            - media_content_type: ""
              media_content_id: media-source://media_source/local/audio
      - action: media_player.play_media
        target:
          entity_id: media_player.345
        data:
          media_content_id: media-source://media_source/local/audio/burglar-alarm.mp3
          media_content_type: audio/mpeg
        metadata:
          title: burglar-alarm.mp3
          thumbnail: null
          media_class: music
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://media_source
            - media_content_type: ""
              media_content_id: media-source://media_source/local/audio

I have the actions in order of when I want them to happen so that the volume is maxed out first and then the MP3 plays.

What I notice happening every time is that the audio will play first and then the volume gets turned up. I’m not sure if it’s a weird nest quirk or if it’s something I can change in my automation. Has anyone else experienced this before? Any help would be greatly appreciated, thank you!

Hello shadow1349,

In that case, they are started in series, but don’t wait for it to complete because the speaker doesn’t feed that back, so add a delay between.

1 Like

As @Sir_Goodenough mentioned, they run in series, however it’s important to note that some actions, although processed in series, may have asynchronous pieces to them, and an action may return as “completed” before the end result is actually reflected.

I had that exact issue dealing with media players myself, trying to reset volumes after playing audio. I had assumed that when I told it to play audio, that immediately following that the state would be playing - but the call to play audio is asynchronous, so it would process the request and go on to the next step in my automation before audio had begun playing.

If you find it feels like it’s racing, put a condition check between your steps (you can use a wait template with a timeout) so it waits for whatever it needs before it continues to the next action.

3 Likes

BTW, I’ll put this script here just in case it’s of use to you…

alias: Play media
icon: mdi:speaker-wireless
fields:
  speaker:
    name: speaker
    selector:
      media: {}
    required: true
  volume:
    name: volume
    selector:
      number:
        min: 0
        max: 1
        step: 0.1
    default: 0.5
    required: false
sequence:
  - alias: Generate unique ID
    variables:
      scene_id: pm_{{ slugify(this.context.id) }}
  - alias: Ensure media player is ready
    if:
      - alias: If media player is not on
        condition: template
        value_template: "{{ is_state(speaker.entity_id, ['off', 'unavailable']) }}"
    then:
      - alias: Turn on media player
        action: media_player.turn_on
        target:
          entity_id: "{{ speaker.entity_id }}"
  - alias: Save media player state
    action: scene.create
    data:
      scene_id: "{{ scene_id }}"
      snapshot_entities: "{{ speaker.entity_id }}"
  - alias: Set volume
    action: media_player.volume_set
    target:
      entity_id: "{{ speaker.entity_id }}"
    data:
      volume_level: "{{ volume | default(0.5) }}"
    continue_on_error: true
  - alias: Play media
    action: media_player.play_media
    target:
      entity_id: "{{ speaker.entity_id }}"
    data:
      announce: true
      media_content_id: "{{ speaker.media_content_id }}"
      media_content_type: "{{ speaker.media_content_type }}"
    continue_on_error: true
  - alias: Wait for media player to finish playing media
    sequence:
      - alias: Wait for media to start playing
        wait_template: "{{ is_state(speaker.entity_id, 'playing') }}"
        continue_on_timeout: true
        timeout: "00:00:15"
      - alias: Wait for media to finish playing
        wait_template: "{{ is_state(speaker.entity_id, 'idle') }}"
        continue_on_timeout: true
        timeout: "00:00:30"
  - alias: Restore media player state
    action: scene.turn_on
    target:
      entity_id: scene.{{ scene_id }}
  - alias: Garbage collection
    action: scene.delete
    target:
      entity_id: scene.{{ scene_id }}
mode: parallel
1 Like

Ah ok, that makes sense. Thank you!