Help with automation for Music Assistant and Sonos

I'm using Music Assistant and have a Sonos system with 6 speakers including a soundbar. I need an automation to unjoin the soundbar from the player group and transfer its queue to the other players when the TV is turned on. Specifically:

  1. Retrieve the list of group_members for the Soundbar

  2. Check if the Soundbar is the master player (first member) in the group_members attribute

  3. If it is, transfer the queue to the next member of the group and then join back the remaining players (which get unjoined when the queue is transferred)

  4. If the Soundbar was not the master player then simply unjoin it from the group

I understand the Actions to perform these functions but my yaml and jinja2 skills are pretty bad so could use some help getting started with the script that retrieves and iterates through the member attribute list. Thanks.

I ended up solving this myself as it was much simpler than I expected. Again, the purpose is to remove the Sonos Soundbar from the media player group that it is a member when the TV is turned on. If the soundbar was the master, then transfer the queue to the next player in the member list and add the previous members back. This is necessary since either removing the master or transferring the queue destroys the player group.

Here's the code I used:

alias: soundbar test
description: ""
triggers:
  - trigger: state
    entity_id:
      - media_player.lg_webos_smart_tv
    to:
      - "on"
    id: TV On
conditions: []
actions:
  - condition: template
    value_template: "{{members[0] == 'media_player.soundbar_ma'}}"
  - action: music_assistant.transfer_queue
    metadata: {}
    target:
      entity_id: "{{members[1]}}"
    data:
      source_player: media_player.soundbar_ma
    enabled: true
  - repeat:
      for_each: "{{ members[2:]}}"
      sequence:
        - action: media_player.join
          metadata: {}
          target:
            entity_id: "{{members[1]}}"
          data:
            group_members: "{{repeat.item}}"
          enabled: true
  - action: media_player.select_source
    metadata: {}
    target:
      entity_id: media_player.soundbar_ma
    data:
      source: TV
mode: single
variables:
  members: "{{state_attr('media_player.soundbar_ma', 'group_members')}}"

With the release of MA 2.10 today the solution has become much simpler. MA will now automatically move the queue to another member when the master (primary) is removed from the group. So all that needs to be done in the automation is unjoin the soundbar when the tv is turned on and change the soundbar source to TV. When the tv is turned off you can change the source back to MA. The native Sonos soundbar option of TV Autoplay/Ungroup on Autoplay still will not work properly so the simple automation is still the best bet.

alias: Soundbar Autoplay
description: >-
  Unjoin the soundbar from all groups when tv is turned on and switch source to
  TV. Switch source back to MA when tv is turned off.
triggers:
  - trigger: state
    entity_id:
      - media_player.lg_webos_smart_tv
    to:
      - 'on'
    id: TV On
    from:
      - 'off'
      - playing
  - trigger: state
    entity_id:
      - media_player.lg_webos_smart_tv
    to:
      - 'off'
    id: TV Off
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - TV On
        sequence:
          - action: media_player.unjoin
            metadata: {}
            target:
              entity_id: media_player.soundbar_ma
            data: {}
          - action: media_player.select_source
            metadata: {}
            target:
              entity_id: media_player.soundbar_ma
            data:
              source: TV
      - conditions:
          - condition: trigger
            id:
              - TV Off
        sequence:
          - action: media_player.select_source
            metadata: {}
            target:
              entity_id: media_player.soundbar_ma
            data:
              source: Music Assistant Queue
mode: single