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.