How can I use a script field as a value for snapshot_entities in the service: scene_create

I have several automations that send out text messages as voice to various media players. I have simplified this, by creating a script with fields (se below) that can be called from any automation, that simply needs to send a voice message to one or more media players.
It works, but I would also like to restore the state of the media player, when the message has been delivered by the media player.
In the automations that was done by calling the service: scene.create and saving the state using the snapshot_entities of the media_player.
Now here’s my problem:
I can’t figure out how I can use the field “speaker” in place of the entity_id: “media_player.hub_stue” in my script. I know that the “speaker” field resolves to “entity_id: media_player.hub_stue”, which is not accepted as a value by snapshot_entities.

Can any of you please give me some advise on this problem?

Thanks

alias: "warning: speak text message"
sequence:
  - service: scene.create
    data:
      scene_id: before_warning
      snapshot_entities: media_player.hub_stue
  - service: media_player.volume_set
    data:
      volume_level: "{{ speaker_volume }}"
    target: "{{ speaker }}"
  - service: tts.cloud_say
    data:
      message: "{{ text_message }}"
      options:
        gender: male
    target: "{{ speaker }}"
  - delay: "00:00:03"
  - service: scene.turn_on
    target:
      entity_id: scene.before_warning
    data: {}

fields:
  speaker:
    selector:
      target:
        entity:
          domain: media_player
    name: speaker
    required: true
  text_message:
    selector:
      text: null
    name: Text Message
    required: true
    default: null
  speaker_volume:
    selector:
      number: null
    name: Speaker Volume
    required: true
    default: 0.5
mode: single

For speaker, use an Entity Selector instead of a Target Selector. Then you can reference it with a template in scene_entities.

Naturally you will need to adjust all existing references to speaker in your script because it will no longer be based on a Target Selector.

  - service: scene.create
    data:
      scene_id: before_warning
      snapshot_entities: 
        - "{{ speaker }}"
  - service: media_player.volume_set
    data:
      volume_level: "{{ speaker_volume }}"
    target:
      entity_id: "{{ speaker }}"
  ... etc ...

Thanks Taras

That makes total sense. I changed the other references accordingly, and they work as expected.
However, the state of the speaker (which was playing youtube music) is unfortunately still not restored.
If possible, could you please call the script, and verify that it works for you.
Thanks for your help.

alias: "warning: speak text message"
sequence:
  - service: scene.create
    data:
      scene_id: before_warning
      snapshot_entities:
        - "{{ speaker }}"
  - service: media_player.volume_set
    data:
      volume_level: "{{ speaker_volume }}"
    target:
      entity_id: "{{ speaker }}"
  - service: tts.cloud_say
    data:
      message: "{{ text_message }}"
      options:
        gender: male
    target:
      entity_id: "{{ speaker }}"
  - delay: "00:00:05"
  - service: scene.turn_on
    target:
      entity_id: scene.before_warning
    data: {}
fields:
  speaker:
    selector:
      entity:
        domain: media_player
    name: speaker
    required: true
  text_message:
    selector:
      text: null
    name: Text Message
    required: true
    default: null
  speaker_volume:
    selector:
      number: null
    name: Speaker Volume
    required: true
    default: 0.5
mode: single

That’s a separate issue from your original question. Restoring a media_player’s state depends on the capabilities of its underlying integration and the content it’s playing.


FWIW, I have Sonos speakers so I use sonos.snapshot instead of scene.create.


EDIT

I found one of my old posts that contains a script very similar to the one you’re using (although without variables) and the person who tested it reported even the volume level wasn’t restored. They said their media_player was using Google Cast.

1 Like

Many thanks,

I will continue experimenting. Apparently, Google Nest Hub and Nest Audio reacts differently on the snapshot_entities request.
I’ve just found out that it works on my Nest Hub Max.

Regards