Timing of Actions in Automations

I have an automation which is triggered when someone presses my doorbell. It performs the following actions:

  1. Plays an mp3 chime on 3 Google Home speakers
  2. Plays an mp3 chime on a Logitech Squeezebox Boom media player
  3. Casts the doorbell video stream (from Blue Iris) to 2 Chromecast devices (this is done in the script referred to below)
  4. Changes the TV source to the Chromecast
  5. Changes the source on my Onkyo AV amplifier to the main Chromacast
  6. After a delay of a minute or so, stops casting the video streams to the Chromecasts.

My question is: will step 3 only be executed when the mp3 file has finished playing on the media players, or will it be executed as soon as the request has been sent to the media players? As the mp3 lasts for 5-10 seconds, I don’t want to delay execution of Step 3 for that long. But by the same token, Steps 1 & 2 are the highest priority as they are the primary alerting mechanism for my doorbell.

My automation is below:

alias: Doorbell Pressed
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.sage_doorbell_sensor
    to: 'on'
condition: []
action:
  - service: media_player.play_media
    data:
      media_content_id: http://www.mydomain.com/front-door.mp3
      media_content_type: audio/mp3
    target:
      entity_id:
        - media_player.lounge_speaker
        - media_player.study_speaker_3
        - media_player.kitchen_speaker
      device_id:
        - dcc47ea181329c3ae12855a8e26649b7
        - 435c3bbba27f8992f5e79355837b22d6
        - f6264fe7ca5404bf0767db98ddbde81a
  - service: media_player.play_media
    target:
      entity_id: media_player.bedroom
    data:
      media_content_id: http://www.mydomain.com/front-door.mp3
      media_content_type: music
  - service: script.1642515823804
  - service: switch.turn_on
    target:
      entity_id: switch.tv_hdmi2
  - service: switch.turn_on
    target:
      entity_id: switch.onkyo_game
  - delay:
      hours: 0
      minutes: 1
      seconds: 30
      milliseconds: 0
  - service: media_player.turn_off
    target:
      entity_id:
        - media_player.lounge_tv_2
mode: single

No, everything executes one after the other, without waiting

Like Tinkerer said, everything executes in order without waiting … except for this line:

  - service: script.1642515823804

When you call a script in that manner, all of the script’s actions must finish before execution proceeds to the next action in your automation (the service call to turn on switch.tv_hdmi2).

In contrast, if you call the script like this:

  - service: script.turn_on
    target:
      entity_id: script.1642515823804

it will not wait for the script to finish and will immediately proceed to execute the automation’s next action.

This behavior is documented here: Waiting for a script to complete

So if the mp3 chime lasts for 10 seconds, there will not be a 10 second delay before the next action is executed?

No.

Telling the media_player to play something involves no appreciable delay. It doesn’t matter if it takes 10 seconds or 10 minutes to play the tune, execution will proceed immediately to the next step.

Have you tried your automation yet? Because the behavior Tinkerer and I have described will be immediately apparent.

There’s not, though you can use the scripting language to delay until the chime is over if you want.

  - service: media_player.play_media
    target:
      entity_id: media_player.bedroom
    data:
      media_content_id: http://www.mydomain.com/front-door.mp3
      media_content_type: music
  - delay: '00:00:01' # Give the media player a chance to report that it's playing
  - wait_template: "{{ is_state('media_player.bedroom','idle') }}" # now wait for it to be idle
  - more actions go here

Good - thanks. The video stream is slow to load, so it was difficult to tell whether it was waiting for the media to finish playing before it was executed. I guess that it isn’t, so it’s happening as quickly as it can.