Sync volume

Hi!
i want to sync volume between two different media_player.
Volume is an attribute for both entity.

When i change the volume on media player A i want that media_player B change too.

Is it possible to create an automation ?

It is possible. You have to create trigger base on entity attribute.

Here is the example:

automation:
  trigger:
    platform: numeric_state
    entity_id: sensor.temperature
    # Optional
    value_template: "{{ state.attributes.battery }}"
    # At least one of the following required
    above: 17
    below: 25

    # If given, will trigger when condition has been for X time, can also use days and milliseconds.
    for:
      hours: 1
      minutes: 10
      seconds: 5

Then you have to call a service to set a volume on another media_player entity.

Thanks for your help.

I 'll try this:

- alias: 'manage volume'
  trigger:
    - platform: numeric_state
      entity_id: media_player.salon
      value_template: "{{ state.attributes.volume_level }}"
      above: 0
  condition: []
  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.cuisine
        volume_level: ??

For ā€œvolume_levelā€ how can i call the volume level attribute of the media_player.salon?

{{ state_attr('media_player.salon', 'volume_level') }}

Great thanks, almost!

- alias: 'manage volume'
  trigger:
    - platform: numeric_state
      entity_id: media_player.salon
      value_template: "{{ state.attributes.volume_level }}"
      above: 0
  condition: []
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.cuisine
        volume_level: "{{ state_attr('media_player.salon', 'volume_level') }}"

It works the first time i change the volume on media_player.salon, they are sync. But it only works one timeā€¦? strange. Any idea?

I can set a trigger every seconde but itā€™s not very smartā€¦

1 Like

Maybe HA is preventing a loop here. You could also just have a trigger listen for any state change with a sensor

#sensor:
  - platform: template
    sensors:
      salon_volume:
        value_template: >
          {{ state_attr('media_player.salon', 'volume_level') }}
#automation:
- alias: manage volume
  trigger:
    - platform: state
      entity_id: sensor.salon_volume
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.cuisine
        volume_level: "{{ states('sensor.salon_volume') }}"
3 Likes

Yes! Thank you @Mattias_Persson!! Youā€™re the best!

1 Like

this automation works as intended. all you need to do is add the following at the end
service: automation.reload

reason is firs trigger happens when value moves above zero, after that any other movements wont trigger.

reloading the automation, starts the process all over.

Thanks for the thread! Got my volume syncing.
Anyone know how I can subtract so the volumes are pegged but not the same.
I tried

volume_level: "{{ state_attr('media_player.salon', 'volume_level') - 10 }}"

and

volume_level: "{{ state_attr('media_player.salon', 'volume_level') }} - 10"

and

volume_level: "{{ state_attr('media_player.salon', 'volume_level') }}" - 10

and one or two of those saved, but did not work.

Thanks.

Well, obviously none of those would work. The last one is not a even valid template. The second one is, but would result in a string that literally prints out ā€œX.XX - 10ā€ where X.XX is the current volume.

The first one would perform a calculation alright, but end up with a value somewhere between -10 and -9. The range of the volume_level attribute is not an integer 0ā€“100 but a float 0ā€“1.

Thus the way to do it would be this:

volume_level: "{{ state_attr('media_player.salon', 'volume_level') - 0.1 }}

Tried that too, but without the ā€œ0ā€.
Thank you. It works

Hi all,

New to the community and fairly new to building automations.

Iā€™m trying to do exactly this, sync volums between 2 media players.
For my case itā€™s the main zone and zone 2 of my Denon AVR.

Main zone is at the TV and Zone 2 is the additional speakers at the other side of the room in the kitchen area.
I managed to make automations so Zone 2 switches on when I switch to Spotify or Tuner and it switches back to main zone (zone 2 switches off again) only when I switch to my TV input again.
All good.

Now I want to sync the volume of the main zone and zone 2. When I change the volume of the main zone, zone 2 changes accordingly (but with an offset of + 0.05)

Iā€™m terrible at this YAML code stuff (trying hard to learn) so Iā€™m getting stuck.
Seems like the YAML code has changed slightly since the posts from 2020?

I made this part:

description: Sync volume Main zone and zone2
platform: template
sensors:
  mainzone_volume:
    value_template: 
      {{ state_attr('media_player.denon_avr_x1800h', 'volume_level') }}

But when I lookup entity ā€œsensor.mainzone_volumeā€ in the developer tools > states it says ā€œstate unknownā€
so this first step isnā€™t working for me.

@danieljbrennan would you mind sharing the code you are using? As Iā€™m expecting itā€™s slightly nupdated from the 2020 version above?

Thanks for the help, appreciated!