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!

I hope my solution helps you out. Works to allow the volume wheel on the Voice PE to control my spotify volume on the house speakers. This can be pasted directly in the yaml section of an automation.

alias: Sync Spotify Volume with Voice Media Player
description: "Sync the volume of Spotify with the voice media player."
trigger:
  - platform: state
    entity_id: media_player.voice_media_player
    attribute: volume_level
action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.spotify_nicolas
    data:
      volume_level: "{{ state_attr('media_player.voice_media_player', 'volume_level') }}"

Thanks a million for your reply! That indeed workes like a charm. I added an offset of 0.04 so that the volume of the speakers are as I want them. Awesome!
There seems to be a short delay or something causing the update of the zone 2 volume not to work if I change volume 5 times in say in 2 seconds but it’s totally acceptable for me now.

FYI, I think I was close, I made a sensor for the Mainzone volume as mentioned above by @Matias_Persson, that sanser works so something else must have caused it not to trigger. If you see what I did wrong please let me know so I can learn from my mistake.

I had this code:

alias: Denon Volume up and down zone 2
description: >-
 Causes Zone 2 volume to follow Main zone.
triggers:
  - trigger: state
    entity_id:
      - sensor.mainzone_volume
    attribute: volume_level
conditions: []
actions:
  - action: media_player.volume_set
    metadata: {}
    data:
      volume_level: "{{ states('sensor.mainzone_volume') }}"
    target:
      entity_id: media_player.denon_avr_x1800h_3
mode: single

Now changed it to your code and added the offset. Couple posts back had some additional spaces, that didn’t work for me. Removing those spaces was the solution for making the volume offset work.:

trigger:
  - platform: state
    entity_id: media_player.denon_avr_x1800h
    attribute: volume_level
action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.denon_avr_x1800h_3
    data:
      volume_level: "{{ state_attr('media_player.denon_avr_x1800h', 'volume_level') +0.04}}"

Thanks again for the reply and apologies for my slow response to it.

/edit:
Just tried changing the code to use the sensor as mentioned by Matthias above, that does work but I couldn’t get the offset to work that way. Also the delay in update (updates the volume 3 out of 5 times I change it or something like that) is identical with the sensor.
no issue. I’m happy with how it works now. Thanks.