Remember current volume, change it when automation running and set it back

I want to emulate what google nest devices do - lower music volume when assistant in action. Is it possible to do it in HA?

At the moment I created automation to set volume to X and after delay set it to Y. It works, but I want to use volume value that was before automation started.

Or alternative automation - lower volume to 20% of current music volume and in the end go back to initial volume.

What devices are we talking about? Sonos speakers have a snapshot/restore option.

it’s dumb speakers connected to HA. HA controls them via mpd integration.

Just stumbled across this:

Could you use it to store volumes?

1 Like

I will check it out, thanks. Looks a bit too complex with yaml.

I see there is also Input number integration Input number - Home Assistant

Have you considered using a snapshot scene?

You use the scene.create action to make a “snapshot scene” of your media_player entity prior to changing its volume using media_player.volume_set. Later you restore the snapshot scene (which reverts the media_player to its previous state).

Reference

Scenes - Creating scenes on the fly

1 Like

I tried it. it works, but the problems are:

  • after activating scene, track starts playing from the beginning
  • it borks mpd current playlist (you will get only one song in the playlist and playback will stop after it)

The behaviors you described depend on the underlying integration and/or the music source. Some produce fewer unwanted side-effects.

In your case, the side-effects are undesirable so you’ll have to revert to using either a script variable or an Input Number helper (or the suggested Trigger-based Template Sensor) to store/restore the media_player’s volume. Which one you choose depends on the application. For example, if just one automation handles both storing/restoring then a script variable is ideal.

yea, I gonna try it later, thanks

Ok, found similar threads.

So actually you can do it with Scene: Create block (in UI)

In Entity states you need to add only volume state:

media_player.mpd:
  state: "{{ states('media_player.mpd') }}"
  volume_level: "{{ state_attr('media_player.mpd', 'volume_level') | float(0) }}"

and in Scene: Activate block select scene you created in Scene: Create block.

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

It works without problems in my testing.


Another solution is to use Define variables block (in UI)

from Turn back previous volume state of media player

variables:
  volume: "{{ state_attr(\"media_player.mpd\", \"volume_level\") }}"

(those \\ added by HA, not sure why)

and in the last block - set volume back using volume variable:

action: media_player.volume_set
metadata: {}
data:
  volume_level: "{{ volume }}"
target:
  device_id:

I did it in YAML and Visual editor says it doesn’t like this configuration.
Anyway it works too.

I like Define variables block method more because it’s more straightforward.
The challenge was to found a proper syntax for volume state. And use YAML a bit :slightly_smiling_face: