Unjoin Script

Hey all,
Sometimes I find that sonos devices (i have 4) get joined for whatever reasons.
This shows up when “sonos_group” has more than 1 value.

The goal - split sonos groups out with a script, once joined.

so what i was trying to do was write out templates for each device, that should show if they are grouped and then use that in a script to ungroup them overall
However, my templates dont seem to work very well.

the “sonos_group_devices” that pulls everything together, so i can show it as a conditional card in the UI just doesnt seem to do what i want and give a simple true/false?

im wondering if im over thinking it all maybe?

#Sonos Group Status
  - platform: template
    sensors:
      sonos_group_bedroom:
        value_template: >
          {% if is_state('sensor.sonos_group_bedroom','') %}
            {% set group2 = (states.media_player.sonos_bedroom.attributes.sonos_group | list | length) %}
            {% if group2 > 1 %}
            {{ states.media_player.sonos_bedroom.attributes.sonos_group  }}
            {% else %}
            Not Grouped
            {% endif %}
          {% endif %} 
        friendly_name: Sonos Group Bedroom
        icon_template: mdi:bed
      sonos_group_living_room:
        value_template: >
          {% if is_state('sensor.sonos_group_living_room','') %}
            {% set group2 = (states.media_player.sonos_living_room.attributes.sonos_group | list | length) %}
            {% if group2 > 1 %}
            {{ states.media_player.sonos_living_room.attributes.sonos_group  }}
            {% else %}
            Not Grouped
            {% endif %}
          {% endif %} 
        friendly_name: Sonos Group Living Room
        icon_template: mdi:sofa
      sonos_group_spectrum:
        value_template: >
          {% if is_state('sensor.sonos_group_spectrum','') %}
            {% set group2 = (states.media_player.sonos_spectrum_studio.attributes.sonos_group | list | length) %}
            {% if group2 > 1 %}
            {{ states.media_player.sonos_spectrum_studio.attributes.sonos_group  }}
            {% else %}
            Not Grouped
            {% endif %} 
          {% endif %} 
        friendly_name: Sonos Group Spectrum
        icon_template: mdi:volleyball
      sonos_group_ensuite:
        value_template: >
          {% if is_state('sensor.sonos_group_ensuite','') %}
            {% set group2 = (states.media_player.sonos_ensuite.attributes.sonos_group | list | length) %}
            {% if group2 > 1 %}
            {{ states.media_player.sonos_ensuite.attributes.sonos_group  }}
            {% else %}
            Not Grouped
            {% endif %} 
          {% endif %} 
        friendly_name: Sonos Group Ensuite
        icon_template: mdi:shower-head

      sonos_group_state:
        value_template: >
          {% if is_state('sensor.sonos_group_state','') %}
            {% if (states.sensor.sonos_spectrum_studio.state) != 'Not Grouped' %}
              {% if (states.sensor.sonos_ensuite.state) != 'Not Grouped' %}
                {% if (states.sensor.sonos_living_room.state) != 'Not Grouped' %}
                  {% if (states.sensor.sonos_group_bedroom.state) != 'Not Grouped' %}
                    True
                  {% else %}
                    False
                  {% endif %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %} 
        friendly_name: Sonos Group State
        
      sonos_group_devices:
        value_template: >
            {% if is_state('sensor.sonos_group_devices','') %}
              {% set sonosgrouped = [states.sensor.sonos_group_bedroom.state,
                                    states.sensor.sonos_group_ensuite.state,
                                    states.sensor.sonos_group_living_room.state,
                                    states.sensor.sonos_group_spectrum.state]
              %}
              {{ ((((sonosgrouped)|unique|list)|replace("'Not Grouped'", ""))|replace('[', ""))|replace(']', "") }}
            {% endif %} 
        friendly_name: Sonos Group Devices

sonos_unjoin_all:
  alias: Sonos - Unjoin - All
  sequence:
  - data_template:
      entity_id: all
    service: sonos.unjoin

Is there a particular reason to display which Sonos devices are grouped? Or is it sufficient for a single sensor to simply indicate the quantity of grouped devices?

Also, are all of your media_players Sonos devices?

Hey!

Fair point, don’t need to know which or how many really
I just need to be able to unjoin anything/everything when they are joined

I have sonos and Alexa