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

Kids woke up so tested with Pandora. I started a station on Pandora and then ran the script. The announcement played but the music stopped. Here is the trace -

Choose: Default action executed

washer is done (script.1654728782683) started
(script.1654728782683) turned on
(script.1654728782683) turned off
(media_player.sewing2) turned playing
1 second later

(media_player.sewing2) turned paused

I’m running 2022.5.x still. I usually wait a bit to upgrade and there were some Sonos integration changes (and reverts apparently). I’ll upgrade shortly and see if my script behaves differently in 2022.6.x.

Update: I just upgraded to 2022.6.4 and I tested with Spotify and TuneIn, they worked. I had a friend test Pandora previously, so I’ll see if still works for him.

Works great for me on 2022.6.4 too. With Spotify and TuneIn and even normaal tv.

Thanks for checking Martijn. Definitely a curious one.

@billraff, I may be able to build a work around this weekend, but want to do some digging first. Any more information to help?

  1. The script/automation you wrote that calls my script, does it happen to do something interesting with the Sonos after, or perhaps force quit (stretching here, but figured I may as well ask :slight_smile: )?
  2. Is your Sonos an S1 or S2 system (not sure if that makes a difference, but mine are S2).
  3. What type of speaker? I might be able to test on the similar speaker. My testing was on a Beam, Ones and Fives (grouped and ungrouped), but I can test on an Arc, Roam, Move and Play:3s as well.

Quick update: I spoke to my friend who uses Pandora and they had no problems either. If you have a chance to answer the questions above I’ll do a bit more digging.

Would not be surprised to find my environment or usage is unique since others are not seeing the same behavior. No need to build anything special for me. The automation I wanted to use your script on is from a blueprint found here on this site as well.

Basically it sends a notification when our washer and dryer is finished. It knows this since I can monitor
electrical usage from a Sonoff outlet the dryer and washer each have.
But for testing your script this morning I simply used the developer tools. Here is the script:

All my speakers are S2 but they are also running on SonosNet (WM:0) to try and cut down on the amount of wifi traffic. So there is a Sonos Boost in the picture. I have a total of 12 Sonos products. The ‘cave1’ I used in the Developer tools test is actually a pair of Sonos Ones that have been joined. I tested using just a single speaker in the dining room and saw the same result.

(script.1654728782683) turned on

Call service tts.google_translate_say

(script.1654728782683) turned off
(media_player.dining_room) turned playing
2 seconds later

(media_player.dining_room) turned paused

Finished at June 12, 2022, 7:51:02 AM (runtime: 0.08 seconds)

Somehow it wants to pause after playing the announcement.

The Sonos integration was one of the first things I set up in HA. My whole Home Assistance adventure was because I bought a Sonos Port and hooked it up to my Yamaha AVR and wanted to turn on the AVR once Sonos started playing music and set the AVR to the proper input/output. Could have bought an Onkyo AVR that ‘works with Sonos’ to do that but HA was much cheaper. I think tomorrow I’ll remove the Sonos integration and re-add to freshen things. I’ll report back afterwards.

From the screenshot you sent it isn’t calling my script but is calling tts.google_translate_say directly. So just to confirm, when you said you tried using the dev tools, did you …

  1. Get some music playing
  2. Go here: Developer Tools – Home Assistant
  3. Fill-out the form (the script name is likely script.sonos_play and will show Script: Text-to-Speech on Sonos)
  4. Submit

Example:

Well, that’s embarrassing! You’re right I wasn’t calling your script. Everything works great when I do Computers are so literal! Thanks for the blueprint and the help.

No problem :slight_smile: . I’m glad I could help! Anything else you are looking for, let me know.

Ha, I had no clue. I thought you based your script of my original that’s why I was so confused it wasn’t working. It looked kinda the same but I’m no programmer.

Sadly can’t just edit the javascript "case ‘sonos’ ", it seems they compiled the whole thing in 1 .js file.

It’s an edge case anyway, script works fine in automations I just can’t type in my dashboard and press send to play a self typed message.

Glad it is working for you otherwise.

I was doing some research on how tts on sonos works. i could not find any that works for me. i tried everything however i still could not solve this issue. when searching, i found your blue print but there is not instruction how to use it. i am a beginner with home assistant. Can you please help how i can use your blue print. thank you

On duration, instead of guessing you can use this

delay: >-
  {% set duration = state_attr(sonos_entity, 'media_duration') %} {% if duration
  > 0 %} {% set duration = duration %} {% endif %} {% set seconds = duration %
  60 %} {% set minutes = (duration / 60)|int % 60 %} {% set hours = (duration /
  3600)|int %} {{ [hours, minutes, seconds]|join(':') }}

It was working consistently for me until last week, when it started cutting off early for some reason. I added a 5 second delay prior to this command in case the media_duration attribute wasn’t loaded yet. probably need to make a default too in case it’s gone by the 5 second mark.

Hey krazykaz83. Did you try setting the optional max_wait? It is there as a fail-safe to wait for the playing to complete,

Hey there, what are you looking to do in particular?

Fantastic work, @Talvish !

I did have a question regarding buffering and pausing audio being played on the Sonos prior to the voice message.

With our setup, the volume of the Sonos speakers when playing music or TV in relation to the volume of the spoken voice (via your blueprint and the subsequent script) is vastly different. We need to set the volume of the spoken voice pretty high. And while we can do this, we’re still hearing the existing sound coming out of the Sonos (music, for example) ramp up briefly before the music mutes and the voice starts.

Any suggestions to remedy this?

FWIW, our script Mode is “Single”, and our maximum wait is 6 seconds.

Thanks!

Hey @mkammes, glad you are enjoying the script!

What I’ve noticed is that the state HA thinks something is in is not necessarily the actual state. In this particular case I ask music to stop playing if music is playing when the script starts. I then wait for the music to stop but clearly HA is coming back too early.

I can address this by adding a configurable delay for after playing. Unfortunately I’m not in position to do this at the moment, but I’ll try to soon.

1 Like

Thanks so much, @Talvish !

Hi Talvish, could you explain a bit further? Do you mean instead of doing the delay calc i shared?