Template sensor to find master in multi room audio setup

Guess this is one for template god @petro :slight_smile:

With latest release 0.107 hass finally supports multi room states for Bose Soundtouch speakers. When a multi room zone exists all speaker have an attribute is_master: true/false.

I want to create a template sensor that outputs the current master’s name. My 4 Soundtouch devices have the following names:

  • media_player.soundtouch_kuche
  • media_player.soundtouch_schlafzimmer
  • media_player.soundtouch_room2
  • media_player.soundtouch_wohnzimmer

Is it possible to use these media_players as a list and iterate the over it to find the one with the attribute is_master: true?

You could jus use

{% if  is_state_attr('media_player.soundtouch_kuche', 'is_master', 'true' %}
  Soundtouch Kuche
{% elif  is_state_attr('media_player.soundtouch_schlafzimmer', 'is_master', 'true' %}
  Soundtouch Schlafzimmer
{% elif  is_state_attr('media_player.soundtouch_room2', 'is_master', 'true' %}
  Soundtouch Room 2
{% elif  is_state_attr('media_player.soundtouch_wohnzimmer', 'is_master', 'true' %}
  Soundtouch Wohnzimmer
{% else %]
  Undefined 
{% endif %}

yep, you can do what @tom_l posted. You could also use:

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

You may need to alter True to 'true' if it’s a string.

If you have more than 1 media_player with is_master equal to true, we’ll have to modify the template.

2 Likes

Is it possible to use wildcards like states.media_player.soundtouch_* so that only the soundtouch media players are adressed in the search for the master device?

That’s a little more difficult. You can’t use a wildcard but you can make this work.

{%- set media = namespace(devices=[]) %}
{%- for m in states.media_player if m.object_id.startswith('soundtouch_') %}
{%- set media.devices = media.devices + [ m ] %}
{%- endfor %}
{{ media.devices | selectattr('attributes.is_master','eq',True) | map(attribute='name') | list | first }}

I have tried what @tom_l suggested but somehow this is not working and I have no clue why. Attribute is_master is clearly true in this example. Any idea?


Template does not work as it states undefined:

Try replacing ‘true’ with True (no quotes, capital initial letter).

Still no luck :thinking:

put this in your editor

{% set s = state_attr('media_player.soundtouch_kuche', 'is_master') %}
{{ s }}
{{ s * 2 }}

EDIT: And screen shot the results.

Throws an error:

remove the second line ({{ s * 2 }}), what does it return?

Return None

image

Ok, so that’s interesting. It’s pretty much saying that is_master is None or doesn’t exist as a attribute.

So, can you try this

{{ states.media_player.soundtouch_kuche.state }}
|{{ states.media_player.soundtouch_kuche.attributes | list | join('|\n|') }}|

EDIT: I edited the template to show the start and end of attributes, make sure you use the up to date one

You seem to be right. This Attribute is pretty new and came with the Hass 0.107 release but there seems to be an error in the integration. The Attribute is_master is not listed:

alright, you don’t have is_master in that list. So i’m guessing is master only appears when you have them ‘grouped’ or ‘playing together’?

Try pairing them together for ‘group play’ and run that template again. To re-execute a template add a space then remove it anywhere in the template lines

Screenshot was made when media_player.soundtouch_kuche was grouped and master. The attribute itself only appears when speakers are grouped.

Ok, so then you have to build that into your logic. Using if statements is going to be a bitch. You can use this template.

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

This also delivers no output:

This is strange. I mean even the if statement should not be a problem because it would simply say Undefined when the attribute is not available. Guess I’ll open an issue in hass/core. Thanks for your help @petro. If you have other ideas I am happy to try later on.

If they aren’t grouped that will return nothing. They need to be grouped and the attribute has to exist.

You need to understand that attributes appear and disappear. This is not an issue with hass/core.

If the attribute shows up in the states page, it will be accessible. If it does not, it will not. Attributes can appear and disappear based on current hardware settings. I"m trying to help you through a solution that works.

So, group them and run the template. If it doesn’t work, then we can fix it so it does.