[solved with a helper] (re)store media player volume in/(from) on the fly scene?

I want to store only the volume level from 3 mediaplayers to be able to restore them.
The way I tried to do this is with ‘scenes on the fly’ in an automation but somehow I’m struggling with the template.

This template gives me the right value, so that seems OK:

{{ state_attr(‘media_player.2_kitchen’, ‘volume_level’) }}

If I create the scene with the following automation, the scene is created:

action:
  - data_template:
      scene_id: before
      snapshot_entities:
        - media_player.2_kitchen
        - media_player.1_livingroom
      entities:
        media_player.2: >
          volume_level: "{{ state_attr('media_player.2_kitchen', 'volume_level') }}"
        media_player.1: >
          volume_level: "{{ state_attr('media_player.1_livingroom', 'volume_level') }}"
    service: scene.create

When I activate this scene, the volume is restored but it also affects the current playlist.

store the volume in a helper and set new volume

  - service: input_number.set_value
    data:
      value: "{{(state_attr('media_player.living_room','volume_level')|float)}}"
    target:
      entity_id: input_number.mm_volume
  - service: media_player.volume_set
    data:
      volume_level: 0.4
    target:
      entity_id: media_player.living_room

do your other stuff and then restore:

  - service: media_player.volume_set
    data:
      volume_level: "{{states('input_number.mm_volume')}}"
    target:
      entity_id: media_player.living_room
1 Like

Hi aceindy, thanks for this solution! :bowing_man:

I was already thinking of a different approach instead of using an on the fly scene by storing the value somehow.
So apart from this being solved, I have learned something new! :+1:

Thanks A LOT!

The reason your automation affected the current playlist is because it employs snapshot_entities which stores many media_player attributes which are later restored.

It’s possible to use a scene to restore the volume level (a separate helper is not needed).

I used the following to temporarily change a media_player’s volume level without affecting what it’s currently playing after the scene is restored. Note that the scene.create service call doesn’t employ snapshot_entities. The inclusion of state is mandatory otherwise an error will appear in the Log when the automation executes.

  - service: scene.create
    data:
      scene_id: before
      entities:
        media_player.kitchen:
          state: "{{ states('media_player.kitchen') }}"
          volume_level: "{{ state_attr('media_player.kitchen', 'volume_level') | float(0) }}"
        media_player.living_room:
          state: "{{ states('media_player.living_room') }}"
          volume_level: "{{ state_attr('media_player.living_room', 'volume_level') | float(0) }}"
  - service: media_player.volume_set
    data:
      volume_level: 0.2
    target:
      entity_id:
        - media_player.kitchen
        - media_player.living_room
  - ... whatever else you want to do ...
  - service: scene.turn_on
    target:
      entity_id: scene.before
mode: single
2 Likes