Semantics/roles of entities within a device

Hi there, I’ve recently started automating my home with Home Assistant, and mostly love it. I’ve been able to integrate most of my devices with ease, either directly or through zigbee2mqtt, set up some automations, played around with pyscript and wrote my first integration for my fireplace.

What I haven’t really been able to wrap my head around is how one is supposed to work with various entities in a device in a way that honors the semantics of the entity in the scope of the device. I realize this may well be a feature request, but I still think I am maybe missing something obvious because it seems hard to imagine that others don’t have this need.

Let’s make this very concrete. I’m using the hass_nuki_ng integration for my Nuki smart lock, which works like a charm from a functionality standpoint. Nuki has a feature called lock’n’go, activated by double-tapping the button, that opens the lock and will lock it the next time the door is closed (the door can be either opened or closed when activating that function). What I want to do is to build an automation that when lock’n’go is activated casts a lovelace dashboard to a nearby device that shows all unsecured doors, except for the one in lock’n’go mode (and I don’t want to build one automation for each door).

The challenge is that lock’n’go is reported via a sensor sensor.nuki_main_entrance_state, which is the state of the lock - locked, unlocked, unlocked (lock'n'go) etc. What I want to show on the dashboard is the state of the sensor.<name>_door_security_state entity of the other doors, which also factors in the door contact sensor state - closed & locked, closed & unlocked, open etc, and so would also exposes doors that are open but where the lock is locked (could happen). So the list of entities that I want to show can be obtained by:

  • list all entities from integration nuki_ng
  • collect all devices from these entities (these will be the Nuki devices)
  • for each device, get the corresponding lock state sensor entity (???)
  • remove entities with a state that is unlocked (lock'n'go)
  • collect the devices for the remaining entities
  • for each device, get the corresponding door security state sensor entity (???)
  • remove entities with a state that is closed & locked

??? marks the steps for which I don’t see a reasonable way to accomplish them. Definitely not in templates, and let alone in lovelace cards. One thing I also don’t want to do is to depend on entity IDs, as they can be changed. Even when disregarding that possibility, how would I find the lock state entity among the entities of my device? The device ID is opaque, the device name is some human-readable name such as Nuki Main Entrance, the lock entity will be called sensor.nuki_main_entrance_state. I can’t even filter the device entities for sensor.*_state, because that also matches sensor.nuki_main_entrance_door_security_state. What I want is to retrieve the lock state or the door security state sensor within a given device. This use case - retrieving an entity of a device that plays a certain role within that device - seems very fundamental to me, so I really think that I must be overlooking something more obvious.

I’ve found a way to solve what I want in pyscript. The device in the device registry has an identifiers field, and for the case of nuki_ng devices, this contains an entry (id, 123456789) - some 9-digit number. The entities then all have a unique_id of nuki-<id>-<role>, such as nuki-123456789-state, or nuki-123456789-door_security_state, and the entity registry allows me to get the entity IDs for these unique IDs. Unfortunately, neither that scheme nor the identifiers entry seem to be a general concept, nor are they easily accessible from templates or entity filters. And the pyscript approach doesn’t help me with Lovelace cards either…

Really would appreciate some experiences or thoughts on how other people have solved similar problems.