Script for Sonos Speakers to do Text-to-Speech and Handle Typical Oddities

Hey @JamesLodge, so looks like you wanted something to disrupt playback temporarily by playing something else? You could definitely combine this script and the Play Media script. This one has a few more safeguards in place.

Out of curiousity, what is your use case? I liked @saint’s notion of a chime. I’m still considering making an update here to support that but given how hectic things are this time of year for me, may not make it over the next 3 or 4 weeks.

OH and looks like you’re new to HA (at least the community site), welcome!

I would love the ability to add a chime, my girlfriend jumps everytime it starts speaking out of the blue. :smiley:

1 Like

Thanks @Talvish, much appreciated. Yes I’ve been lurking for a while and had HA running for about 8 months now.

Yes my use case is actually for the kids (or possibly this big kid) and similar to @saint. Not sure if any of you are from the UK, but here on the BBC we have a children’s program called Justin’s House and as part of that, when someone rings the door they sing “who’s at the door, who’s at the door, someone better answer it”, that doesn’t do it justice, but the kids love it. If you’re interested here @ 14:22 So I have an mp3 of that stored locally on HA server. It plays when someone presses our Hikvision door station. It works brilliantly thanks to your amazing TTS script which I modified, but would be great to have it in all rooms. As current setup only for our lounge ARC. A you say, time is limited this time if year. I’ll have another look if i get chance between HA and put togerther some pixel LEDs/WLED for the tree, oh an actual real work. Too much cool stuff and not enough time.

Thanks again for your great work.

Thanks very much for creating this. I’m using this for many announcements in the house and love it.

Recently I ran into a small issue. When the speaker is muted in the SONOS app, the automations would not play the announcement audibly. You can see the announcement being played in the SONOS app, also the volume being adjusted, but the speaker stays muted.
Any way to fix this?

I’d like some guidance for a use case that I am working on. My Sonos system consists of speakers in every room and also the garage and back porch. I generally keep all the indoor speakers grouped together and this blueprint has served us well in making announcements when the washer and dryer, etc are done.
Now my use case. One of my Sonos products is a Sonos Port. Basically, the port is connected to an AVR that has some speakers and I use this in my office when listening to music, watching tv or playing games. I have an automation for that receiver that basically switches it to the correct output ports for the Sonos Port whenever the Port goes from Paused to Playing. The issue I’d like to work around is when I am watching tv or playing a game and another automation runs to make the announcement that the washer is done then my receiver switches to the Port output and I lose the video to the tv.
Is there a way that I can point my automation running this script to a Sonos group I have created that doesn’t contain the Sonos Port? That way the announcement is made through the rest of the house and doesn’t interrupt the tv in my office.

In the image I have a group named Announce I’d like to use.
Thanks in advance for any ideas on how I can maybe accomplish this.

Great script, thanks for all the hard work. One question: like one other person on this thread, speakers are not resuming when playing Spotify (started using Spotify phone app or when started using the Sonos app). Two paired speaker sets, all Play:1. If you hit resume on a speaker it replays the TTS. Tested in developer tools, same issue.

And did some more testing. It will eventually resume (over 7 minutes later). Note this was with:

service: script.text_to_speech_on_sonos
data:
  entity_id: media_player.master_bedroom
  message: test
  volume_level: 0.2
  min_wait: 4

Had to bump min_wait to 12, now it works.

Hi @Talvish
Thanks for a great blueprint script.
Have you every used it to notify multiple Sonos devices at the same time, without grouping them?
I setup an automation with the actions running in parallel, but still only the first listed Sonos device plays the TTS.

Hey @goonerx1 (and congrats on your first post).

If you have a single call to the service, and don’t want to group, you can put more than one media_player in the entity_id. For example:

Does this suffice for you? Given how you worded the question, I suspect not, but figured I’d show the simple solution first.

Ya, the min_wait solves oddities … the Sonos HA integration frequently reports state changes/information incorrectly. When it seems like the script isn’t resuming it is because it is actually incorrectly using the remaining length of time of the previously playing song as the amount of time to wait before resuming (because of the HA integration state issue). Using min_wait helps address this.

I am going to look at my script to better address this so the issue will occur with less frequency (and perhaps won’t occur at all); I could argue my script is trying to be too smart and getting caught with the state change integration oddity.

Hey @billraff , been awhile!

Unfortunately, those Sonos group names aren’t available in HA … but that doesn’t mean there isn’t a potential workaround. Assuming I’m interpreting what you’re saying, I would have my automation check the receiver to see if it is set to the TV / game input and if so send the announcement to one set of speakers, and if not, send to another set of speakers.

Now unfortunately, this blueprint doesn’t allow you to group speakers automatically (like my music blueprints do), so either you could code the group and ungroup yourself, or you could comma separate the entity_id (see my comment to @goonerx1 … though the announcement playback may not be sync’ed correctly across the speakers).

If you would like I can look to add speaker grouping/ungrouping as part of this script. I’d have to test that sonos.restore will put groups back together, otherwise my script would need to (which would be a pain if multiple groups are involved).

That help?

1 Like

Glad it has been helpful!

Huh, just tested and confirmed I see the same thing … so very strange (given you can see the volume go up and back down). I’ll have to do a bit of digging.

Thanks for the information Talvish. I tested the service you referenced above and ran into the same issue where, since my Port is in a group with almost all speakers, the automation detects Sonos Port has gone from Paused to Play and so the input changes.
I was looking at what appears to be a new feature that would probably make this easier but it doesn’t appear to be fully ‘baked’ yet.

Announce = True doesn’t seem to work?

I’ll play around with my automation to see if I can ungroup/group and the ungroup/regroup the speakers.
Is there a way maybe to create an entity of the group I want to use and point the blueprint to that entity? At one point of playing around with the HASS Music Assistant I had that entity there but for some reason wasn’t able to select it.

HASS Music Assistant
First world problems :wink:
Thanks Again!

Ya, I think you could create something that holds those speaker values as state in HA … but I’m going to do some playing today and see if I can do the grouping automatically so then you would just need to figure out how to detect the source on your receiver and then make two different calls to my script. I’ll keep you posted.

I’m also going to address what @0_0 pointed out (turns out the mute button state is independent of the volume BUT I did confirm I can control that) and may change some wait handling.

1 Like

Just posted an update:

  • @0_0 , fixed your issue … turns out modifying volume doesn’t unmute speakers, so now I forcibly unmute all speakers in the group and that is done independent of speaker volume settings. Let me know if that works for you.
  • @val1, changed my wait handling so there shouldn’t be unexpected delays when resuming music. I’m also not sure if you will need the 12 second delay anymore, you can likely make it smaller, but let me know how it goes. As part of this change I have deprecated max_wait and in a future update will eliminate the max_wait entirely (didn’t now so it wouldn’t break any automations and blueprints)
  • volume, if given, is applied to all speakers in the group

@billraff , I haven’t added grouping as a parameter because it is a tad complicated to handle generically though I’m still looking at it. It needs to check all speakers provided as a parameter to see if they are in separate groups and if so do snapshots/restores for each of group.

OH, also one comment regarding not resuming when the music was started from the Spotify mobile app … this appears to be a Sonos or Sonos HA integration issue. I did some testing and digging today and added some comments to the Github issue found here.

Thnks @Talvish ,

Brilliant, works perfectly now with speakers muted. Thanks very much for the quick help.

1 Like

Didn’t realise you could do it this way, thanks for the idea @Talvish . I’ll give it a go after work :slight_smile: