@Troon thank you for providing a solution to my problem. I have now reported a bug as the above regex is a work around for the following problem I am experiencing (not sure if I should post it here):
@Troon thank you for pointing me back to Jinja filters. I did try to use this initially however I can never get a value returned using this method on the below output:
I keep getting “undefined”,
<generator object do_map at …> or similar with select/ selectattr when trying to return the value in the “friendly_name” attribute which is why I turned to using regex. I presume it is possible with Jinja filters?
Your test1 variable is a one-item list containing a State object. As you can see here, friendly_name is an element of the attributes dictionary of a State object.
The result of your Jinja template is a list containing an entity object, but while these are possible to store in a Jinja variable, you cannot store them in a HASS variable. At the end of your Jinja template the final output must be a basic data type (dict, list, string, number, boolean, etc.).
If you try to return an entity object what you will actually end up with is a string. In fact all output from Jinja is only ever a string, but depending on what this string contains it may then be reinterpreted by HASS as other basic data types.
Going round in circles here, as you are generating strings not real objects. Explain what you’re trying to achieve.
You should be able to test this in Developer Tools / Template by substituting in the device ID of one of your media players rather than blindly running automations.
I already explained exactly what your problem is. You cannot store an entity object, as in the direct result of an expand() as you attempt to do for test1, in a HASS variable.
I have managed to achieve what I want . To have music assistant to play radio/ music from the device I request it from. The requesting device is the entity_id that Music Assistant derived it’s MediaPlayer entity from and is now using friendly name instead of entity_id with _2 appended. I have voice PE devices (media players).
Glad you’ve fixed it, but it still seems wrong to be using regex operations that turn a data structure into a string, then trying to force it back again. If you’re up for experimentation, try this in the Template Editor with your own device ID and requirements for the entity ID pattern:
Hard-coding a device ID: get this from the URL of the device page:
{% set media_player_id = "ea986a7eb43bd2317ea442b202ce63a1" %}
{{ media_player_id }}
Get a list of all entity IDs associated with that device that start with
"media player" and end with "room"; get the first such entry:
{% set media_player = device_entities(media_player_id)
|select('search','^media_player\..*room$')|first %}
{{ media_player }}
That is the entity ID done, now pull the friendly name
assuming that is still required:
{% set friendly_name = states[media_player]['attributes']['friendly_name'] %}
{{ friendly_name }}