My setup to make tts announcements with sonos

Hi everyone, first project shared here :slight_smile:
Today I was working on creating an easy script to make voice announcements without interrupting the music.
I use the sonos.snapshot and restore service, which in my experience needed some time to properly save the queue therefore the delay.
Therefore I created 2 script, which could be combined into one big one, but i just kept it separate for easier debugging.

play_tts:
  alias: "Spiele TTS auf Sonos Gerät"
  description: "Unterbreche Musik und spiele tts ab, aber mach danach wieder weiter"
  fields:
    player:
      description: "sonos device to play on"
      example: "media_player.daniel"
    tts:
      description: "tts service to run"
      example: "tts.marytts_say"
    message:
      description: "Message to be played with tts"
      example: "Welcome to TTS"
  sequence:
    - service: sonos.snapshot
      data:
        entity_id: "{{ player }}"
    - delay:
        seconds: 2
    - service: script.play_notification
      data:
        player: "{{player}}"
        tts: "{{tts}}"
        message: "{{message}}"
    - wait_template: "{{ is_state('script.play_notification', 'off') }}"
    - service: sonos.restore
      data:
        entity_id: "{{ player }}"

Is used with a local bling.mp3 to grab attention of the announcement

play_notification:
  alias: "Spielt einen sound und nachricht"
  fields:
    player:
      description: "sonos device to play on"
      example: "media_player.daniel"
    tts:
      description: "tts service to run"
      example: "tts.marytts_say"
    message:
      description: "Message to be played with tts"
      example: "Welcome to TTS"
  sequence:
    - service: media_player.play_media
      data:
        media_content_type: music
        media_content_id: https://db-homeassistant.caserio.net/local/bling.mp3
        entity_id: "{{player}}"
    - delay:
        seconds: 1
    - service: "{{ tts }}"
      data:
        entity_id: "{{ player }}"
        message: "{{ message }}"
    - wait_template: "{{ is_state(player, 'paused') }}"

Maybe it can help anyone.

1 Like

You might want to look into this:

It works really well and doesn’t need any snapshots/restore/resetting the queue since plays over the currently playing music.

That actually looks very interesting.
Propably not a solution for me, because I don’t wanna use the Cloud and have only S1 devices, but still a good alternative to have up here.

1 Like

Did you stick with this method or move to the cloud based API?

Sorry for showing my noobishness but How do I use your example script? Are you defining the variables in the first part under fields? like should my script be as an example:

  fields:
    player: media_player.kitchen