Turn back previous volume state of media player

Hello, I would like ta make automation where TTS is reproduced by speaker. While this action runs I want to mute or give volume to very low for all other media players (now only KODI).

Is there some way how to do it and after some delay (after the TTS message is read) and return back to previous Volume?

Thanks.

The way to solve this is to set the current volume to an input_number variable before you play the TTS. Then, play the TTS, then set the volume using the media_player.volume_set. You might need to use a 5 second delay or something depending on what service you are using for TTS generation.

What you’ll want to make sure of is that your media player supports the volume_level attribute. Not all of them do and I’m not sure is Kodi does or not.

1 Like

Or creata a scene on the fly as explained here.

3 Likes

I completely forgot about using a scene for volume control. Derp. Good catch sir!

Hm, but how can it “remember” how was volume set before TTS starts? I need to recall this remmbered state after TTS again…

Using the scene.create service. So, in your case, you’d want to do the following:

- service: scene.create
    data:
      scene_id: before
      snapshot_entities:
      - media_player.[your_media_player]

Then, after you send the TTS (again, you might need a delay depending on the length of the TTS):

- service: scene.turn_on
    data:
      entity_id: scene.before
4 Likes

This is possible? Great. This “scene before” is stored in some cache?

1 Like

Yup. Check the link that @Burningstone posted.

and…this could cause my whole KODI media restart. because if there will be movie playing and I will make some type of snapshot it will cause media reload. Or not?
What to do if I need to handle only pause/play? Or mute/unmute?

Possibly? I’m not 100% certain. If you want to JUST capture the volume, you’ll need to go with the method I posted up above (capture the current volume into an input_number, call the TTS service, restore the volume using the media_player.volume_set service).

The easier route is going to be the scene.create service, but you should try both to see what works for you.

1 Like

Did you get this working with scene.create? Because this is wreaking my head.

It does not remember media_player source, and state. Everytime when I activate created scene, it plays different source. Anyone have fix for that ?

1 Like

Hi Guys, I would like to get on board on this topic. I’ve also struggled with this and tried the snapshot entities solution. While everything works out fine in the trace, the create scene/snapshot function won’t restore volume. This is my automation;

alias: Test
description: ''
trigger: []
condition: []
action:
  - choose:
      - conditions:
          - condition: device
            device_id: 1abfe37e9443f0d11207ce701f94912e
            domain: media_player
            entity_id: media_player.nest_mini
            type: is_off
        sequence:
          - service: media_player.turn_on
            target:
              device_id: 1abfe37e9443f0d11207ce701f94912e
    default: []
  - service: scene.create
    data:
      snapshot_entities: media_player.nest_mini
      scene_id: media_player_previous_state
  - service: media_player.volume_set
    data:
      volume_level: 0.6
    target:
      device_id: 1abfe37e9443f0d11207ce701f94912e
  - service: tts.cloud_say
    data:
      entity_id: media_player.nest_mini
      language: nl-NL
      cache: true
      message: Test, één, twee, drie.
  - delay:
      hours: 0
      minutes: 0
      seconds: 8
      milliseconds: 0
  - scene: scene.media_player_previous_state
  - choose:
      - conditions:
          - condition: device
            device_id: 1abfe37e9443f0d11207ce701f94912e
            domain: media_player
            entity_id: media_player.nest_mini
            type: is_idle
        sequence:
          - service: media_player.turn_off
            target:
              device_id: 1abfe37e9443f0d11207ce701f94912e
    default: []
mode: single

Does anybody have an idea how to solve this?

Best,

I’m having the same issue, if I create a scene then try to restore it at the end of the automation, nothing happens. It doesn’t seem to take a snapshot of all attributes or it doesn’t reapply them. Would love to find a fix for this since this can be a pretty important feature.

I know this is a dead topic, but I had the same challenge and found this page but it did not solve it for me. I did find a solution - at least for Spotify media players - that I would like to share with anyone searching for the answer on this page. To see if you can use this solution, check the Developer Tools > States, find your media player and check if it has the volume_level attribute (also not sure if it’s always there or only if you set the volume at least once).

The key then is to use templating, defining a variable at the beginning of the script and using it later to restore the volume:

play_sound:
  alias: Play Sound
  variables:
    volume: '{{ state_attr("media_player.spotify_yourname", "volume_level") }}'
  sequence:
  - service: media_player.volume_set
    data:
      volume_level: 0
    target:
      entity_id: media_player.spotify_yourname

# Do your stuff

  - service: media_player.volume_set
    data:
      volume_level: '{{ volume }}'
    target:
      entity_id: media_player.spotify_yourname

I hope it helps someone.

5 Likes

this helped! thank you

1 Like