Sonos setup in Hassio 0.93.2

Hi All

**Decided to create a new thread taking one issue at a time :slight_smile: **
So I finally got the new Sonos setup working, but I still need a little help fixing a couple of things!

First the ground configuration:

script:
  sonos_say:
    alias: "Sonos TTS script"
    sequence:
     - service: sonos.snapshot
       data_template:
         entity_id: "{{ sonos_entity }}"
     - service: sonos.unjoin
       data_template:
         entity_id: "{{ sonos_entity }}"
     - service: media_player.volume_set
       data_template:
         entity_id: "{{ sonos_entity }}"
         volume_level: "{{ volume }}"
     - service: tts.google_say
       data_template:
         entity_id: "{{ sonos_entity }}"
         message: "{{ message }}"
     - delay: "{{ delay }}"
     - service: sonos.restore
       data_template:
         entity_id: "{{ sonos_entity }}"

Then the scripts for the Group, Ungrouping and sound level (They also work, individually)

  sonos_group_all:
    alias: "Sonos group all"
    sequence:
     - service: sonos.join
       data:
         master: media_player.sonos_koekken
       entity_id:
         - media_player.sonos_sovevaerelse
         - media_player.sonos_thomas
         - media_player.sonos_victoria

  sonos_ungroup_all:
    alias: "Sonos ungroup all"
    sequence:
     - service: sonos.unjoin
       entity_id: all

  sonos_set_volume:
    alias: "Sonos set volume"
    sequence:
     - service: media_player.volume_set
       entity_id:
         - media_player.sonos_koekken
         - media_player.sonos_sovevaerelse
         - media_player.sonos_thomas
         - media_player.sonos_victoria
       data:
         volume_level: 0.20

Now how to combine the above scripts:

1. script.sonos_ungroup_all - Clear existing groups are in place
2. script.sonos_group_all - Group my speakers **
3. script.sonos_set_volume - Set volume for all speakers

I would like to include my google mini speaker: media_player.stue but sofar that breaks my sonos setup

**** in the below automation: which works but need the actions of the scripts****

automation:
# Time is now 18:00 from Sun to Fri on Sonos
  - alias: Spisetid klokken er 18:00 Sonos
    hide_entity: false
    trigger:
      - platform: time
        at: '18:00:00'          
    condition:
      - condition: and
        conditions:
        - condition: time
          weekday:
          - sun
          - mon
          - tue
          - wed
          - thu
          - fri
# Betingelse en af vores børn skal være hjemme
        - condition: and
          conditions:  
        - condition: state
          entity_id: sensor.victoria_presence
          state: 'home'
      - condition: or
        conditions:
        - condition: state
          entity_id: sensor.thomas_presence
          state: 'home'
    action:
     - service: script.sonos_say
       data_template:
         # group.sonos_players ?
         sonos_entity: media_player.sonos_koekken, media_player.stue, media_player.sonos_thomas, media_player.sonos_victoria
         volume: 0.5
         message: "Victoria og Thomas Nu er det spisetid klokken er {{ now().strftime('%H %M') }}"
         delay: '00:00:05'

Another and last issue is how to include the TTS in a card in lovelace like I had it before?

input_text:
  announce_sonos:
    name: Sonos TTS
    initial: Some Text

automation: (THIS DOESN'T WORK...)
# Announce TTS input over Sonos
  - alias: Announce TTS Input Over Sonos
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_text.announce_sonos
    action:
     - service: script.sonos_say
       data_template:
         sonos_entity: media_player.sonos_koekken
         volume: 0.5
         message: "{{ states.input_text.announce_sonos.state }}"
         delay: '00:00:05'

This worked before but with the new syntax I cant get it working (Above work in progress).

This is actually already there hidden as a standard under the configuration “group.sonos_players” :
But how to get that on the dashboard in a card?

Hope someone can share some light on this?
Maybee even a simpler way of doing this?

Best regards
Casperse

3 Likes

Thank you for sharing this, casperse :grinning: :+1:t2:
It really made my day!

Did you eventually get this all to work, @casperse?

Yes most, except the " Another and last issue is how to include the TTS in a card in lovelace like I had it before?" but since that card is in the default card I can live with it :slight_smile:

Voice notification over the speakers when the kids get home works perfectly…
Hope you got things working to?

I am currently moving 80% of my setup to Smartthings so I have a mix of the two
(I had to much work every time things broke)

Utfortuenatelly not… yet.
Seems to be an issue with the /scripts mapping which is defined as script: !include_dir_merge_list scripts/ in configuration.yaml.

Somehow I cannot get te scripts in the /scripts folder to run…
SOLUTION: scene: !include_dir_merge_list scenes/

B.T.W. do you mind sharing your final automation and Lovelace configuration?

Hei, @casperse
I have managed to get it working. And I have enclosed it all in one package.

I also figured out that the Sonos_entity in the data_template cannot be set to all, but must include a list of every speakers to work. So even how much I would like this to work…

data_template:
  sonos_entity: all

It seems to only work like this:

data_template:
  sonos_entity: media_player.sonos_kjokken, media_player.sonos_stue, media_player.sonos_soverom, media_player.sonos_bad, media_player.sonos_kontor, media_player.sonos_lekerom

Here is my Sonos package
NOTE: I have added the sonos_play: script which takes a known music file as argument instead of a text string. Then I can call whatever music file I want in the same wrapping ensuring the Sonos state is preserved, and restored.
I use it as a doorbell chime.

homeassistant:

input_text:
  send_til_sonos:
    name: Sonos TTS
    initial: Some Text

automation:
    # Announce TTS input over Sonos
  - alias: Send TTS-tekst til Sonos
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_text.send_til_sonos
    action:
     - service: script.sonos_say
       data_template:
         sonos_entity: media_player.sonos_kjokken, media_player.sonos_stue, media_player.sonos_soverom, media_player.sonos_bad, media_player.sonos_kontor, media_player.sonos_lekerom
         volume: 0.5
         message: "{{ states.input_text.send_til_sonos.state }}"
         delay: '00:00:05'

###########################################################################################
#   https://community.home-assistant.io/t/sonos-setup-in-hassio-0-93-2/120345
#
#   - service: script.sonos_say
#     data_template:
#       sonos_entity: media_player.sonos_kjokken, media_player.sonos_stue, media_player.sonos_soverom, media_player.sonos_bad, media_player.sonos_kontor, media_player.sonos_lekerom
#       volume: 0.5
#       message: "Victoria og Thomas Nu er det spisetid klokken er {{ now().strftime('%H %M') }}"
#       delay: '00:00:05'
#
#     #### For sonos_play:
#     sonos_media_file: http://192.168.1.11:8123/local/doorbell.mp3
#     #sonos_media_file: /local/doorbell.mp3
#
###########################################################################################

script:
  sonos_say:
    alias: '[Sonos] - TTS script'
    sequence:
      - service: sonos.snapshot
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: sonos.unjoin
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ sonos_entity }}"
          volume_level: "{{ volume }}"
      - service: tts.google_say
        data_template:
          entity_id: "{{ sonos_entity }}"
          message: "{{ message }}"
      - delay: "{{ delay }}"
      - service: sonos.restore
        data_template:
          entity_id: "{{ sonos_entity }}"


  sonos_play:
    alias: '[Sonos] - Play audio file'
    sequence:
      - service: sonos.snapshot
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: sonos.unjoin
        data_template:
          entity_id: "{{ sonos_entity }}"
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ sonos_entity }}"
          volume_level: "{{ volume }}"
      - service: media_player.play_media
        data_template:
          entity_id: "{{ sonos_entity }}"
          media_content_id: "{{ sonos_media_file }}"
          media_content_type: 'music'
      - delay: "{{ delay }}"
      - service: sonos.restore
        data_template:
          entity_id: "{{ sonos_entity }}"

  sonos_group_all:
    alias: '[Sonos] - group all'
    sequence:
      - service: sonos.join
        data:
          master: media_player.sonos_kjokken
          entity_id: all

  sonos_ungroup_all:
    alias: '[Sonos] - ungroup all'
    sequence:
      - service: sonos.unjoin
        entity_id: all

  sonos_set_volume:
    alias: '[Sonos] - set volume'
  # description: Set volume for all speakers
    sequence:
      - service: media_player.volume_set
        entity_id: all
        data:
          volume_level: 0.20

  sonos_morning_volume:
    alias: '[Sonos] - morgenvolum'
    sequence:
      - service: media_player.volume_set
        entity_id: 
          - media_player.sonos_kjokken
          - media_player.sonos_stue
          - media_player.sonos_soverom
          - media_player.sonos_bad
          - media_player.sonos_kontor
          - media_player.sonos_lekerom
        data:
          volume_level: 0.20

  sonos_morning_radio:
    alias: '[Sonos] - Morgenradio'
    sequence:
      - service: script.turn_on
        entity_id:
          - script.sonos_morning_volume
          - script.sonos_group_all
      - service: media_player.select_source
        entity_id: media_player.soverom
        data:
          source: "P4 Radio Norge"

  sonos_test_tts:
    alias: '[Sonos] - Test av TTS'
    sequence:
      - service: script.sonos_say
        data_template:
          sonos_entity: media_player.sonos_kjokken, media_player.sonos_stue, media_player.sonos_soverom, media_player.sonos_bad, media_player.sonos_kontor, media_player.sonos_lekerom
          volume: 0.5
          message: 'Dette er en testmelding. Den sendes til alle SONOS-høytalere. Og pauser i 5 sekunder.'
          delay: '00:00:05'