I wish to create a group for all my plex media players dynamically?

Is this possible at all? Something like media_player.plex_* ? If so, could someone share how with me?

I then would use such a group with expand as the entities argument in a state trigger. Specifically I want to include it in the automation here - I have lots of tablets, TVs, laptops, phones, etc which are Plex media players. It’s not going to be at all easy to add them all manually to this automation (or, even to a group) . Simple notifications when Plex is playing - #20 by jamesking

You can use the group.set service with a template to get the entity ids:

service: group.set
data:
  object_id: all_plex_players
  name: All Plex players
  icon: mdi:plex
  entities: >
    {{ states.media_player | select('search','\.plex_') 
    | map(attribute='entity_id') | list }}

Groups created this way are not restart safe and will not update without calling the service again, so you will need to set up an automation to handle those occurences.

EDIT: Incoporated improvement from 123.

1 Like
select('search','.plex_')
                 ^^^^^^
         This is a regex pattern

Because it’s a regex pattern, the period has special meaning (it matches any single character) and will not be interpreted exclusively as a literal period. For this particular application (where you want to match a literal period exclusively) its special meaning should be escaped by prepending a backslash.

select('search','\.plex_')
  1. List item
2 Likes

Thanks both of you, I’ll implement that on startup

Out of interest, could I just enter media_player.plex_ as the term to look for, or is the search only looking at the object ID?

You could do that or, to make the selection even more precise, constrain it to search exclusively within the entity’s object_id.

  entities: >
    {{ states.media_player | selectattr('object_id', 'match', 'plex_') 
    | map(attribute='entity_id') | list }}

That will select all media_players whose object_id starts with the string plex_.

The equivalent using the search filter is:

selectattr('object_id', 'search', '^plex_') 

The leading ^ in the regex pattern means the balance of the pattern must match starting at the beginning of object_id.

1 Like

Thank you. you have explained it so well. These filters are so powerful. I really enjoy this - I can read the filters, understand what they are doing, but don’t know them well enough to design/think up solutions myself!

I am grateful to you for taking the time to help.

In the end I am not sure this is working how I want as I am trying to expand this group that I create in the entity_id of a state trigger of an automation; which doesn’t appear to work (!). (can see here if you want to know how wrong I have got it :smiley: ) I think perhaps it’s not possible to do what I want to do. :slight_smile:

But the automatic groups like this will be super useful in future!

Thank you for this thread.
I am trying to use this to create a dynamic group with wll the members connected to my wifi mesh devices.

service: group.set
data:
  object_id: fritz_clients_rep_kueche
  name: Geräte an Fritz Küche
  icon: mdi:network-outline
  entities: >
    {{ states.device_tracker | selectattr('connected_to', 'match', 'fritz-Rep-1200ax-Kueche') 
    | map(attribute='friendly_name') | list }}

The group is created if I run the group.set, but the state of it is unknown even though some devices are connected to this very repeater.
Any suggestion what could be wrong in my code?

The goal is to create a list of entity_ids but your template creates a list of friendly_names.

Replace this:

map(attribute='friendly_name')

with this:

map(attribute='entity_id')

Of course - Stupid me. :wink:
The friendly name is not unique for identifying the entity.
Thank you!

But I still get unknown only - even if I check right before if there is at least one device connected:
I also tried 'eq' instead of 'match' and used the exact text with no luck. :frowning:

EDIT: It works if I match object_id but not connected_to :frowning:

EDIT II:
It seems that none of the other attributes work with search except entity_id!?

EDIT III:
Got it!
This works:

selectattr('attributes.connected_to', 'search', 'fritz-Rep-1200ax-Kueche') |

In the example I posted in March 2022, the template uses selectattr to select object_id.

object_id is a property of an entity’s State Object.

attributes is another property of a State Object and, as explained in the documentation, it contains a dictionary. Each key in the dictionary is an “attribute”. For example, your sensor has source_type, ip, mac, etc. These are all keys in the dictionary contained within the attributes property.

So if you want to use selectattr() to get the value of a key in the attributes dictionary (such as connection_type), you reference it using attributes.connection_type.

Thank you for the explanation.
That totally makes sense.
I usually just use states of an entity, so I was not aware of this difference to attributes.
I guess I was irritated by selectattr and assumed I can „select“ also attributes.
Thanks again for your help.