Yes, the first item in group_members represents the master but it’s not “grouped” unless there’s more than one item.
If you want to select the master media_player of a grouped set of media_players, the media_player’s group_members attribute should contain more than one item.
understood. Both players in this example are grouped (together). Thats why the output of {{ state_attr(player.entity_id, 'group_members') }} gives a list namely ['media_player.ralf', 'media_player.eltern'].
My expectation is adding | first will give me the first element. If you do the example without the for loop it works.
{{ state_attr('media_player.ralf', 'group_members') }}
{{ state_attr('media_player.ralf', 'group_members') | first }}
Unless you check if they’re actually grouped, each one will be identified as a master.
I have several media_players and some are grouped but others are not. So if you want to create a general purpose template to select the masters of grouped media_players, it needs to ensure the media_player is actually grouped (i.e. more than one item in group_members).
And I want to select all players which play … and if they are grouped I only want to have the master.
This should be part of the solution … but the “first” is not working.
{% for player in states.media_player | selectattr('state','eq','playing') %} |
{{ state_attr(player.entity_id, 'group_members') | first }}
{% endfor %}
That’s exactly what I said. If you want grouped players, you have to select the ones that have more than one item in group_members. The template you posted doesn’t do that. It simply attempts to report the first item in the list, even if there’s only one item.
# from those playing
{% for player in expand(states.media_player) | selectattr('state','eq','playing') %}
# I want all of those (grouped or ungrouped) where the following is true:
{{ state_attr(player.entity_id, 'group_members') | first == player.entity_id}}
{% endfor %}
I need help in two ways
selecting the first is not working in this example. It works when specifically using the entity id. But not in the for loop with the variable. Why?
how to build a group of exactly those players where the condition will evaluate true
raising this from the dead just to say thanks! I was searching for something completely unrelated today and came across this. I had gone through similar trials and troubles trying to get this to work for a while and eventually gave up. I actually had the template working but didn’t know how to apply the mini-media-player to the template entity in auto-entities. So thanks very much for putting this out there! Works great for me.