Storing current volume levels

Let’s step back: Why do you want to store the volume?

If the goal is to restore a media_player to its previous state (volume, content, etc) it can be more efficient, and simpler, to use a snapshot scene.

1 Like

I am using telegram to broadcast an audio message to a speaker. I turn the volume up but now wanting to return it to original state. I will look for a snapshot scene tutorial.

Many thanks

You can use scenes for that - create snapshot before and then restore it.

1 Like

Review the second example shown here: Creating scenes on the fly. Let me know if you need additional assistance.

Many thanks. I am using the following Blueprint: “Telegram Bot: Respond to Telegram Command by executing an action”

The yaml part of the scene activation is
image

but this says the device is known even though the visual editor lists it.

Not sure how to link it all together.

I can help you compose an automation that employs snapshot scenes. If you’re encountering difficulties with someone’s blueprint, ask them for assistance. They invented it so they know it best.


EDIT

FWIW, the screenshot you posted contains an invalid value for device_id. You supplied an entity_id (scene.photoframe_savesettings) but it wants a device_id (a long string of numbers and letters). I guess you entered the entity_id in the wrong field of the blueprint.

There’s something you need to take into account: the volume is only there when the media_player is on.

1 Like

Ok, not sure… but I will eventually get there :slight_smile:

aah, ok so that it why I don’t see any reference to in in any dropdowns of the visual interface then. Looks like I am going to need to spend the time getting to know YAML :wink:

Not unless the blueprint is designed to exclude media_players that are stopped/idle (which would be a strange constraint).

BTW, I looked at the blueprint you said you were using:

However, I also looked at the blueprint’s code and it doesn’t appear to be anything like the screenshot you posted.

https://raw.githubusercontent.com/marc-romu/home-assistant_blueprints/main/blueprints/automation/marc-romu_telegram-bot/telegram-bot--command__action.yaml

Ok so I think I understand how it works now

scene.create locks in the current settings for media_player.photo_display

scene.turn_on should restore them.

However my volume does not return to it’s original state.

By posting screenshots, instead of YAML, you’re effectively discouraging others from posting corrections to your example. Why? Because in order to reply to you, they have to re-type what is shown in your screenshots. In addition, no one can see what you entered in the sections that are not expanded.

For more information, refer to guideline 11 in the FAQ.

image

Appologies

alias: "Telegram: /say"
description: Broadcast message to speakers
use_blueprint:
  path: marc-romu/telegram-bot--command__action.yaml
  input:
    telegram_command: /say
    telegram_bot: "@mybot_bot"
    sequence:
      - service: scene.create
        data:
          scene_id: photoframe_savesettings
          snapshot_entities: media_player.photo_display
        enabled: true
      - service: media_player.volume_set
        data:
          volume_level: 1
        target:
          device_id: c56f7aaf9b35374575f9b029884c52d2
      - service: tts.google_translate_say
        data:
          entity_id: media_player.photo_display
          message: "{{ telegram_args_as_text }} "
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - service: scene.turn_on
        data: {}
        target:
          entity_id: scene.photoframe_savesettings
        enabled: true

Create the following script (using the built-in Script Editor, create a new empty script, copy-paste the YAML shown below into it, then save the script).

alias: Example 1
sequence:
  - service: scene.create
    data:
      scene_id: example_1
      snapshot_entities: media_player.photo_display
  - service: media_player.volume_set
    data:
      volume_level: 0.75
    target:
      entity_id: media_player.photo_display
  - service: tts.google_translate_say
    data:
      entity_id: media_player.photo_display
      message: "Hello world!"
  - delay:
      seconds: 5
  - service: scene.turn_on
    target:
      entity_id: scene.example_1
  • To test it, first set the media_player’s volume to a value lower than 75% like 25%.
  • Run the script. Confirm the media_player announces "Hello world!
  • Check if the media_player’s volume is now back to 25% or is still at 75%.
1 Like

Additionally you might want to actually open the scene and look at it’s YAML and post it here.
That will at least tell us if it did actually snapshot the volume.
But because the media_player is called photo_display I am guessing it’s probably a Google Nest Display Gen 2, or a Lenovo Smart Display - and Chromecast devices don’t provide a volume level when they are not active, so the volume would not be part of the snapshot.

You would need to first send the power on command to the media player, then snapshot it.

A good spot to learn what attributes are available is in the Developer Tools “States” tab.

When writing my original reply I went there to see if I could do what you’re looking for myself, as I do have some automations that play with media_player volumes but only to set them, not store them. When I did look there I didn’t find a attribute for the current volume, hence I commented that you may have difficulty pulling that value from your photoframe. but thanks to @Edwin_D’s comment I now know that the attributes do exist but are only accessible when the media_player is on.

1 Like

One comment I have on this snippet is the delay - whilst this will likely achieve what you’re after, one thing to consider is the intent. The intent here is to delay the rest of the script while the photo frame plays the audio.

I did this the same way with my announcements, but that involved some tweaking on the longer “briefings” to allow sufficient delay. I then had some occasions that the announcement would get cut off as there was a slight timing differences between the script triggering, the media_player turning on, then starting the audio. You could obviously resolve this with a longer delay, but again: intent.

What I ended up doing was changing the script from having a delay to having two wait_for_trigger's. One would wait for the media_player to be playing, the next would wait for the media_player to be idle. The script would then continue as normal. This meant that I could have the delay without having to fuss about with timings, and I could use the same code on any annoucement (I actually ended up moving this ‘delay’ code out to it’s own automation which triggers when media_player's start playing and waits for them to become idle; but my use case was for switching them back off when done, not fiddling with volumes)

1 Like

The volume stays at 75%

What kind of media player is it? To put it another way, which integration are you using to integrate the media player with Home Assistant?

It lists under Google Cast: Lenovo Smart Display 10