2 or more scripts for volume alexa notifications

Hello, sorry for my English first, it is translated directly from Google. I need your help.
I have a script for each alexa echo to create a scene with the previous volume, say the Tts on the volume I want, and then apply the previously saved scene to recover the volume.
These are the scripts:

alias: "Tts lavadora noche atico lavando "
sequence:
  - service: scene.create
    data:
      snapshot_entities:
        - media_player.atico
      scene_id: alexa_anterior_lavadora_noche_atico_lavando
  - service: media_player.volume_set
    data:
      volume_level: 0.2
    target:
      entity_id:
        - media_player.atico
  - service: notify.alexa_media
    data:
      target:
        - media_player.atico
      message: ciclo de lavado terminado, ahora a centrifugar
  - service: scene.turn_on
    data: {}
    target:
      entity_id:
        - scene.alexa_anterior_lavadora_noche_atico_lavando
mode: parallel
max: 10

alias: Tts lavadora noche pantalla lavando
sequence:
  - service: scene.create
    data:
      snapshot_entities:
        - media_player.pantalla
      scene_id: alexa_anterior_lavadora_noche_pantalla_lavando
  - service: media_player.volume_set
    data:
      volume_level: 0.2
    target:
      entity_id:
        - media_player.pantalla
  - service: notify.alexa_media
    data:
      target:
        - media_player.pantalla
      message: ciclo de lavado terminado, ahora a centrifugar
  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.alexa_anterior_lavadora_noche_pantalla_lavando
mode: parallel
max: 10

When I test them individually they work perfectly, the problem comes when I want to put them together and run at the same time. I have tried executing it in an automation and also through another script and the same thing always happens, the first is executed ok but the second is not executed or does not recover its previous state.
I have tried forcing the first script to wait through the “wait for template” option:

alias: "Tts lavadora noche lavando general  "
sequence:
  - service: script.turn_on
    data: {}
    target:
      entity_id:
        - script.tts_lavadora_noche_atico_lavando
  - wait_template: "{{ is_state('script.tts_lavadora_noche_atico_lavando', 'off') }}"
    continue_on_timeout: true
    enabled: true
  - service: script.turn_on
    data: {}
    target:
      entity_id:
        - script.tts_lavadora_noche_pantalla_lavando
    enabled: true
  - wait_template: "{{ is_state('script.tts_lavadora_noche_pantalla_lavando', 'off') }}"
    continue_on_timeout: true
    enabled: true
mode: parallel
max: 10

And launching both at the same time:

alias: "Tts lavadora noche lavando general  "
sequence:
  - service: script.turn_on
    data: {}
    target:
      entity_id:
        - script.tts_lavadora_noche_atico_lavando
        - script.tts_lavadora_noche_pantalla_lavando
mode: parallel
max: 10

I don’t know what I’m doing wrong, I can’t get it. Thank you very much for your help. Regards

There can often be issues when calling concurrent services or calling services on multiple Alexa devices using Alexa media player. The first thing to try is to switch to calling the scripts directly instead of using the script.turn_on service.

alias: "Tts lavadora noche lavando general"
sequence:
  - service: script.tts_lavadora_noche_atico_lavando
  - service: script.tts_lavadora_noche_pantalla_lavando
mode: parallel

Because the scene you are calling will effectively be calling a volume set service as well , you may also need to add a short delay after the scene.turn_on service in each of your scripts.

Hey it works!! I have added a delay of 10 seconds and calling the scripts directly and it works. The only problem is that they don’t say it at the same time. Can you make the Tts say it at the same time?

That will be difficult to do with the method you are using. I use a slightly different method:

alias: Alexa TTS Volume
description: Say something using text-to-speech on an Alexa media player. Optional volume control and reset.
mode: parallel
fields:
  type:
    name: Type
    description: TTS mode to use
    example: announce
    default: tts
    selector:
      select:
        options:
          - tts
          - announce
  entity_id:
    name: Entity
    description: Alexa media player entities.
    default: "media_player.kitchen_dot"
    example: "media_player.kitchen_dot"
    selector:
      entity:
        integration: alexa_media
        domain: media_player
        multiple: true
  message:
    name: Message
    description: Text to speak on devices.
    required: true
    example: "There's a snake in my boot"
    selector:
      text: null
  volume_level:
    name: Volume Level
    description: Volume level to set as float.
    required: false
    example: "0.5"
    selector:
      number:
        min: 0
        max: 1
        step: 0.01
sequence:
  - alias: Save Current Volume
    variables:
      entity_id: "{{ [entity_id] if entity_id is string else entity_id }}"
      saved_volumes: |-
        {%- set saved_volumes_json -%}[
          {%- set comma = joiner(', ') -%}
          {%- for eid in entity_id -%}
            {{- comma() -}}
            {"entity_id": "{{ eid }}", "volume": {{ state_attr(eid, 'volume_level') | float(0) }}}
          {%- endfor -%}
        ]{%- endset -%} {{- saved_volumes_json | from_json -}}
  - alias: Set volume where necessary
    choose:
      - alias: Is Volume Defined
        conditions:
          - condition: template
            value_template: "{{ volume_level is defined }}"
        sequence:
          - alias: Set Volume
            service: media_player.volume_set
            target:
              entity_id: "{{ entity_id }}"
            data:
              volume_level: "{{ volume_level }}"
          - delay:
              seconds: 1
  - alias: Send TTS/Announcement
    service: notify.alexa_media
    data:
      data:
        type: "{{ type }}"
      message: "{{ message }}"
      target: "{{ entity_id }}"
  - delay:
      seconds: 15
  - alias: Return Volume if Necessary
    choose:
      - alias: Is Volume Defined
        conditions:
          - condition: template
            value_template: "{{ volume_level is defined }}"
        sequence:
          - repeat:
              count: "{{ saved_volumes | length }}"
              sequence:
                - alias: Choose Whether to Revert Volume
                  choose:
                    - alias: If there is a Saved Volume
                      conditions:
                        - condition: template
                          value_template: "{{ saved_volumes[repeat.index - 1].volume > 0 }}"
                      sequence:
                        - alias: Change Volume Back to Original
                          service: media_player.volume_set
                          target:
                            entity_id: "{{ saved_volumes[repeat.index - 1].entity_id }}"
                          data:
                            volume_level: "{{ saved_volumes[repeat.index - 1].volume }}"
max: 10

Using the above, your script would look like:

alias: "TTS lavadora noche lavando general"
sequence:
  - service: script.alexa_tts_volume
    data:
      type: tts
      message: "Ciclo de lavado terminado, ahora a centrifugar"
      volume_level: 0.2
      entity_id:
        - media_player.panta
        - media_player.atico
1 Like

Waaaaauuuuu!!! I had tried a lot of scripts that I had found googling but none of them work for me, they only said the text without adjusting the volume.
The script that I had done was a huge exponential job and also only half worked, because they were asynchronous. You have helped me a lot with my automations.
Thanks a lot

1 Like

I’m happy to have helped.

FWIW, you may still experience some small delays between messages due to how the Alexa media player integration/Alexa works. There is a way around this by setting up speaker groups in the Alexa app. I have not used that method because the minimal improvement doesn’t outweigh the additional setup and other limitations inherent to the method… at least in my setup. YMMV.

Also, since the average word-count is usually 20-35% higher in for sentences in Spanish compared to their English equivalent, you may want to make the last delay: a little bit longer. If the delay isn’t long enough, the volume will not be reset properly. I’ve thought about adding another variable field to have a message length option, but it hasn’t been a big issue so I never got around to it… :slight_smile:

Very grateful for your help, take the :coffee: to my health.
If I have noticed some slight delays but nothing worrying, it can be tolerated. In case of seeing that the volume does not recover, I will increase the delay in the script. By now all the ones I have created have always recovered the volume.