Template sensor to find master in multi room audio setup

I just looked through the code and…

If the device is not currently in a zone, master and group will not be present.

I did run the template when speakers were grouped but the attributes were not accessible. All screenshots posted from my side are made in the situation where two speakers were grouped.

So when you see these values in the states page and you run this template, the values attribute names don’t match

Wait a minute, the display in the states page flattens everything.

Try this template:

{%- for attr, value in states.media_player.soundtouch_kuche.attributes.items() %}
{{ attr }}| {{ value }}
{%- endfor %}

Here is the response. Again, 2 Speakers are grouped and playing music in sync:

SOrry man for reiterating myself but what you’re saying doesn’t make sense. At all. Luckly that last template proved my theory correct, is_master was being flattened to the root making it look like it was an attribute on the root but it is not.

Try this

{{ states.media_player | selectattr('attributes.soundtouch_zone', '!=', None) | selectattr('attributes.soundtouch_zone.is_master','eq',True) | map(attribute='name') | list | first }}

Edit to clarify to get to the ‘is_master’ field on a single state object.

{{ state_attr('media_player.soundtouch_kuche','soundtouch_zone')['is_master'] }}

I appreciate your help. I have a dozen template sensors in my hass config but this one seems to be tricky. Anyway, here is the output:

Ok ok ok, ugh, try this:

{{ states.media_player | selectattr('attributes.soundtouch_zone', 'defined') | selectattr('attributes.soundtouch_zone.is_master','eq',True) | map(attribute='name') | list | first }}

Wow! We’re getting closer. Master is correctly identified. This looks good!

When I ungroup the speakers the response is simply empty.

So based on that… what would a template sensor look like that prints the current master if a group exist and prints “No Master” when a group of speakers does not exist?

That would be alittle longer, at least we got to the root cause of the issue.

{% set master = states.media_player | selectattr('attributes.soundtouch_zone', 'defined') | selectattr('attributes.soundtouch_zone.is_master','eq',True) | map(attribute='name') | list | first %}
{{ master if master else "No Master" }}

Now keep in mind, more than one group will always produce the first group’s master only.

2 Likes

That’s it! You did it! This was exactly what i was looking for. Thank you very much. Just out of curiosity… why are you so skilled with this jinja templating stuff? I am always impressed when I stumble over your posts in the forum!

Honestly, I like puzzles.

1 Like

Best answer you could give :slight_smile: Have a nice day. Thanks for your patience.

No problem, glad to help.

Hey @petro ,

I guess I need your help. Even after a year of fiddling my multi room audio setup still needs some minor improvements:

Whenever a speaker e.g. media_player.soundtouch_flur is in a group with another speaker the soundtouch integration creates an attribute called soundtouch_group. This attribute contains a list of all speakers that are currently streaming music together:

I want to create a binary_sensor that checks if a specific speaker is currently in a group. I need a template that checks wheather a list of speaker is present under the attibute soundtouch_group. I have tried the following:

{{ is_state_attr( 'media_player.soundtouch_flur', 'soundtouch_group', true ) }} but this returns a false althoug the speaker is curently in the group.

Do you have an idea?

{{ not is_state_attr( 'media_player.soundtouch_flur', 'soundtouch_group', []) }}

Thanks for your help. This answer looked promising but it also renders true even if a soundtouch_group does not exist:

Do you have another idea?