Making a script conditional on entity state and/or attribute

I’m trying to modify some existing scripts to be conditional on an entity… specifically, on the value of an entity attribute.

This script will join or unjoin my living room Sonos speaker to a group with the kitchen Sonos speaker as master, depending on the current state of the living room Sonos speaker:

sonos_living_room_join_unjoin_kitchen:
  alias: Sonos Living Room Join/Unjoin Kitchen
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_living_room
        state: playing
      sequence:
      - service: sonos.unjoin
        data:
          entity_id: media_player.sonos_living_room
    - conditions:
      - condition: state
        entity_id: media_player.sonos_living_room
        state: paused
      sequence:
      - service: sonos.join
        data:
          entity_id: media_player.sonos_living_room
          master: media_player.sonos_kitchen
    - conditions:
      - condition: state
        entity_id: media_player.sonos_living_room
        state: idle
      sequence:
      - service: sonos.join
        data:
          entity_id: media_player.sonos_living_room
          master: media_player.sonos_kitchen
    default: []
  mode: single

It works well enough, but it’s not flexible enough to reflect the changes I want to see in my UI (using custom button cards). What I’d like to do is change the script so that instead of the conditions being the state of the living room Sonos speaker, it instead relies on the attribute sonos_group. The current value of sonos_group is media_player.sonos_kitchen, media_player.sonos_living_room… however, I can’t figure out a condition statement that will return true if one value or another is present in that attribute. I started with getting the exact attribute value from the template feature:

…and then I did a quick logic test of the value, but I failed to return true that I’d hoped to see:

Truthfully, what I really want to do is check if media_player.sonos_living_room is contained in the state_attr, but I don’t know what operators to use. If that text is within state_attr, I’ll run one service; if not, then another. Can anyone assist?

EDIT: I’d considered creating template sensors for each state_attr that I’m interested in… but ultimately, I still don’t know the syntax used to determine whether media_player.sonos_living_room is contained within it.

Hold on now… this returns true:

{% set group = state_attr('media_player.sonos_living_room', 'sonos_group') %}
{% if 'media_player.sonos_kitchen' in group %} TRUE
{% else %} FALSE
{% endif %}

I think this is enough to get me started…

EDIT: Yes, this script totally works:

sonos_living_room_join_unjoin_kitchen:
  alias: Sonos Living Room Join/Unjoin Kitchen
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: media_player.sonos_living_room
        state: playing
      sequence:
      - service: >-
          {% if 'media_player.sonos_kitchen' in state_attr('media_player.sonos_living_room', 'sonos_group') %} sonos.unjoin
          {% else %} media_player.media_pause
          {% endif %}
        data:
          entity_id: media_player.sonos_living_room
    - conditions:
      - condition: state
        entity_id: media_player.sonos_living_room
        state: paused
      sequence:
      - service: sonos.join
        data:
          entity_id: media_player.sonos_living_room
          master: media_player.sonos_kitchen
    - conditions:
      - condition: state
        entity_id: media_player.sonos_living_room
        state: idle
      sequence:
      - service: sonos.join
        data:
          entity_id: media_player.sonos_living_room
          master: media_player.sonos_kitchen
    default: []
  mode: single

Now I need to see if I can put if-then statements into my custom card for matching the colors to the various states & group attr’s…

Actually, I just reduced these scripts to this:

 

…because I split the custom button cards into conditional cards that display alternate buttons based on group membership status. So instead of calling a script, the button will call the join or unjoin service directly. Thanks for looking into this!