Dynamic delay for TTS announcements on Sonos speaker

I just got my first Sonos speaker yesterday and happily integrated it to Home Assistant.

Now I am trying to get announcements working via TTS on the speaker. So basically:

  1. saving a Sonos snapshot
  2. making the announcement via tts.cloud_say
  3. restore the Sonos snapshot

At the moment I’m testing this in Node-Red and basically everything works.

sonos.snapshot -> tts.cloud_say -> delay -> sonos.restore

The problem is that I need to use a static delay for the duration of the announcement. But I would like to use something dynamic, so that I can create dynamic text strings and after the announcement is finished, the shapshot will be restored.

I found the script that you can trigger via an automation, but this also uses a static delay.

The idea is to use the “wait” action to trigger the snapshot restore after the TTS service is finished. The problem is, I can not find any entity or attribute where the status reflects the activity of the TTS.

Any idea if there is any status that I can use for this? Or is there maybe a totally different approach that you use to send announcements to the speaker and switch back to the initial source?

Can’t help with node red but in home assistant I’d just use a wait_template instead of a fixed delay.

wait_template: "{{ not is_state('media_player.your_sonos, 'playing') }}"
3 Likes

Hi Tom,

This is great…this was the right input. Sometimes it can be so easy, and you don’t see it :sweat_smile:

After the TTS is done the media_player entity changes to paused…this I can use to trigger the snapshot restore.

Thank you so much :pray:

1 Like

Would you be able to share how you did this in node red? Trying to do the same…

Hi, I ended up with having a script in HA which I trigger from Node-Red

sonos_say:
  alias: Sonos TTS script
  sequence:
  - service: sonos.snapshot
    data:
      entity_id: '{{ sonos_entity }}'
  - service: sonos.unjoin
    data:
      entity_id: '{{ sonos_entity }}'
  - service: media_player.volume_set
    data:
      entity_id: '{{ sonos_entity }}'
      volume_level: '{{ volume }}'
  - service: tts.cloud_say
    data:
      entity_id: '{{ sonos_entity }}'
      message: '{{ message }}'
  - delay: 00:00:01
  - wait_template: '{{ is_state(sonos_entity, ''paused'') }}'
  - service: sonos.restore
    data:
      entity_id: '{{ sonos_entity }}'

In Node-Red then I can prepare any text in msg.payload (here I just used the “change node”)

Then I use the “call service node” to trigger the script

In the data field you can specify the parameters

1 Like