How to find entity_id using the Voice Alias using templates?

I am not sure if I just missed something, or if it’s not (yet) possible to link an alias to an entity_id.
So in short, is there a way to relate voice aliases to their entity_id?

Here’s my story
I created a small custom sentence to join/unjoin my speakers using a script. The thing is that the variable name holds the alias I mention in the instruction instead of the entity_id where the alias refers to. Since the variables are sent to a script called script.sonos_group_manager, I could just “search” for the entity_id in the script and trigger the actions from that point forward. But I haven’t found a way to get from the alias to the entity_id.

I was hoping for an attribute to filter on or maybe a function, like you have at devices and areas, to trace back the entity_id or device_id.

Anyone an idea how to achieve the goal of linking an alias to an entity_id?

The YAML, for those interested
Sentence:

language: "en"
intents:
  sonos_group_manager:
    data:
      - sentences:
          - "{do_what} [the|speaker] {name} [speaker]"
        requires_context:
          domain: media_player
lists:
  do_what:
    values:
      - in: "(Join|Add)"
        out: "join"
      - in: "(Unjoin|Remove)"
        out: "unjoin"

The intent_script

sonos_group_manager:
  action:
    service: script.turn_on
    data:
      variables:
        room_id: "{{name}}"
        action: "{{do_what}}"
    target:
      entity_id: script.sonos_group_manager

The script script.sonos_group_manager handles the rest, but now needs to know how to find the entity_if of the alias/{{name}}

There are many ways to do this, but we’d need more information on what type of entity you’re trying to get. Multiple entities can have the same name, so you’d need to intellegently select the correct one based on the context.

That’s good to know, great!

In my particular case I want to find entities in the domain media_player and with manufacturer ‘Sonos’.

If I just have a starting point, I might be able to solve the puzzle.
Like if I can get all entities that match on the alias (not the friendly_name) ‘Kitchen’ I more or less know how to drill down using for example:

  • is_device_attr(<entity_id>, 'manufacturer', 'Sonos')
    and
  • select('match', '^media_player.')
{{ states.media_player | map(attribute='entity_id') | select('is_device_attr', 'manufacturer', 'Sonos') | list }}

Yeah, I get that part, but it’s the link to/from the alias.
In the image below I have a media player the alias ‘Kitchen’, so now I can use a voice command that points to the name ‘Kitchen’.

image

Now I want to know the entity_id of the speaker that has the alias ‘Kitchen’.

add a select('is_state_attr', 'friendly_name', 'Kitchen')

No, not the friendly name. That doesn’t hold the Alias, nor is there an attribute aliases that I can use.

If there’s nothing to link it, then there’s nothing that can be chosen…

What I hoped for is something like you can do with areas. There is no link in the attributes from an entity to the area, but you can do this for example:

{{ area_entities(area_id('Kitchen')) | select('match', '^light.') | list}}

or

{{ integration_entities("sonos") | select("match", "^media_player") | list }}

So the link is not always obvious, even when it is there. But if it isn’t there (yet), I might just give it a go as feature request. Thanks

I was facing the same problem and found my own solution. I don’t know if there is any ‘offical’ one yet.

So I noticed that in .storage/core.entity_registry there are all information we need. With the following shell_command you can extract the entity_ids and aliases:
get_entity_alias: jq '[.data.entities[] | select(.options.conversation.should_expose == true and (.aliases | length > 0)) | {aliases, entity_id}]' ./.storage/core.entity_registry

I’ve then made a sensor:

  #Sensor für Zuordnung von Alias Namen zu Entity-IDs (Update über Neustart oder Service homeassistant.update_entity)
  - trigger:
      - platform: homeassistant
        event: start
    action:
      - service: shell_command.get_entity_alias
        data: {}
        response_variable: result
    sensor:
      - name: "Assist: Entity IDs mit Alias"
        state: '0'
        attributes:
          entities: "{{result.stdout}}"

And there you have it. In your script you can then use something like this:

variables:
  area: "{{area}}"
  area_id: "{{area_id(area) or None}}"
  name: "{{name}}"
  entity_id: >-
    {{ (state_attr('sensor.assist_entity_ids_mit_alias','entities') |
    selectattr('aliases', 'contains', name) | map(attribute='entity_id') |
    list)[0] or None }}
3 Likes

Sounds great, I’ll have a look ASAP but am currently too busy.

If it works, I’ll mark it as solution.

Thnx for posting your solution!