Google home - set volume up and back again

Here’s an adaptation that also restores the volume to its original volume, after the announcement.

announce_downstairs:
  alias: 'Announce downstairs'
  sequence:
    - service: media_player.volume_mute  # The first part gets rid of the default Google Home bloop-bloop sound. First mutes the speaker...
      data:
        entity_id: media_player.googlehome_downstairs
        is_volume_muted: true
    - service: media_player.play_media  # then plays a random 1 second silent track ...
      data:
        entity_id: media_player.googlehome_downstairs
        media_content_type: music
        media_content_id: "{{ states('input_text.base_url') + '/local/audio/1sec.mp3' }}"
    - delay:
        seconds: 1
    - service: input_number.set_value  # store the current volume level (should be after playing 1sec because only then the volume_level attribute is available)
      data:
        entity_id: input_number.current_volume
        value: "{{ state_attr('media_player.googlehome_downstairs','volume_level') }}"
    - service: media_player.volume_set  # then sets the desired volume for the announcement ...
      data:
        entity_id: media_player.googlehome_downstairs
        volume_level: >-
          {% if now().hour > 18 or now().hour < 7 %} 0.35
          {% else %} 0.65
          {% endif %}
    - service: media_player.volume_mute  # only then unmutes, so you don't hear the volume changing sounds
      data:
        entity_id: media_player.googlehome_downstairs
        is_volume_muted: false
    - service: media_player.play_media  # then it plays a nice ding-dong sound
      data:
        entity_id: media_player.googlehome_downstairs
        media_content_type: music
        media_content_id: "{{ states('input_text.base_url') + '/local/audio/announcement.mp3' }}"
        # My base_url is contained in an input_text, so that I could put it in secrets.yaml. You can just put the external url to your HA frontend here. NB an internal url won't work in my experience.
    - delay:
        seconds: 2
        # milliseconds: 800
    - service: tts.google_say  # then finally here's your TTS message :-)
      data:
        entity_id: media_player.googlehome_downstairs
        message: "{{my_message}}"
    - delay:   # Set this delay for the maximum length of your announcement messages
        seconds: 4
    - service: media_player.volume_mute  # Again, muting to prevent volume changing sounds
      data:
        entity_id: media_player.googlehome_downstairs
        is_volume_muted: true
    - service: media_player.volume_set
      data:
        entity_id: media_player.googlehome_downstairs
        volume_level: "{{ states('input_number.current_volume') }}"
    - delay:
        seconds: 1
    - service: media_player.volume_mute
      data:
        entity_id: media_player.googlehome_downstairs
        is_volume_muted: false

This requires an input_number to be defined in configuration.yaml:

input_number:
  current_volume:
    name: 'Current volume'
    min: 0
    max: 1

Although in the latest HA versions you might be able to use an on-the-fly variable, but I haven’t used that before so there’s that.

7 Likes