#Working with aliases programmatically

1 messages · Page 1 of 1 (latest)

summer wasp
#

Hi,

I'm super curious about voice assistants (who isn't?) and have started working on custom intents to make it a little more dynamic.. But I have come to some limitations (I think).

  1. All my areas, floors and relevant devices have one or more aliases on them. But I can't seem to find a way to pass the area based on alias-usage from the custom_sentence to the custom intent.

For example. I have a custom sentence that is "List all devices in {area}" (but in Swedish) which I want to list all devices that are in a certain area. The problem is that if I say "List all devices in vardagsrummet nere" (which is the way to properly say the area) whereas the area name is actually named "Vardagsrum Nere" (which is the proper way to describe the area in text).. All I get from Home Assistant is that it doesn't understand what I mean.

If I say "List all devices in vardagsrum nere" it just works, but that is not the way you would naturally ask.

In my custom intent I have therefore created a "translation array" which translates all the aliases into their proper actual areas, but {area} doesn't seem to return "vardagsrummet nere" to the intent. So it still just fails.

  1. Another problem with this is that there doesn't seem to be a way to programmatically limit the returned devices to only devices that are actually exposed to the voice assistant. It returns all devices in the room no matter if I can actually control them through assist or not.

  2. Another problem is also that I can't seem to return the actual aliases for the devices. I have figured out how to return the friendly name, but having the possibility to return one or all of the aliases for the devices would be even greater.

What have I missed and how do I fix this? 🙂 I haven't spent very long on this so I'm sure it's just a case of limited experience. 🙂

summer wasp
#

This is my custom sentences:
HassListDevices:
data:
- sentences:
- "Vilka lampor och strömbrytare finns i {area}?"
- "Lista alla enheter i {area}"
- "Vilka enheter kan jag styra i {area}?"
- "Vad har jag för lampor och brytare i {area}?"
- "Visa alla ljuskällor och brytare i {area}"
- "Vilka enheter finns i {area}"
response: "devices_area"

#

And this is my code for handling the request:
HassListDevices:
speech:
text: >
{% set alias_map = {
"garaget": "Garage",
"källaren": "Källare",
"vinkällaren": "Vinkällaren",
"badrum nere": "Badrum Nere",
"badrummet nere": "Badrum Nere",
"gästrummet nere": "Gästrum Nere",
"gästrum nere": "Gästrum Nere",
"hallen nere": "Hall Nere",
"entrén nere": "Hall Nere",
"entrén": "Hall Nere",
"köket": "Kök",
"matrummet": "Matrum",
"matrum": "Matrum",
"vardagsrummet nere": "Vardagsrum Nere",
"badrum uppe": "Badrum Nere",
"badrummet uppe": "Badrum Nere",
"gästrummet uppe": "Gästrum Uppe",
"gästrum uppe": "Gästrum Uppe",
"hallen uppe": "Hall Uppe",
"kontoret": "Kontor",
"leias rum": "Leias Sovrum",
"leias sovrum": "Leias Sovrum",
"lejas rum": "Leias Sovrum",
"lejas sovrum": "Leias Sovrum",
"leopolds rum": "Leopolds Sovrum",
"leopolds sovrum": "Leopolds Sovrum",
"vardagsrummet uppe": "Vardagsrum Uppe",
"sovrummet": "Vårt Sovrum",
"utomhus": "Trädgård",
"vårt sovrum": "Vårt Sovrum"
} %}

{% set requested_alias = area | lower %}
{% set actual_area = alias_map.get(requested_alias, requested_alias) %}
{% set devices_in_area = area_entities(actual_area) %}

{% set friendly_names = namespace(value=[]) %}
{% for entity in devices_in_area %}
{% if entity.startswith("light.") or entity.startswith("switch.") %}
{% set name = states[entity].attributes.get("friendly_name", "") %}
{% if name != "" %}
{% set friendly_names.value = friendly_names.value + [name] %}
{% endif %}
{% endif %}
{% endfor %}

{% if friendly_names.value | length > 0 %}
Lampor och strömbrytare i {{ requested_alias }} är: {{ friendly_names.value | join(", ") }}.
{% else %}
Jag kunde inte hitta några lampor eller strömbrytare i {{ requested_alias }}.
{% endif %}

lunar spear
#

I got PR that makes possible using area_entities and floor_entities with aliases - and it reportedly got merged, but I don't see it working...

summer wasp
#

Interesting, I wonder where it went. Did it get merged into current or next?

lunar spear
#

Oh damn

#

Sorry, it was so long ago

#

It's for area ID. So the usage would be area_entities(area_id(your_alias_or_name))

summer wasp
#

Wait, so it does work?

#

Nope, I can confirm that it indeed still doesn't work.

lunar spear
#

And it works with area_entities as well

#

yay 🙂

summer wasp
#

That'll be my number one reason for upgrading straight away. 🙂 Thanks man! 🙂

summer wasp
#

I can confirm that your addition did a world of difference. 🙂 Now my only problem really is to be able to have it match both floor by alias in combination with room by alias. 🙂

So I can ask for
What is the humidity in the bathroom downstairs?

Where both bathrooms have the alias "bathroom" and the only thing separating them is that the floors has the alias downstairs vs upstairs. 🙂

Then I'll be gucci 🙂

rough tartan
#

You need to make a sentence like what is the humidity in the {area} {floor}
and then you can use floor_entities(floor) | select('in', area_entities(area)) | list

summer wasp
#

It's that simple? Really? 😮 Thanks man, really appreciate it. 🙂

rough tartan
#

hmm

#

wait, I'm testing it now, but it seems I found an issue in the area_entities(alias) function

summer wasp
#

Yeah?

rough tartan
#

it only shows the entities of one area if mutliple have the same alias

summer wasp
#

So, if I have a bathroom downstairs and a bathroom upstairs first selecting the floor and then selecting room in the area_entities within that floor wouldn't suffice?

rough tartan
#

let's say both of these areas have the alias (or name) bathroom

summer wasp
#

Yes

rough tartan
#

area_entities('bathroom') will then only return the entity_id's of one of those areas (it looks like the first one on which the alias was added)

#

and not both of them

summer wasp
#

So, in that case we would have to get the floor-entities, and then filter that list by area-entities that match the list of floor-entities?

#

Oh wait.,.. No, now I follow

#

Roger, that is indeed a problem. area_entities really should allow two input parameters in that case I guess. Where the first one is area and the other one is an optional floor. Maybe

rough tartan
#

But why not just give the area the alias bathroom downstairs

summer wasp
#

I absolutely could do that, no problem... But I was of the impression that there was a reason for adding aliases to the floors as well 🙂

rough tartan
rough tartan
summer wasp
#

True, that would probably be the smartest way. Have it return objects for each room with a list of entities for the room, where you can then further filter down the lists yourself

rough tartan
#

and you can target floors by their alias

summer wasp
#

Yeah, I guess I thought that the alias for floors would be so that I could make the aliases for rooms more generic. But yeah, like you said. I could absolutely add the floor to the individual room aliases as well 🙂

#

The amount of aliases would be a lot more though

rough tartan
#

there are no built in intents to target a combination or area and floor. They either target an area or they target a floor

summer wasp
#

I'm swedish speaking from the northern parts of sweden, and have a wife that is from the southern parts... So we each call the floors AND the rooms different stuff. Having to handle the aliases on a room basis would mean I would have to duplicate the amount of aliases on the rooms to cover the speech pattern of both me and the wife, whereas if the floor aliases could be used there would be about half the amount of aliases in use 🙂

#

So instead of having
nere
nedervåningen
uppe
övervåningen

badrum
badrummet

gästrum
gästrummet

I would have to have
nere badrum
nere badrummet
nedervåningen badrum
nedervåningen badrummet
nere gästrum
nere gästrummet
nedervåningen gästrum
nedervåningen gästrummet

Yeah, you might see the point? 🙂

#

And if we figure out another way to say the name of a floor for example, I would have to add that alias to all my rooms on that floor, instead of just adding it to the floor itself. 🙂

#

Which in essence is the whole reason for me to build custom intents really. 😛 Make it work a little more fluently in "our" Swedish

rough tartan
#

and what if you do it like this:

language: en
# intent
intents:
  HumidityAreaFloor:
    data:
      - sentences:
          - "what is the humidity in {area} {floor}"
        requires_context:
          domain: sensor
          device_class: humidity

# response
responses:
  intents:
    HumidityAreaFloor:
      default: "The humidity is {{ state.state_with_unit }}"

No further intent script should be required

summer wasp
#

I should be able to use that! Some rooms only occur once, so I would have to do a [{floor}] I guess but other than that

rough tartan
#

hmm, doesn't really work for me

lunar spear
#

Yeah guys, alias will take first area it finds. The case is that internally there's no way to return list of areas there... We discussed it a lot, and decided that so far it's acceptable to assume aliases are unique... Sorry for that.