Managing volume in notify.alexa_media

Hello all,
I have this simple speech sequence:

  sequence:
  - service: notify.alexa_media_echo_spot
    data:
      message: This is my message
      data:
        type: tts

to which I would like to set the volume to maximum, and at the end return to the previous setting that I don’t know. I need it to send a warning to the maximum volume and then reset the volume previously existing in the device. It’s possible to do it?
Also, if I wanted to send a message to a whole group managing the volume, like

- service: notify.alexa_media_everyone

how could I do?
Thank you

1 Like

You can use variables in scripts, store current volume in one variable, set volume to maximum, announce your message and set volume to the variable.

Example: Script: saving entity state and restore it at the end of the script - #7 by 123

Hello @anon63427907 ,
Thank you for your answer. Theoretically this is correct, but how can I manage the volume of multiple devices with a message sent to a group?

You can get benefit of scenes but not sure if scenes will support groups.

You can create a scene on the fly and save the current state of defined entities into a scene, which will not be saved to db but will be available until restart. At the beginning, create scene and store the state of group (or each entities under group), send your notification and apply scene. Just a thought experiment.

Hi @anon63427907 ,
the solution would seem excellent, but putting it into practice is far from simple. I should

- know all the alexa_media entities (the group is everyone)
- extract the volume value individually
- change the volume at all
- send the message
- set the old volume of the single entities

I don’t know if such a complex scene like this is achievable, I need to study it.
Anyway thanks for the idea.

i am also interested in this and have a possible way to start from. I think it’s not possible to notify all echo devices at once but with a delay this is working: (you need to create a group with all echo devices mine is group.all_media_players)

  sequence:
    - variables:
        players_and_volume: >
              {% set x = expand('group.all_media_players')
                  | list %}
              {% set ns = namespace(result=[]) %}
              {% for s in x %}
                {% set ns.result = ns.result + [ [s.entity_id, s.attributes.volume_level] ] %}
              {% endfor %}
              {{ ns.result }}
    - repeat:
        count: '{{ players_and_volume | count }}'
        sequence:
        - delay: "00:00:02"
        - variables:
            player_and_volume: '{{ players_and_volume[repeat.index - 1] }}'
        - service: media_player.volume_set
          data_template:
            entity_id: "{{ player_and_volume | first }}"
            volume_level: "0.03"
    - delay: "00:00:02"
    - repeat:
        count: '{{ players_and_volume | count }}'
        sequence:
        - delay: "00:00:02"
        - variables:
            player_and_volume: '{{ players_and_volume[repeat.index - 1] }}'
        - service: notify.alexa_media
          data_template:
            target: "{{ player_and_volume | first }}"
            data:
              type: tts
            message: "A"
    - delay: "00:00:02"
    - repeat:
        count: '{{ players_and_volume | count }}'
        sequence:
        - delay: "00:00:02"
        - variables:
            player_and_volume: '{{ players_and_volume[repeat.index - 1] }}'
        - service: media_player.volume_set
          data_template:
            entity_id: "{{ player_and_volume | first }}"
            volume_level: "{{ player_and_volume | last }}"

got the idea from Repeat same action on multiple person - how to optimise this automation? - #5 by 123 and Get multiple mapping values in templating - #2 by 123

1 Like

Wow. Fantastic! It is practically the code of what first @anon63427907 and then I have expressed schematically. Something will certainly need to be adapted (I don’t know if Alexa’s everyone group is accessible), but it’s definitely a big job ready and well done. Tomorrow I will try to adapt and modify it for my environment, but apparently the most is done.
Thanks @florian.ec, really very useful.

i am very sure that notify does not work with alexa groups Configuration: Notification Component · custom-components/alexa_media_player Wiki · GitHub

however, i have an idea to get all media_player entities from the alexa_media integration.
This will match only media_player entities (cause there is a chance that sensors are created from the integration Home · custom-components/alexa_media_player Wiki · GitHub )

{% set alexa_media_entities = integration_entities("alexa_media") | select('match', "media_player") | list | join('|') %}
{% set x = states.media_player | select("search", alexa_media_entities) | list %}

and only the entity_id’s as list for the notify target

{% set alexa_media_entities = integration_entities("alexa_media") | select('match', "media_player") | list | join(',') %}

so my previous sequence would be

  sequence:
    - variables:
        players_and_volume: >
          {% set alexa_media_entities = integration_entities("alexa_media") | select('match', "media_player") | list | join('|') %}
          {% set x = states.media_player | select("search", alexa_media_entities) | list %}
          {% set ns = namespace(result=[]) %}
          {% for s in x %}
            {% set ns.result = ns.result + [ [s.entity_id, s.attributes.volume_level] ] %}
          {% endfor %}
          {{ ns.result }}
    - repeat:
        count: '{{ players_and_volume | count }}'
        sequence:
        - delay: "00:00:02"
        - variables:
            player_and_volume: '{{ players_and_volume[repeat.index - 1] }}'
        - service: media_player.volume_set
          data_template:
            entity_id: "{{ player_and_volume | first }}"
            volume_level: "0.5"
    - delay: "00:00:02"
    - service: notify.alexa_media
      data_template:
        target: >
          {% set alexa_media_entities = integration_entities("alexa_media") | select('match', "media_player") | list | join(',') %}
          {{alexa_media_entities}}
        data:
          type: tts
        message: "Test"
    - delay: "00:00:02"
    - repeat:
        count: '{{ players_and_volume | count }}'
        sequence:
        - delay: "00:00:02"
        - variables:
            player_and_volume: '{{ players_and_volume[repeat.index - 1] }}'
        - service: media_player.volume_set
          data_template:
            entity_id: "{{ player_and_volume | first }}"
            volume_level: "{{ player_and_volume | last }}"

With this there is no need to create an additional group.

1 Like

Hi Florian!
Thanks for the script! Works very well.
I just change the select(‘match’, “media_player”) to select(‘match’, “media_player.echo”), because I have other devices that match, and had “Undefined” values that broke the script later.
Just a question, why the delays?

The prosody amazon polly ssml tags work for me to change the volume of tts notifications.
Here’s an example of my tts message with volume lowered by 5 db. Range is -6 to +6 db.

<prosody volume="-5dB">Hey bill, there's an animal in the back yard</prosody>

This worked better for me than changing device volumes.
Maybe it will work for others.
Link to other prosody tags
https://docs.aws.amazon.com/polly/latest/dg/supportedtags.html#prosody-tag

3 Likes

Where to put it? I’m using the following in an automation as action:

service: notify.alexa_media
data:
  message: Achtung, es brennt! Verlasst das Haus!
  target:
    - media_player.saschas_echo_dot_kuche
    - media_player.saschas_echo_dot_wohnzimmer
    - media_player.saschas_echo_show

If you are talking about where to use the prosody tags.
I put
<prosody volume=\"-5dB\">Hey bill, there's an animal in the back yard</prosody>
as the message entry.

Here’s the yaml result

service: notify.alexa_media
data:
  message: "<prosody volume=\"-5dB\">Hey bill, there's an animal in the back yard</prosody> "
  target: media_player.bill_s_echo_dot