Sonos - Ungroup when charging

Working with a Sonos Roam, that I regularly charge via wireless charger. When I place it on the charger I’d like to ungroup it, if it’s grouped, as it seems to charge faster when not playing.

I’ve figured out the trigger just fine, but having difficulty with the action. Primarily determining if the roam is in a group. Once I can programmatically determine that, I can ungroup it.

So the device is media_player.sonos_roam, which has an attribute (when grouped) of:

group_members:
  - media_player.bedroom
  - media_player.sonos_roam

I’ve been playing with a template using an if is_state_attr('media_player.sonos_roam', 'group_members' .. but haven’t gotten much farther. My logic is to count how many members there are in the group, and if it’s greater then 1, consider the speaker grouped, otherwise consider it ungrouped.

For example when a speaker is ungrouped, it also has the group_members attribute, but it’s singular, such as group_members: media_player.living_room.

Bit our of practice and could use some guidance; much appreciated!

Simpler solution: you can call the unjoin service if it’s grouped or not. If it’s grouped it will be split off, and if it’s already alone nothing will happen. Something like this:

service: media_player.unjoin
target:
  entity_id: media_player.sonos_roam

I assume you’ve already found the binary_sensor.roam_power entity which will tell you if it’s charging (and via which method).