Using Alexa for Audible Notifications

I’ve been using the Alexa notify service for a while now, and i use it to announce when someone is coming down my driveway. Works well for this, but i have had a few edge cases where we have been using Alexa to play music, and the driveway notification mucks things up a bit.

I noticed that sometimes the volume of a particular Alexa is sometimes low, so to combat this i tried to pull together a logic flow like this:

Is Alexa playing?

If yes, Pause, Turn up volume.
Run announcement
turn down volume, resume playing

if no, turn up volume.
Run announcement
turn down volume

But i have been having trouble with the script i use to do this, seems to randomly fail to do the right thing (sometimes doesn’t turn volume up, pause music or sometimes doesn’t pause, just blasts us with loudness). Wondered if anyone could see any obvious mistake i was making?

alias: script_notification_arrival
variables:
  notification_term: null
sequence:
  - variables:
      kitchen_play: false
      lobby_play: false
      lounge_play: false
  - parallel:
      - alias: if_lobby_playing_pause
        if:
          - condition: state
            entity_id: media_player.utility_room
            state: playing
        then:
          - variables:
              lobby_play: true
          - action: media_player.media_pause
            metadata: {}
            data: {}
            target:
              entity_id: media_player.utility_room
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 1
            target:
              entity_id: media_player.utility_room
        else:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 1
            target:
              entity_id: media_player.utility_room
      - alias: if_kitchen_playing_pause
        if:
          - condition: state
            entity_id: media_player.kitchen
            state: playing
        then:
          - variables:
              kitchen_play: true
          - action: media_player.media_pause
            metadata: {}
            data: {}
            target:
              entity_id: media_player.kitchen
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 1
            target:
              entity_id: media_player.kitchen
        else:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 1
            target:
              entity_id: media_player.kitchen
      - alias: if_lounge_playing_pause
        if:
          - condition: state
            entity_id: media_player.lounge
            state: playing
        then:
          - variables:
              lounge_play: true
          - action: media_player.media_pause
            metadata: {}
            data: {}
            target:
              entity_id: media_player.lounge
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 1
            target:
              entity_id:
                - media_player.lounge
        else:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 1
            target:
              entity_id: media_player.lounge
  - parallel:
      - action: notify.alexa_media_utility_room
        data:
          message: "{{notification_term}}"
      - action: notify.alexa_media_lounge
        data:
          message: "{{notification_term}}"
      - action: notify.alexa_media_kitchen
        data:
          message: "{{notification_term}}"
  - parallel:
      - alias: if lobby was playing
        if:
          - condition: template
            value_template: >-
              {% if lobby_play is defined and lobby_play == true %}True{% else
              %}False{% endif %}
        then:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.5
            target:
              entity_id: media_player.utility_room
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - action: media_player.media_play
            metadata: {}
            data: {}
            target:
              entity_id: media_player.utility_room
        else:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.5
            target:
              entity_id: media_player.utility_room
      - alias: if kitchen was playing
        if:
          - condition: template
            value_template: >-
              {% if kitchen_play is defined and kitchen_play == true %}True{%
              else %}False{% endif %}
        then:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.5
            target:
              entity_id: media_player.kitchen
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - action: media_player.media_play
            metadata: {}
            data: {}
            target:
              entity_id: media_player.kitchen
        else:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.5
            target:
              entity_id: media_player.kitchen
      - alias: if lounge was playing
        if:
          - condition: template
            value_template: >-
              {% if lounge_play is defined and lounge_play == true %}True{% else
              %}False{% endif %}
        then:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.5
            target:
              entity_id: media_player.lounge
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - action: media_player.media_play
            metadata: {}
            data: {}
            target:
              entity_id:
                - media_player.lounge
        else:
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.5
            target:
              entity_id: media_player.lounge
description: ""