SONOS TTS Script

@Litux Interesting… So your speaker id shows up on http://<your_ha_location>:8349/api/allClipCapableDevices correct?. I haven’t had a chance to play around with those speakers as I don’t have them but everything I have seen points to the fact they should work. Also be sure that you are using the speaker id and not the household id (which also appears on that page). Try a message with no spaces (e.x. Hello) too. If that works it could be a string url encoding issue. Also that volume is way too high if any of the steps above end up working. I’d drop that down to around 35.

EDIT: I looked at the documentation. I think you’re for sure accidentally using the sonos houshold id (starts with Sonos_) instead of an individual speaker id.

OK, that adress only replay with:

{
	"success": true,
	"households": {
		"Sonos_VaZf8dVYuS0Il7C91111111111.Lq1jWwFZdL41111111111": []
	}
}

I changed some number for security reasons.
As you can see only the househols ID is listed.
I have check for updates, but there none. I have also re-do the auth, I restarted the server…
Any ideas why the speaker is not detected?

@Litux Ah I should have read Sonos’ page more closely: https://developer.sonos.com/reference/control-api/audioclip/

They say:

It will also work on the SYMFONISK table lamp with WiFi speaker and SYMFONISK WiFi bookshelf speaker in the next software release

So I think you just have to wait until the next Sonos update happens. After that you’ll see more id’s appear inside that those []. You’ll want to use those

1 Like

Great! At least we (mostly you :slight_smile: )found an answer.
I can sit and wait… or continue with other projects…
Anyways, THANKS

1 Like

SYMFONISK now working

As is often the case, HA develops and scripts described by users no longer work due to syntactic changes. So here is a working version (Oct 2019) of the great script developed by @domlen and shared with us a couple of years ago.

test_tts:
  alias: Test for TTS
  sequence:
    - service: script.turn_on
      entity_id: script.say
      data:
        variables:
          where: 'office'
          what: 'Test.'


say:
  alias: Sonos Text To Speech
  sequence:
  - service: sonos.snapshot
    data_template:
      entity_id: "{{ 'media_player.' ~ where }}"
  - service: tts.google_translate_say
    data_template:
      entity_id: "{{ 'media_player.' ~ where }}"
      message: "{{ what }}"
  - delay:
      seconds: 1
  - delay: >-
      {% set duration = states.media_player[where].attributes.media_duration %}
      {% if duration > 0 %}
        {% set duration = duration - 1 %}
      {% endif %}
      {% set seconds = duration % 60 %}
      {% set minutes = (duration / 60)|int % 60 %}
      {% set hours = (duration / 3600)|int %}
      {{ [hours, minutes, seconds]|join(':') }}
  - service: sonos.restore
    data_template:
      entity_id: "{{ 'media_player.' ~ where }}"


3 Likes

entity_id: “{{ ‘media_player.’ ~ where }}”

Hey @ianadd, is the delay working as applied in your script calling different speakers via automations?

@Coolie1101, To be honest i did my testing on the old play:3 in my office and it behaved as expected, resuming after saying what i asked.
As you have asked, i will do some detailed testing. I will vary the length of what is said and do so on other speakers that i have. I will then report back.

Appreciate it.

For me I have to specify the media player, otherwise it throws an error when the script is activated.

delay: >- {% set duration = states.media_player.living_room.attributes.media_duration %}

Paste here your call to the script as well.

      - service: script.turn_on
        entity_id: script.sonos_tts
        data_template:
          variables:
            where: "living_room"
            what: some message

Ok, I have some feedback.
If I use my Sonos app to start music on a single speaker OR on my soundbar + two rear speakers for example then it works as intended.
If I ask google to play music on the same speakers then the script errors and the music does not resume. Well this seems fairly reasonable to me, though disappointing.
What is your experience, @Coolie1101 ??

As google can now play music to sonos speakers some head scratching is required, by someone more experienced than me, to see if a script can work that out and do something different.

Thanks for posting this! Works great for me!

Sorry for the delayed response, my reference was for the delay without specifying the entity, and I find the following to not work 100% of the time.

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

Either way the delay seems to timeout before the announcement completes.

I have had the SONOS TTS installed and working via HASS.IO it was working fine until a month or so ago. To resolve I have tried to reinstall but now it doesn’t run, I get the following in the logs:

[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] done.
[services.d] starting services
[services.d] done.
ifelse: fatal: unable to exec ./run.sh: Exec format error
[cmd] ./run.sh exited 126
[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-finish] sending all processes the TERM signal.

Any ideas what’s going on?

Thanks Gary

@kevinvincent thanks a lot for your add on!
Unfortunetly I’m stuck with the “Perform auth flow” step… When I try to reach https://my.hassio.ip:8349/auth I always get an ERR_CONNECTION_REFUSED and I’m not able to login. I tried all the possible IPs/URLs that I use to access HA, but none of them worked (yes, I do use https).

This is the addon log (it reports the same error of @connoleg above) :

[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] done.
[services.d] starting services
[services.d] done.
ifelse: fatal: unable to exec ./run.sh: Exec format error
[cmd] ./run.sh exited 126
[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-finish] sending all processes the TERM signal.

Any ideas?
Thanks!

@asp @connoleg I haven’t updated my HA in awhile so might be something weird with the latest Hass.io. I’ve updated the addon with my hunch of what it can be. Can you try reinstalling?

1 Like

Bingo! It’s working now :slight_smile: Thanks a lot for the super quick reply!

One request, would it be possibile in the future to use the Google Cloud TTS? It’s much more smooth and realistic compared to the Google Translate TTS (try it!)