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

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?

@Talvish thank you very much for providing this to the community.

I wanted to ask if you had a sample usage YAML of your blueprint? Meaning, a YAML script using your blueprint rather than creating it via the UI. I scrolled through this thread but didn’t see one. Apologies if I missed it.

Cheers
Matt

Hi @Talvish

Thank you very much for this contribution. I tried to do this myself months ago, but didn’t get it to function properly at all.

But as @matthewcbyington suggested, could you provide us with an example YAML on how one would implement your blueprint in practice? Thank you very much again for this!

Edit: Nevermind, I realised I hadn’t loaded the blueprint properly at all. Now it works like a charm, thank you so much for this. I will use this a lot!

Any luck, @Talvish ?

Thanks!

I tried creating the script from the blueprint, and it fails with the following:

Logger: homeassistant.components.script
Source: components/script/__init__.py:187
Integration: Scripts (documentation, issues)
First occurred: 12:22:30 (3 occurrences)
Last logged: 12:22:30

Blueprint Text-to-Speech on Sonos generated invalid script with input {}: extra keys not allowed @ data['id']. Got None

I dont think the script is importing correctly for me. What should I see?

I can see a space for the entity id(it doesnt auto populate with options? should it?)
I have tried media_player.office and it complains about that and says to try media_player_office
I have tried tts.google_translate_say in the tts service area.
Lastly there is no place to input a message and the script doesnt properly save if I hit save.

1 Like

I didn’t really do anything besides trying again 3 weeks later, and now it works. :man_shrugging:

Very odd that it was failing, but glad it is working now!

Hey @dasbooter ,

When you create the script from the blueprint, it only takes the text to speech service you want to use (defaults to Google’s since it is included by default in HA) and the language to use (defaults to en). Here is a screenshot from creating a script from the blueprint:

When you use the new script in an automation you specify the entity and what message to send. For example, this is what the Developer Tools / Services tool in HA shows when choosing a script created from the blueprint:

It doesn’t default with an entity_id because everyone’s system and names are different.

Hopefully that helps!