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

You need to use a media source URL instead:

service: media_player.play_media
data:
  entity_id: media_player.kitchen
  media_content_id: media-source://tts/cloud?message=I am very loud
  media_content_type: music
  announce: true
  extra:
    volume: 80

Oh, very nice. Could be the thing I searching for if it working with all tts platforms (google or better ms edge custom)?!

The TTS platform doesnā€™t matter and all should work similarly with this feature.

I think I got it. We must replace cloud with the tts provider name. So I Must use:


media_content_id: media-source://tts/edge_tts?message=I am very loud

If I want to play more then one announcement the announcements are cut because the script is running on. Any idea how to wait because announce: true do not change the play state? How can I calculate the delay time if I want to play a sound and then a tts?

As of right now itā€™s not possible to determine when a specific alert has finished playing. If you need to play multiple announcements in succession, youā€™ll probably need to rely on delays between service calls for now.

1 Like

As a hack you can move the announcement part of the automation to a script and make the script mode queued, then call the script from the automation. You might also want to make this as a parallel call in your automation so it doesnā€™t make other actions wait. It might be ugly but should work.

1 Like

Just wanted to say thank you for this wonderful scriptā€™ Iā€™m using it on al daily basis for almost everything that could be announced per voice message. Keep up the good work

Quick question, I have a Piper setup as well, and was wondering what I have to switch out for it to utilize that one?

service: media_player.play_media
target:
  device_id: 4be047904a638de10989b94e14210260
data:
  announce: true
  media_content_id: >
    media-source://tts.piper/?message="I am very loud"
  media_content_type: "music"
  extra:
    volume: 40

Does not seem to be working, but tts/cloud does.

If I press the button ā€œTry voiceā€ in the below window, it works, but of course outputs the sound on the device my dashboard is shown on.

Try media-source://tts/piper?

1 Like

Got it working with:

service: media_player.play_media
target:
  device_id: 4be047904a638de10989b94e14210260
data:
  announce: true
  media_content_id: >
    media-source://tts/tts.piper?message="I am very loud"
  media_content_type: "music"
  extra:
    volume: 40
2 Likes

This somehow doesnā€™t work for me, I get:

Unable to find referenced devices media_player.dining_room or it is/they are currently not available
Unable to find referenced devices media_player.bedroom or it is/they are currently not available

Iā€™m litterally just changing your device id with media_player.dining_room (and to test, also .bedroom)
If I try with a media file like an mp3, it works, also working if I use directly the TTS service.

Any advice?

1 Like

You need to use entity_id instead of device_id.

2 Likes

Yeah this was simply just me mashing something quick together in UI and yaml mode. Glad you got the right instructions.

1 Like

@bjorn.sivertsen & @jjlawren
Thank you so much to both of you! Fixed immediately! I guess was a bit too late for me to edit scripts!

Thank you for this - it works nicely on my Sonos speakers, but one thing:

If I play a long message (Whether typed or the output from an entity), the volume of the speech drops off and the music gets louder again before playback is finished. I understand min_wait fixes that, but 2 things with this:

  • It also means there is a delay before anything is said (Equal to min_wait I think). So setting it to 8 so a long message can play out means 8 seconds of silence prior to speech starting. Is there any way to avoid this?

  • Messages of unpredictable length (Like playing the weather summary from the BOM here in Australia) mean we cannot be sure when the volume needs to be ā€˜turned upā€™ again.

Is there any way to avoid this? Particularly the first point.

Thanks

@Talvish Iā€™m actually having the same issue that @vongole83 describes here. Iā€™ve been using your script for close to a year now ā€¦ and itā€™s actually always worked well except the last 4-6 weeks where Iā€™m experiencing what vongole83 is.

Iā€™m wondering if maybe the Sonos integration and/or APIs changed, since your script didnā€™t have these symptoms before? Any ideas?

Thanks in advance and appreciate your hard work.

Matt

Can anyone help with combining jinja with

media-source://tts/cloud?message=

Simple text works ok, but Iā€™m tring to convert a message that pulls current values from sensors, that used to work fine with tts.cloud_say.

I made template sensors for the messages

- sensor:
    - name: achterdeur
      unique_id: 7e8bb50d-e1e1-462b-8056-04f6baa58b70
      icon: mdi:door-open
      state: >-
        {{ [
            "Willen we alstublieft de deur dicht doen?",
            "Het is buiten nog te warm!",
            "Sluiten",
            "De deur dicht, het is te warm buiten",
            "Dicht doen!",
            "Hoeveel keer moet ik het nog zeggen, doe die deur dicht!"
            ] | random }}

And this is the action I use in my automations

action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.achterdeur
  - service: media_player.play_media
    target:
      entity_id: media_player.keuken
    data:
      announce: true
      media_content_type: "music"
      extra:
        volume: 65
      media_content_id: >
        {% set message =  states('sensor.achterdeur') %}
          media-source://tts/tts.piper?message="{{message}}"
2 Likes

Anyone have success using this with the NabuCasa cloud say? I canā€™t figure out which language to specify.