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:
Retrieve the list of group_members for the Soundbar
Check if the Soundbar is the master player (first member) in the group_members attribute
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)
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.
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