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.