Media Player return to playing state

Hey gang,

Here’s my scenario. I have HA setup to play different online radio stations on my Google Home nest speaker. I’m trying to create an script that when triggered plays a short song on a said Google Home nest speaker then returns back to the music it was streaming prior to being triggered. The key is the radio stations change so I want it to be able to recall the station that was playing prior to the trigger.

I think I’m close to having it setup right using the “scene.create” and “scene.reload” call services. But think I’m missing a part in either the “Scene entity ID”, “Entities state” or “Snapshot entities”. Any advise?

alias: Auto Trigger
sequence:
  - service: scene.create
    data:
      scene_id: scene_autom
      snapshot_entities: media_player.office
  - service: media_player.volume_set
    data:
      volume_level: 0.35
    target:
      entity_id: media_player.office
  - service: media_player.play_media
    data:
      media_content_id: media-source://media_source/local/Music/sfx01.mp3
      media_content_type: music
    target:
      entity_id: media_player.office
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: media_player.turn_off
    target:
      entity_id: media_player.office
  - service: scene.reload
mode: single

Is there really no way of doing this?

According to the last example posted in the documentation, at the end of your script you need to turn on the snapshot scene you created at the beginning of the script in order to restore it.

Try replacing this:

  - service: scene.reload

with this:

  - service: scene.turn_on
    target:
      entity_id: scene_autom

If that fails to work then it implies the snapshot scene is unable to store/restore the media_source from an online provider.

BTW, the script contains two options on the same line (typo?):

      media_content_id: media-source://media_source/local/Music/sfx01.mp3

Thank you for your comment. I’m certainly closer but maybe your right. It may not able to restore media_source, which is a major bummer. I’ve got it now where the volume goes back to what it was prior and the speaker makes the chime noise like it’s beginning to play something but it doesn’t. =( Is there anything else I need to add into the “snapshot_entities:” section in order for it to recall the media?

With your question about the typo. That’s correct. Strangely that’s the only way I’m able to get local media to play. Weird, right?

I read about the issue in another post where a snapshot scene couldn’t restore a Spotify stream. The workaround that was presented stored everything in variables, then restored everything from the variables afterwards. For more information, refer to this post:

FWIW, I also learned (from yet another post) that the Sonos integration has a dedicated service call for this purpose.

Hi @techtim,

Be aware that different platforms (Sonos, google ChromeCast, Nest, Mi AI Speaker, Mi TV Box, Airplay etc.) behave differently.

For Sonos it is further different state whether you for example are streaming Spotify or a radio station controlled from the Sonos App, or you directly use the SpotifyConnect on Sonos connecting from a Spotify Client or The Airplay from for example TuneIn on your mobile.

In order to figure out what you are missing: Take a snapshot (copy all the contents from the developer state window) of the state and al attributes of your media player prior and after your script call, then you will find out what you are missing. Another parameter is timing. Some changes in the state or attributes need some time to take effect completely and you need to wait for that before you fire the next action

Hope this gives you some ideas about how to proceed. Otherwise you are welcome to share the state and attributes snapshots before and after, then I will try to help if In can.

Yes, this is super helpful. Thank you!

So once I have that info captured how can I apply it to the script? Here’s how the capture looks prior to launching the script. The “media_content_id” is different from time to time.

state - playing

volume_level: 0.10000000149011612
is_volume_muted: false
media_content_id: http://wallyjradio.com:8000/80sAmerica
media_position: 1.364503
media_position_updated_at: '2021-07-17T10:15:26.656872+00:00'
app_id: CC1AD845
app_name: Default Media Receiver
entity_picture_local: null
friendly_name: Office
supported_features: 152463

After the script this is what it looks like.

state - off

friendly_name: Office
supported_features: 152461

Hi @techtim,

This proves that the scene snapshot doesn’t work!

I’d suggest that you use my script her:

and modify it to play a stream url instead of sending a TTS. that is you replace

service: tts.google_translate_say
    data:
      entity_id: '{{ tts_entity }}'
      language: da
      message: '{{ msg }}'

By

- service: media_player.play_media
    data:
      media_content_id: media-source://media_source/local/Music/sfx01.mp3
      media_content_type: music
    target:
      entity_id: media_player.office

It you only have one specific media player you want to do this on, then you can hard code the media_player entity (tts_entity) instead of passing it as a variable, on the other hand if the stream you want to play is always the same, then keep it as is, otherwise consider passing it as a parameter to the script.

Please let me known if you need further help to do this.

Kind regards,
Ghassan

Wow, thank you for being so helpful. So the radio stream or media_content_id can change from day to day. So what “parameter” as you said do I need to set or input when that variable changes and is random?

Have you tried removing this part

 - service: media_player.turn_off
    target:
      entity_id: media_player.office

It should not be necessary to turn your device off.

So this works but I’m wanting it to capture the radio stream is was playing prior to firing the sound effect then recall that same radio stream. That radio stream will be different from time to time. That’s my major hiccup. I have no issue just inputting a manual radio stream but I’m wanting it to recall the random stream it was playing prior to running. Hope I’m not confusing anyone… =/

Hi @techtim,

I made a modified version of my previously mentions “TTS and resume” script and tested it. I expect this will work for you:

alias: Auto Trigger
variables:
  media_entity: media_player.office
  temp_media_ContentId: media-source://media_source/local/Music/sfx01.mp3
  temp_volume: 0.35
  temp_duration_seconds: 15
  mediaplayer_State: '{{ states(media_entity) }}'
  mediaplayer_volume_level: '{{ state_attr(media_entity,''volume_level'') }}'
  mediaplayer_media_content_id: '{{ state_attr(media_entity,''media_content_id'') }}'
sequence:
  - service: media_player.volume_set
    data:
      entity_id: '{{ media_entity }}'
      volume_level: '{{ temp_volume }}'
  - service: media_player.play_media
    data:
      entity_id: '{{ media_entity }}'
      media_content_id: '{{ temp_media_ContentId }}'
      media_content_type: music
  - delay:
      hours: 0
      minutes: 0
      seconds: '{{ temp_duration_seconds }}'
      milliseconds: 0
  - service: media_player.volume_set
    data:
      entity_id: '{{ media_entity }}'
      volume_level: '{{ mediaplayer_volume_level }}'
  - service: media_player.play_media
    data:
      entity_id: '{{ media_entity }}'
      media_content_id: '{{ mediaplayer_media_content_id }}'
      media_content_type: music
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mediaplayer_State != ''playing'' }}'
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 6
              milliseconds: 0
          - service: media_player.media_pause
            data:
              entity_id: '{{ media_entity }}'
mode: single

Note:

  • At the beginning I made the media player entity, temp url, volume and duration as variables.
    If these are always the same, then keep them as is.
    Otherwise you can pass these as parameters to the script from the automation.

  • You have to make sure that the temp duration is enough to capture and play the stream.(this might take noticeable time in some situations). You can experiment changing this until you hit the desired duration.

Please let me know if this works for you

Kind regards,
Ghassan

3 Likes

Thank you, thank you, thank you!!! THIS WORKED AMAZINGLY!!! YOU ROCK!!!

1 Like

@ghassan another ask with this same type scenario. Rather than temporarily playing an audio file can I have my Google devices via TTS speak a message then after the message revert back to their previous playing state/music? Thank you!

Hi @techtim
Yes sure, you can use my original script you find here: How continue play the previous music after a tts voice announcement? - #19 by ghassan

Let me knw if you haver any challenges getting it to work.

Oh yes! I remember now seeing your original post. I just copied that original script into a new script in my instance and get the following error message. “Failed to call service script/1628729991857. UndefinedError: ‘tts_entity’ is undefined”. So I guess i need to customize something in it to fit my setup? Looking to use the same media player “media_player.office” as before. Then the tts google message “Hello, welcome home.”. Thanks so much for you help again.

Oh, I think I get it now… I added " tts_entity: media_player.office " to just under “variables:” and that seems to fix my issue.

1 Like

Hej @ghassan

Your script works like a charm! A billion thanks for that excellent example.
Enough of the flattering I guess, even do its meant sincerely!

I got a lot of Google Nest and Google compatible Cast Speakers.
Grouped both in the Google app and in groups.yaml …

Can I somehow just use groups with your “TTS and resume” script ?

Best regards
Jan Ø.

Hi Jan Ø,

I haven’t actually tried working with groups. But it seams like the script should work with groups too.
take a look at this this post: How continue play the previous music after a tts voice announcement? - #22 by wburgers

But I am not sure what happens if not all group members were playing the same source at same volume level, pre TTS, you won’t be able to resume different sources at different volume levels. Maybe you can use one of these as your master reference and resume to its state pre TTS.

Otherwise I’ll suggest that you change

mode: single 

to

mode: parallel 

then call the script once for each member of the group. I expect that the script will then be executed on all entities in parallel so they don’t get delayed one after another. Should be simple to try it first.

And if this works, please consider making a new script with the same parameters. The entity input parameter is the group, and the only thing this script does, is calling the original script once for each group member, using a for loop.

Make a try and let me know the outcome.
I can probably assist composing some new code if you like

Kind regards,
Ghassan

1 Like