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?
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.
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.
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.
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
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.
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?
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… =/
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.
@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!
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.
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