Alexa/Echo Volume Store/Recall

I have several Announcement automations involving Alexa’s. (ie. Good Morning templates, Washing Machine complete, Garbage reminders, Front door opened, etc…).

I’d like to be able to store the Echo’s volume before these Announcements fire, and then revert back to that volume after the announcements. All of my Announcement automations use a volume_level, but if music/radio/etc is playing before the announcement, I’d like it to go back to the volume it was at.

Alexa volume is a state attribute.
I think I need to use some sort of trigger based template, but I’m a bit of a noob with templates.
Perhaps the announcement automation triggers a 1st action to store the current volume_level, then run the remaining automation actions, then the final action is to go back to that original volume.

Any suggestions? Point me down the right path? Thanks.

I guess there’s a better way but, you could save the current volume in an input_text. Then revert back

EDIT

I’m not familiar with them but, maybe using variables?

I use the following scripts to save the volume and then restore it after the announcement:

script:
  tts_save_dot_volume:
    alias: Save Current Volume for Echo TTS
    sequence:
      - service: variable.set_variable
        data:
          variable: tts_comp_rm_dot_volume
          value_template: "{{ state_attr('media_player.computer_room_dot', 'volume_level') }}"
  tts_restore_dot_volume:
    alias: Restore Previous Volume for Echo TTS
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.computer_room_dot
          volume_level: >
            {% if states('variable.tts_comp_rm_dot_volume') != 'None' %}
              {{ states('variable.tts_comp_rm_dot_volume') }}
            {% else %}
              .5
            {% endif %}

It uses the custom “hass-variables” integration but you could do the same using input_numbers by changing the service.

and this is how it’s used in an automation:

    action:
      - service: script.tts_save_dot_volume
      - service: media_player.volume_set
        data:
          entity_id:
            - media_player.computer_room_dot
          volume_level: 0.9
      - delay: '00:00:05'
      - service: notify.alexa_media
        data:
          target: 
            - media_player.computer_room_dot
          data:
            type: tts
          message: > <some_message_here>
      - service: script.tts_restore_dot_volume
6 Likes

This seems like exactly what I’m looking for. Didn’t realize it was built in! Thanks, giving it a go now.

Well, I’m not sure what you mean by “built-in”.

If you use the variables that I use you need to install the custom integration first.

If you use input_numbers then you need to create one for each echo device that you want to save.

So I wouldn’t call that built-in.

I’ve gone the custom HACS component route, but getting a stupid error (stupid in that it’s me that is the error :). Can create your first script, but the second one (tts_restore_dot_volume) gives me an error:

Message malformed: extra keys not allowed @ data[‘tts_restore_dot_volume’]

I’ve literally pasted your script in, only changing the name of the media player.

tts_restore_dot_volume:
  alias: Restore Previous Volume for Echo TTS
  sequence:
    - service: media_player.volume_set
      data:
        entity_id: media_player.russell_s_echo_plus
        volume_level: >
          {% if states('variable.tts_comp_rm_dot_volume') != 'None' %}
            {{ states('variable.tts_comp_rm_dot_volume') }}
          {% else %}
            .5
          {% endif %}

I don’t think it’s that script that is causing it.

I copied your script code above directly into my config (only added a “_2” to the alias name) and I get no errors on validation.

I don’t see anything wrong in there.

Did you also create the “variable.tts_comp_rm_dot_volume”?

Sorry for the noobness… I only created the 2 scripts you had above. The first one did not throw an error upon creating it, however it errors when I run it (unable to find service variable.set_variable)

I think I missed the part in the custom component where I needed to put the variables in the config file?

OK, no problem.

So let’s recap…

you installed the custom integration (“hass-variables” by rogro82 has been renamed to “variables+history” by wibias in HACS)?

you created the scripts.

but didn’t actually create the variables themselves yet?

That would be the correct recap :slight_smile:
I can see on the repo instructions for creating the variables, I’m assuming this goes in my config.yaml file?

OK, good.

so then somewhere in your config you need to create the variable:

variable:
  tts_comp_rm_dot_volume:
    value: '0.5'
    restore: true

That’s for my echo but you should probably rename it to something useful for you. Then you will change the entity_id in the scripts to match the name you chose.

yes. you should put it there.

But if you use !include you can put it wherever you want as long as it’s properly referenced in your configuration.yaml

I use packages quite a bit so I created mine in my tts package. But it will work the same either way.

Here is where I’m at. Thanks for your help with this.
Installed HACS component - hass-variables https://github.com/Wibias/hass-variables/blob/master/README.md
I’m still getting an error when I try to run one of the 1st script (Save Current Volume). The 2nd script (Restore Volume) seems to to run fine when triggering these via the UI.

Created 1 variable in config.yaml:

variable:
  tts_echo_plus_volume:
    value: '0.5'
    restore: true

Created 2 scripts for Save Volume & Restore Volume:

announce_save_echo_volume:
  alias: Save Current Volume for Echo TTS
  sequence:
  - service: variable.set_variable
    data:
      variable: tts_echo_plus_volume
      value_template: '{{ state_attr(''media_player.russell_s_echo_plus'', ''volume_level'')
        }}'
tts_restore_dot_volume:
  alias: Restore Previous Volume for Echo TTS
  sequence:
    - service: media_player.volume_set
      data:
        entity_id: media_player.russell_s_echo_plus
        volume_level: >
          {% if states('variable.tts_echo_plus_volume') != 'None' %}
            {{ states('variable.tts_echo_plus_volume') }}
          {% else %}
            .5
          {% endif %}

did you restart HA after installing the custom integration?

it seems like it isn’t picking up the new integration because it is the thing that adds the “variable.set_variable” service to HA.

Yes, several times. The service is there in developer tools, so I don’t think that is the issue. I get this in the logs when trying to run the first script:

Save Current Volume for Echo TTS: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘value_template’]

I tried moving the last line of }}’ up to the same line in the value_template, as well as trying with a space and without.

oops, my bad.

that was a change that happened a while back and I guess this was one of the scripts that I forgot to properly update.

try this instead:

announce_save_echo_volume:
  alias: Save Current Volume for Echo TTS
  sequence:
  - service: variable.set_variable
    data:
      variable: tts_echo_plus_volume
      value: '{{ state_attr(''media_player.russell_s_echo_plus'', ''volume_level'')
        }}'

apparently this script hardly ever gets used so I guess that’s a good thing since it likely hasn’t worked for a while now! :laughing:

That did the trick! Thanks so much, really appreciate the help. Now onto the automations I go.

1 Like

Hi there,
Did this change by any chance? This is not working for me. I think the integration had a breaking change and someone changed it and how it is set up. But I can not get this to work, unfortunately.

I always send these a little too early. I got it figured out. Thanks for your guide :slight_smile:

Could you share your YAML? I’m going to be starting on this, this evening and will review!

Thank you!