you have to save the script into a file example set_state.py and save that file in the python_scripts folder under the configuration directory. if you don’t have that folder already then create it.
and in the configuration.yaml file you have to add the line:
python_script:
then restart HA.
Let me know when you get that far, then we can proceed from there .
Eventually I got the script to work, however I changed it quite a lot. As I am using Sonos for my TTS, fortunately there is an option to snapshot most settings in one go and restore the settings after the TTS message. By using sonos.snapshot it remembers also how far along in a song you were and at resume does not start the song from the beginning. I got the wait_template working, although I still use a small delay before the resume.
@ghassan ,
My main challenge was that I could not get the original script to remember how far along in a song the player was. After the TTS it would start the right track, but it would start from the beginning. The snapshot solution makes the script very compact, but it also makes it sonos specific and not well suited for a generic solution.Come to think of it, I didn’t test yet if volume settings are also ‘snapshotted’. In that case you could even reduce it further. KR, Tankdoz
I’m looking for long time for an option to use TTS with my HomePod Mini. As HomePod Mini is now available as an Mediaplayer i can use TTS. I tried to use the provided Script-Code:
Sorry I’m chiming in late on this thread but I just tried to set this up with the Sonos Ones running Alexa. The announcement works fine but nothing resumes playing after the announcement. Any insight?
Were you able to get this script to work for youtube music. If so can i see your final script. Side not @ghassan does this script support resuming music on previously playing speakers or just resuming music on the speakers used for tts. For example if im listening to music on media_player.xyz, media_player.abc, media_player.lol, and media_player.trx and i get and anoucement on xyz & and abc will all players resume music afterwards?
my code work now in the 2022.9
resume playing in my 3 media player , each one the independent player previous to the tts.
in my case , the tts is play y all the media players. you can add a variable indicate the media player you want to use to the tts, or create a script to each media player.
my code include a day/night time select ,. to change the volume off TTS accord to the hour.
regards.
@ghassan Thank you a lot for this work. Works very well for me and solved an issue!
I have one question:
I’m using the script to control a google nest hub with a display and I’m using 2 modified versions of your script to 1) resume after tts announcement and 2) resume after streaming videos from IP cameras (e.g. when someone rings the doorbell it streams the campera for 30 seconds and than resumes). This works fine in case one is playing music etc. However, it doesn’t stop the media_player (state: off) when it was off prior to the tts announcement or stream. Since I’m using the hub with display it needs to stop media_player at the end if it was stopped/off prior because otherwise it will not return to the photo frame mode of the nest hub.
I tried to solve this by modifying the end of the script but this does not work:
In case of an tts announcement it ends with the media_player state “idle” and in case of the stream (code shown above) it keeps showing the stream without stopping it (even if the same script returns to the playing source succesfully if it was playing something before).
Any suggestion how to turn off the media player in the end in case it was turned off before starting the script?
@ArielBaravalle
Thank you for helping!
Tried this but it regrettably didn’t change the outcome: Stream keeps streaming and after TTS status is “idle” instead “off”…
Problem solved. When starting from “off” state media_player entities, at least with google devices, they do not provide volume information in their states. So the script dies when re-setting the volume (not defined in the variable after the media playback or TTS announcement. One can solve this by setting a fixed volume in that case…
Could you share an example of how you use this. I tried using it to set several attributes of my media player but i doesn’t seem to be registering when when used in an automation. When i call the service manually the attributes do indeed change but the media player becomes unresponsive. I am trying to get this to work with You Tube music and google home speakers.
Hi @DuPunk,
Please add more details:
Do you have a speaker group, and initially listening to same music on all speakers?
Do and you alter the speaker group by the script or you call individual speakers?
Here is the script i am trying to get to work. This weekend i will post some before and after states of the media player when running the script. I appears to be functioning for media started in home assistant but most the time my music is cast from a phone via youtube music and although it logs all the state attr correctly it seems to be unable to restore all states and then resume playback