Notification one by one from different automations at the same time (Sonos)

I have several individual automations that are triggered e.g. at 4PM to play a notification, i.e. it can happen that several notifications are played on a media player at the same time.

But this causes problems because it leads to e.g. two different notifications being played at the same time.

My wish would be that in such a case the notifications are played in sequence one by one.

As an example I share two automations and a script, I use Sonos speakers and media_player.play_media

Automation 1 (trigger at 4PM over time pattern):

alias: Benachrichtigung Luftbefeuchter
description: ""
trigger:
  - platform: time_pattern
    minutes: /30
    id: Ausgabe der Benachrichtigung
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.wassertank_luftbefeuchter_senor
        state: "on"
        enabled: true
      - condition: state
        entity_id: group.jemand_ist_zuhause
        state: home
action:
  - condition: time
    after: "16:00:00"
    before: "19:00:00"
  - repeat:
      sequence:
        - parallel:
            - if:
                - condition: state
                  state: "on"
                  for:
                    hours: 0
                    minutes: 0
                    seconds: 10
                  entity_id: binary_sensor.everything_presence_one_d5345c_occupancy
              then:
                - service: script.skript_benachrichtigung_wassertank_leer_wohnzimmer
                  metadata: {}
                  data: {}
              enabled: true
            - if:
                - condition: not
                  conditions:
                    - condition: state
                      entity_id: light.badezimmer_licht
                      state: "off"
                      for:
                        hours: 0
                        minutes: 0
                        seconds: 0
              then:
                - service: script.skript_benachrichtigung_wassertank_leer_badezimmer
                  metadata: {}
                  data: {}
            - if:
                - condition: or
                  conditions:
                    - condition: state
                      entity_id: binary_sensor.ep_light_occupancy
                      state: "on"
                      for:
                        hours: 0
                        minutes: 0
                        seconds: 10
                    - condition: state
                      entity_id: group.jemand_ist_im_bett
                      state: "on"
                      for:
                        hours: 0
                        minutes: 0
                        seconds: 10
              then:
                - service: script.skript_benachrichtigung_wassertank_leer_schlafzimmer
                  metadata: {}
                  data: {}
        - delay:
            hours: 0
            minutes: 30
            seconds: 0
            milliseconds: 0
      until:
        - condition: or
          conditions:
            - condition: template
              value_template: "{{ state_attr('fan.luftbefeuchter', 'water_tank_detached') }}"
            - condition: template
              value_template: "{{ not state_attr('fan.luftbefeuchter', 'no_water') }}"
mode: restart

Automation 2 (trigger at 4PM):

alias: Benachrichtigung Blumen gießen
description: ""
trigger:
  - platform: time
    at: "16:00:00"
    id: Orchidee gießen
  - device_id: f77497f31d0501cc8097ef052e6efd29
    domain: zha
    platform: device
    type: remote_button_long_press
    subtype: remote_button_long_press
    id: Bestätigen
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - Orchidee gießen
              - condition: time
                weekday:
                  - mon
        sequence:
          - repeat:
              sequence:
                - parallel:
                    - if:
                        - condition: state
                          state: "on"
                          for:
                            hours: 0
                            minutes: 0
                            seconds: 10
                          entity_id: >-
                            binary_sensor.everything_presence_one_d5345c_occupancy
                      then:
                        - service: script.skript_benachrichtigung_orchidee_wohnzimmer_2
                          metadata: {}
                          data: {}
                      enabled: true
                    - if:
                        - condition: not
                          conditions:
                            - condition: state
                              entity_id: light.badezimmer_licht
                              state: "off"
                              for:
                                hours: 0
                                minutes: 0
                                seconds: 0
                      then:
                        - service: script.skript_benachrichtigung_orchidee_badezimmer
                          metadata: {}
                          data: {}
                    - if:
                        - condition: or
                          conditions:
                            - condition: state
                              entity_id: binary_sensor.ep_light_occupancy
                              state: "on"
                              for:
                                hours: 0
                                minutes: 0
                                seconds: 10
                            - condition: state
                              entity_id: group.jemand_ist_im_bett
                              state: "on"
                              for:
                                hours: 0
                                minutes: 0
                                seconds: 10
                      then:
                        - service: script.skript_benachrichtigung_orchidee_schlafzimmer
                          metadata: {}
                          data: {}
                - delay:
                    hours: 0
                    minutes: 30
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: trigger
                  id:
                    - Bestätigen
      - conditions:
          - condition: trigger
            id:
              - Bestätigen
        sequence:
          - parallel:
              - if:
                  - condition: state
                    state: "on"
                    for:
                      hours: 0
                      minutes: 0
                      seconds: 10
                    entity_id: binary_sensor.everything_presence_one_d5345c_occupancy
                then:
                  - service: script.alexa_benachrichtigung_restore_lautstarke
                    data_template:
                      alexa_device: media_player.sascha_s_sonos_beam
                      sonos_device: media_player.soundbar
                      alexa_service: notify.alexa_media_sascha_s_sonos_beam
                      alexa_volume_speak: 0.4
                      alexa_message: >-
                        <audio
                        src="soundbank://soundlibrary/alarms/beeps_and_bloops/bell_02"/>Vielen
                        Dank!"
                enabled: true
              - if:
                  - condition: not
                    conditions:
                      - condition: state
                        entity_id: light.badezimmer_licht
                        state: "off"
                        for:
                          hours: 0
                          minutes: 0
                          seconds: 0
                then:
                  - service: script.alexa_benachrichtigung_restore_lautstarke
                    data_template:
                      alexa_device: media_player.echo_dot_badezimmer
                      alexa_service: notify.alexa_media_echo_dot_badezimmer
                      sonos_device: media_player.soundbar
                      alexa_volume_speak: 0.4
                      alexa_message: >-
                        <audio
                        src="soundbank://soundlibrary/alarms/beeps_and_bloops/bell_02"/>Vielen
                        Dank!"
              - if:
                  - condition: or
                    conditions:
                      - condition: state
                        entity_id: binary_sensor.ep_light_occupancy
                        state: "on"
                        for:
                          hours: 0
                          minutes: 0
                          seconds: 10
                      - condition: state
                        entity_id: group.jemand_ist_im_bett
                        state: "on"
                        for:
                          hours: 0
                          minutes: 0
                          seconds: 10
                then:
                  - service: script.alexa_benachrichtigung_restore_lautstarke
                    data_template:
                      alexa_device: media_player.saschas_echo_dot
                      alexa_service: notify.alexa_media_saschas_echo_dot
                      sonos_device: media_player.soundbar
                      alexa_volume_speak: 0.4
                      alexa_message: >-
                        <audio
                        src="soundbank://soundlibrary/alarms/beeps_and_bloops/bell_02"/>Vielen
                        Dank!"
mode: restart

And here a script:

alias: Skript Benachrichtigung Orchidee Schlafzimmer
sequence:
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://media_source/local/528475__robinhood76__09156-accent-chime-login.wav
      media_content_type: audio/mpeg
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://tts/cloud?message=%E5%85%B0%E8%8A%B1%E5%A4%AA%E5%B9%B2%E9%9C%80%E8%A6%81%E6%B5%87%E6%B0%B4%E3%80%82&language=zh-HK&voice=WanLungNeural
      media_content_type: music
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://tts/cloud?message=Die+Orchidee+ist+zu+trocken%2C+bitte+gie%C3%9Fen.&language=de-DE&voice=KatjaNeural
      media_content_type: music
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://media_source/local/528475__robinhood76__09156-accent-chime-login.wav
      media_content_type: audio/mpeg
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
mode: single
icon: mdi:account-voice

Maybe someone has a solution how the notifications can be played in sequence in case two notifications are triggered at the same time from different automations.

Many thanks

1 Like

scripts can be set to run sequentially.

set

mode: queued
max: 20

max specifies the maximum number of requests to be queued.

i tried right now but still the problem, HA tries play both scripts at the same time:

alias: Test 1
description: ""
trigger:
  - platform: time
    at: "18:48:00"
condition: []
action:
  - service: script.skript_benachrichtigung_orchidee_schlafzimmer
    metadata: {}
    data: {}
mode: single
alias: Test 2
description: ""
trigger:
  - platform: time
    at: "18:48:00"
condition: []
action:
  - service: script.skript_benachrichtigung_waschmaschine_schlafzimmer
    metadata: {}
    data: {}
mode: single

The script:

alias: Skript Benachrichtigung Orchidee Schlafzimmer
sequence:
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://media_source/local/528475__robinhood76__09156-accent-chime-login.wav
      media_content_type: audio/mpeg
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://tts/cloud?message=%E5%85%B0%E8%8A%B1%E5%A4%AA%E5%B9%B2%E9%9C%80%E8%A6%81%E6%B5%87%E6%B0%B4%E3%80%82&language=zh-HK&voice=WanLungNeural
      media_content_type: music
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://tts/cloud?message=Die+Orchidee+ist+zu+trocken%2C+bitte+gie%C3%9Fen.&language=de-DE&voice=KatjaNeural
      media_content_type: music
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://media_source/local/528475__robinhood76__09156-accent-chime-login.wav
      media_content_type: audio/mpeg
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
mode: queued
icon: mdi:account-voice
max: 20
alias: Skript Benachrichtigung Waschmaschine Schlafzimmer
sequence:
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://media_source/local/528475__robinhood76__09156-accent-chime-login.wav
      media_content_type: audio/mpeg
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://tts/cloud?message=%E6%B4%97%E8%A1%A3%E6%9C%BA%E5%B7%A5%E4%BD%9C%E5%B7%B2%E5%AE%8C%E6%88%90%EF%BC%8C%E8%AF%B7%E6%8A%8A%E8%A1%A3%E7%89%A9%E5%8F%96%E5%87%BA%E6%9D%A5%E3%80%82&language=zh-HK&voice=WanLungNeural
      media_content_type: music
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 4
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://tts/cloud?message=Die+Waschmaschine+ist+fertig%2C+bitte+die+W%C3%A4sche+raus+nehmen&language=de-DE&voice=KatjaNeural
      media_content_type: music
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
  - delay:
      hours: 0
      minutes: 0
      seconds: 4
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: >-
        media-source://media_source/local/528475__robinhood76__09156-accent-chime-login.wav
      media_content_type: audio/mpeg
      announce: true
      extra:
        volume: 25
    target:
      device_id:
        - c837fe7ce89808b6c675572ebea8e9cb
mode: queued
icon: mdi:account-voice
max: 20

Yes, I as well have run into this. Home Assistant could use to have some type of FIFO queue option for notification end points. A starting point seems like it could be just in the tts services that Home Assistant offers. Might be a HA ‘value add’ for it’s own services vs. the external tts services.

wrap the actual play_media call in your own script and call it like this instead:

make that wrapper queued.

replace all your media_player.play_media calls with script.queued_play_media. (i end up flattening the params when i do this like the above)

looking at the complexity of the script you posted, i’m guessing that you’re well able to do the wrapper, but if you need assistance with that, holler.

btw. you should avoid using device_id when possible, and use the friendly entity_id instead.