Hi
I’m trying to find a solution to check if a Sonos speaker is grouped or not for a script, but wasn’t able to find one yet. I tried to use the sonos_group attribute as a condition but that didn’t work.
Thanks in advance.
Hi
I’m trying to find a solution to check if a Sonos speaker is grouped or not for a script, but wasn’t able to find one yet. I tried to use the sonos_group attribute as a condition but that didn’t work.
Thanks in advance.
You can use the sonos_group
for this purpose. If it contains more than one entity, those are in a group. The first entity in the list is always the coordinator.
If the attribute is inconsistent it may be because of a race condition on startup that we’re working to resolve: Sonos group data missing after Core restart. · Issue #55008 · home-assistant/core · GitHub.
tSay your speakers are in one group you could do a count. This template condition would do.
{{ state_attr("media_player.<entity_id>", "sonos_group") | count > 1 }}
I’d however prefer to check if the media_player.<entity_id>
-array contains a specific master_group. Say your master is ‘family_room’ and you want to know if ‘kitchen’ is joined, that template condition would look like:
{{ "media_player.family_room" in state_attr("media_player.kitchen","sonos_group") }}
By the way, the Sonos Integration page gives another interesting solution by using {{ state_attr( trigger.entity_id , 'sonos_group')[0] == trigger.entity_id }}
.
My interpretation is that the first element/item in the group is the master, so if a group is the first sonos_group
, you know it’s not a member, but the master
FYI, sonos_group
is renamed to group_members
since 2022.5 if I am not mistaking.
So for example the code to check if an entity is the “group master” changes from:
{{ state_attr( trigger.entity_id, "sonos_group")[0] == trigger.entity_id }}
to:
{{ state_attr( trigger.entity_id, "group_members")[0] == trigger.entity_id }}
I’m using this for my script … to check if the specific sonos speaker is grouped or not
{{ state_attr(‘media_player.sonos_one’,‘group_members’) | list | count > 1 }}
The template returns true if grouped otherwise false