Get entities related to device

Hi All,

is it possible to get entities related to a device (device_id) within templates or python script?
(not integration)

By searching I found: How to get the state attr from a device_id? - #2 by petro
Is this still valid?

Based on: How I can get DeviceEntry by entity_id in python? - #2 by postlund
I would assume it is at least possible from an integration…

No, that’s out dated information

for a list of entity_ids

"{{ device_entities(device_id) }}"

for state objects

"{{ expand(device_entities(device_id)) }}"
2 Likes

It’s the first item in the documented section about this:

https://www.home-assistant.io/docs/configuration/templating/#devices

Awesome, @petro! Exactly what I needed. :star_struck:

Now assume that I have a device and I want to get the name of the one entity that is a switch (I know that there’s only one switch entity). How would I get that via templating? :flushed:

I have the same question. Let me know if you find the answer.

Look at the marked solution…

Hi Petro,

First of all, thank you for all your help. I’m getting closer, but still struggling. My templating skills just aren’t quite there yet.

If I take an Inovelli Red Series ON/OFF switch LZW30-SN, which has quite a few entities within it…and I use your provided solution, for example:

 {{ expand(device_entities('INSERT_THE_DEVICE_ID_HERE'))}}

I get a long TemplateState object/string output thing. I can see the 'friendly name" of all the entities within the inovelli swtich, but I cannot seem to filter out just the one friendly name I need - the one that is related to this devices main function which is a SWTICH.

If I’m able to do this, I can finally reach the holy grail of linking a device_id to a device’s friendly name! (I wake up in the middle of the night sweating just thinking about this problem).

If you need the devices name, just get it from the device. There’s no reason to link to an entity.

Choose your poison.

{{ device_attr('INSERT_THE_DEVICE_ID_HERE', 'name') }}
{{ device_attr('INSERT_THE_DEVICE_ID_HERE', 'name_by_user') }}

!@#$! Dear god. :slight_smile: