Custom Intents Using <name>

I’ve been playing around with making custom sentence templates and intents using the new Year of the Voice features.

According to the documentation you can use the <name> and <area> lists within custom sentence templates as these are provided by Home Assistant. This is working but I’m noticing that you get the name of the entity, not it’s entity id, for use with an intent. I’m struggling with how to use this within a custom intent to actually pull up state information.

As an example, say I have a sentence to make the presence of a person:

- sentences:
  - "is <name> at home right now"
  requires_context:
          domain: 'person'

In my custom intent_script I get the name “Rob” but would like to get “person.rob” so I can pull in the state information. For built in intents like HassGetState the name must be turned in to a useable entity somewhere. Just curious if there is a way within an intent_script to get the entity id or if I’m going to end up having to make my own custom lists to map inputs. I’d rather not do this since the context matching is awesome so I don’t have to spell out each individual entity option.

Hopefully this makes sense and someone as either encountered this or knows if there is a workaround.

Hi,

I somewhere found this line of code that lists all available entitites, filters by name and then returns the entity_id. It works perfectly with the FriendlyName.

intent_script:
  StopCover:
    action:
      service: "cover.stop_cover"
      data:
        entity_id: "{{ states.cover | selectattr('name', 'in', name) | map(attribute='entity_id') | list | first | default }}"
    speech:
      text: "{{name}} fermata!"

you probaly want to change states.cover to states.person

I am now trying to understand how to use aliases since the intent is recognised when useing aliases but the line of code I just provided does not work.

There is no attribute Alias so probably there is something like for Area where you filter the entities with this:

selectattr('entity_id', 'in', area_entities({area}))

Anyone on how to use entity alias in templates?

You could use friendly_name?

{{ states.person.personname.attributes.friendly_name }}

You could create a custom sentence like this:

language: "en"
intents:
  PersonHome:
    data:
      - sentences:
          - "is {person} [at] home right now"         
lists:
  person:
    values:
      - in: "person one"
        out: "person.person_one"
      - in: "person two"
        out: "person.person_two"

and custom response:

  PersonHome:
    speech:
      text: {% if person == 'home' %}Yes{% else%}No{% endif %}

I think this is template I was looking for, at least until a better way to map these comes along. Ideally Home Assistant would pass in a variable “entity_id” or something with the matched ID from the matched entity name or alias. I can use this for now - thank you!

@DonNL - I did play around with making a list but for more advanced use cases this would get tedious really quickly. My goal would be to add something to the system and just have the intent work without having to update all the sentence lists each time. Thanks though.

1 Like

I think you mean the keyword name in the custom_sentence.
and it will automatically do the search between all friendly names and aliases of hassio, so you need no list. to limit the search only to the person domain you have to use require_context

language: "en"
intents:
  PersonHome:
    data:
      - sentences:
          - "is {name} [at] home right now" 
          - "where is {name}"  
        requires_context:
          domain: person

then I still have not find a cleaner way, but the following should address to the entity state

intent_script:
  PersonHome:
    speech:
      text: The {{name}} is at {{ states.person | selectattr('name', 'in', name) | map(attribute='state') | list | first | default }}

you can also use entity_id instead of state to return the string to use in a service

sadly it returns an error when using aliases

2 Likes

Help me to understand?
Can i translate intents to another language too? Right now Estonian language has only turn on/off intents.