#Join/unjoin the sonos speakers

1 messages · Page 1 of 1 (latest)

copper axle
#

I have several Sonos speakers in the home. Integration overall works very well, but I'd like to add the possibility to have a button in each room, that would trigger the playing. This works so far with a script.
The problem arises when first speaker starts playing and another joins, unjoining the first one will kill the complete system.

This is the script I use to execute from the automation depending on the room. I pass the data via parameters.

I've read this where it "seems" to work, but I can't make it work on my side. Any idea how to keep music playing if first (master) speaker tries to unjoin?

https://community.home-assistant.io/t/sonos-media-player-unjoin-stops-music-if-master-player/536090/8

(code too big, pasting as a first comment)

#
alias: "Sonos: Control room speaker"
description: >-
  Toggle play/stop or join Sonos group depending on state for the player for a
  given room
fields:
  player:
    description: The Sonos player entity_id
    example: media_player.era_100_office
    default: media_player.era_100_office
sequence:
  - variables:
      active_player: |
        {% set active_players = expand('media_player.group_room_speakers')
            | selectattr('state', 'eq', 'playing')
            | map(attribute = 'entity_id')
            | list %}
        {{ active_players[0] if active_players | count > 0 else none }}
      group_members: |
        {% if active_player %}
          {{ state_attr(active_player, 'group_members') }}
        {% else %}
          []
        {% endif %}
  - choose:
      - conditions:
          - condition: template
            value_template: >
              {{ state_attr(player, 'group_members') | length > 1 or
              is_state(player,'playing') }}
        sequence:
          - action: media_player.unjoin
            target:
              entity_id: "{{ player }}"
          - action: media_player.media_stop
            target:
              entity_id: "{{ player }}"
      - conditions:
          - condition: template
            value_template: "{{ not active_player }}"
        sequence:
          - action: media_player.play_media
            target:
              entity_id: "{{ player }}"
            data:
              media_content_id: spotify:playlist:6lsYYSHwgwBAiAG0WLrASg
              media_content_type: playlist
            
      - conditions:
          - condition: template
            value_template: "{{ active_player and active_player != player }}"
        sequence:
          - data:
              entity_id: "{{ active_player }}"
              group_members: >-
                {{ group_members + [player] if player not in group_members else
                group_members }}
            action: media_player.join
copper axle
#

Things I've tried on top was to create a new group without the master, but that fails because it says "already joined on its own, unjoin first". When you unjoin first, music fully stops

feral grove
#

What is you do it the other way around. Aka unjoin the non-master speaker.

90% of the times when I unjoin the music continues just fine. Only sometimes it does restart a stream or something.

copper axle
#

If I unjoin the non-master speaker, than this speaker stops working, the rest keeps playing. This works as intended.

But I'd like to unjoin the master speaker (because button has been pressed or no presence for long time), while keep others joined and continue with same music. I have no clue how to, if even possible. Of course I can completely kill the music and start with playlist again, but that's not the point as it dosn't continue.

I don't even know if this is possible at this point