Script to smoothly increase volume of media player to a target level

I have a whole house Google audio group that I use this with in conjunction with motion / door contact sensors in order to turn up the volume of a media player as I enter the room.

alias: Music Walk Volume Adjust
fields:
  media_target:
    description: The media player to target
    example: media_player.kitchen_nest
  volume_target:
    description: The target volume to increase to
    example: 0.96
sequence:
  - repeat:
      sequence:
        - delay:
            milliseconds: 650
        - service: media_player.volume_up
          data:
            entity_id: '{{ media_target }}'
      until:
        - condition: template
          value_template: '{{ state_attr(media_target,''volume_level'') >= volume_target }}'
  - delay:
      seconds: 1
  - service: media_player.volume_set
    data:
      entity_id: '{{ media_target }}'
      volume_level: '{{ volume_target }}'
mode: parallel
max: 5

The script takes 2 arguments, 1 is the media player you want to control, and the other is the target volume level.

Here is one of my automations so you can see how it works:

alias: 'Music Walk With Me: Hall'
description: ''
trigger:
  - type: no_motion
    platform: device
    device_id: 16942ffea955cab30431d0ef8be0e53a
    entity_id: binary_sensor.hall_motion_home_security_motion_detection
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: 16942ffea955cab30431d0ef8be0e53a
    entity_id: binary_sensor.hall_motion_home_security_motion_detection
    domain: binary_sensor
condition:
  - condition: device
    device_id: b1dd9b31c5204c4a9068af5307db22d6
    domain: media_player
    entity_id: media_player.hall_speaker
    type: is_playing
  - condition: state
    entity_id: input_boolean.music_walk
    state: 'on'
  - condition: device
    device_id: 01748a082f53b5952ac397d2a42323d9
    domain: media_player
    entity_id: media_player.kitchen_nest
    type: is_playing
  - condition: device
    device_id: fd3325ab72dc4455ac3044beed339fe6
    domain: media_player
    entity_id: media_player.store_room_speaker
    type: is_playing
action:
  - choose:
      - conditions:
          - type: is_motion
            condition: device
            device_id: 16942ffea955cab30431d0ef8be0e53a
            entity_id: binary_sensor.hall_motion_home_security_motion_detection
            domain: binary_sensor
          - condition: numeric_state
            entity_id: media_player.hall_speaker
            attribute: volume_level
            below: '0.6'
        sequence:
          - service: script.turn_on
            target:
              entity_id: script.music_walk_volume_adjust
            data:
              variables:
                media_target: media_player.hall_speaker
                volume_target: 0.96
          - service: timer.cancel
            target:
              entity_id: timer.music_walk_with_me_hall
          - service: timer.start
            data:
              duration: '01:30:00'
            target:
              entity_id: timer.music_walk_with_me_hall
      - conditions:
          - type: is_no_motion
            condition: device
            device_id: 16942ffea955cab30431d0ef8be0e53a
            entity_id: binary_sensor.hall_motion_home_security_motion_detection
            domain: binary_sensor
        sequence:
          - service: timer.cancel
            data: {}
            entity_id: timer.music_walk_with_me_hall
          - service: timer.start
            data:
              duration: '00:15:00'
            target:
              entity_id: timer.music_walk_with_me_hall
    default:
      - service: timer.cancel
        target:
          entity_id: timer.music_walk_with_me_hall
      - service: timer.start
        data:
          duration: '00:15:00'
        target:
          entity_id: timer.music_walk_with_me_hall
mode: restart

As you can see I have timers for each room, so that I can automatically turn down the volume again after a period where it is determined the room is empty.

I hope this helps someone.

4 Likes

Thank you for this! It’s exactly what I was looking for to slowly turn up the volume on an echo playing spotify through our bluetooth receiver in the bedroom. I have it triggered to start playing night nature sounds for 30 minutes when the lights turn off.

1 Like

In case stumbles across this (it’s the number 1 hit for a Home Assistant volume ramp) and needs to ramp the volume up OR down, adding a conditional check against your media_player’s volume attribute (if it has a functional one) and utilizing the choose branching function can accomplish that:

volume_ramp:
  sequence:
    - choose:
      - alias: "Volume ramp down"
        conditions: 
          - condition: template
            value_template: "{{ state_attr(media_target, 'volume_level') > volume_target }}"
        sequence:
          - repeat:
              sequence:
                - delay:
                    milliseconds: 250
                - service: media_player.volume_down
                  data:
                    entity_id: '{{ media_target }}'
              until:
                - condition: template
                  value_template: '{{ state_attr(media_target,''volume_level'') <= volume_target }}'
          - delay:
              seconds: 1
          - service: media_player.volume_set
            data:
              entity_id: '{{ media_target }}'
              volume_level: '{{ volume_target }}'
      - alias: "Volume ramp up"
        conditions: 
          - condition: template
            value_template: "{{ state_attr(media_target', 'volume_level') < volume_target }}"
        sequence:
          - repeat:
              sequence:
                - delay:
                    milliseconds: 250
                - service: media_player.volume_up
                  data:
                    entity_id: '{{ media_target }}'
              until:
                - condition: template
                  value_template: '{{ state_attr(media_target,''volume_level'') >= volume_target }}'
          - delay:
              seconds: 1
          - service: media_player.volume_set
            data:
              entity_id: '{{ media_target }}'
              volume_level: '{{ volume_target }}'

This will ramp up or down to whatever value you specify, including zero. You can further modularize this by placing the delay time / milliseconds in a variable.

2 Likes

Sorry, i have question.
How to get ‘volume_level’ of my Google Nest Mini.
I check in Hass and don’t find anything for Volume Level.
Only set volume.

The volume level is only exposed when it’s actually on. If it’s in standby the volume level is not exposed - although you can still set the volume when it’s in that state, oddly.

I’m getting parse errors when trying to use this…can you assist?