Python_script - get entity_ids for device_id

Hi all,

how can I receive the entity_ids related to a specific device_id within python_script?
I have found a description how this is done within custom components (How I can get DeviceEntry by entity_id in python?) but this requires some import which is not possible within python_script.

I additionally found how this can be done within templates (Templating - Home Assistant), but still no option how the same as template: “device_entities(device_id)” is achieved within python_script.

Thanks for some enlightenment :slight_smile:

Just read the code for the template device_entities

I should clarify that you may not have access to this information, but the point of my response was to show you that you can get any example from the HA code as the calls are identical in python_scripts.

Thank you for your reply - I am unfortunately not sure I fully got it :see_no_evil:

Do I get your reply

you may not have access to this information

correct that this function is not available within python_scripts. But IF it would be, based on

…to show you that you can get any example from the HA code as the calls are identical in python_scripts

I could simply access it via identical function as done in HA source code? (which is true for all python_script functions?)

I’m not sure I understand you’re response.

All I’m saying is: Look at Home Assistant integrations & helpers to see how they access that information and then replicate it in your code.

Also, to me, this seems like it could be an XY problem. Why don’t you post your intentions with the python script. Chances are, you don’t need it.

Thanks petro,

but I do not think this is a XY problem :wink:
My intention is to have a script which does activity guessing for a person within my home.

For this I have used the custom variable integration and created a variable for every person.
This variable holds different device_id’s or entity_id’s as attributes like e.g:

type: family
room: buero
location: home
sleeping_room: schlafen
alexa_voice_id: amzn1.ask.person.AEFAIKHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
entities:
  car: dc347f2369586ae46bb35623971f3c31
  computer: device_tracker.cebcb921c3637
  work_computer: e55462bc37379d521eb5c06c9e0f667c
  beacon: sensor.chipolo_one_black
  person: person.martin_kraemer
  phone: bedbc4dd25f192839a9ce28e9e45b1e9
  smartwatch: 95a3f614fae80205a85bdc3c6da23e09

Furthermore I have a variable which holds different “home” device_id’s / entity_id’s (like washing_machine, tv, ceramic_hobin, dishwasher, fridge etc.) the same way as attributes dict.

With help of combining these states I should be at a good position to guess the current activity of a person within my home. But as you see some of them are entities, some of them are devices (which is btw. intended as for some of them I have more information so I am able to do a more reliable guessing, and for some of them I only have that single state entity where I mostly can only do a best guess).
So what I need to do at some point within the script is extend the device_id’s to entity lists to be able to get the corresponding states.

Just if somebody else comes around this and is interested how I handled this.
I am using a custom integration now which allows imports…not awesome (as I would have preferred to use the built in python_scripts component with the device_entities command - but as said this one is not defined) so at the moment the best way to go as I see it: AlexxIT/PythonScriptsPro: Advanced version of python scripts for Home Assistant without limits (github.com)