Get_entities node to get area and domain information

Hi,
Is there a way of using the get_entities node to gather information from a defined area and domain?

There is a card called auto-entities that does this for the front-end.
https://github.com/thomasloven/lovelace-auto-entities
Now I am looking for something to get the similar information into Node-Red.

Example:
I want to get information about all my light.entities in the area "hallway"

Hope that someone can make my day 🙂

Most information is available in Node Red, but the area does not seem to be included.
If your lights have some name or attribute that can used as filtering, then it should be possible.

You can see the available information in Node Red by clicking the Context Data in the upper right corner (might be listed in the down arrow if the right side panel is not wide enough).
Click the refresh icon to the right of the “Global” text and homeassistant should appear with the data listed.

1 Like

Thanks for the info to use Context Data.

How would the line of code look in get_entities to filter into a single message?

Lets say that the name given to my 3 entities in HA is:
1. light.hallway.lamp_1
2. light.hallway.lamp_2
3. light.hallway.lamp_3

Property: entity_id
is: homeAssistant.states {{ name: '*allwa*'}} ?

Thanks for the help this far :slight_smile:

Figuret out the way.

Property: entity_id
starts with: light.hall

Result: You get an array from 0-9 that will give you your entitiy_id of all entities that starts with light.hall

Thank for helping me WallyR
If there is an update where you can limit the search into zones, please hit me up again :) :slight_smile:

Good to see you figured it out.
I do not think areas will be available in NodeRed, since it seems to be only related to dashboards in HA.
The entities in HA do not have areas included, but rather the other way around, so areas have entities included.

This is a modified version of Get state_changed Events Based on Area | node-red-contrib-home-assistant-websocket

[{"id":"cb4710e25393bf2a","type":"inject","z":"892eaca92f0b4509","name":"kitchen","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"kitchen\"}","payloadType":"json","x":226,"y":512,"wires":[["631d2715a44c822a"]]},{"id":"1c1ad28af6565ae0","type":"debug","z":"892eaca92f0b4509","name":"debug 30","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":708,"y":512,"wires":[]},{"id":"631d2715a44c822a","type":"link call","z":"892eaca92f0b4509","name":"","links":["d8335751137d9b1f"],"linkType":"static","timeout":"30","x":502,"y":512,"wires":[["1c1ad28af6565ae0"]]},{"id":"25ea45b9a197ebee","type":"inject","z":"892eaca92f0b4509","name":"kitchen light","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"kitchen\", \"domain\": \"light\"}","payloadType":"json","x":246,"y":560,"wires":[["631d2715a44c822a"]]},{"id":"c8ee1c4ec5b54fad","type":"inject","z":"892eaca92f0b4509","name":"bedroom","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"bedroom\"}","payloadType":"json","x":236,"y":656,"wires":[["631d2715a44c822a"]]},{"id":"de1a3368a45da440","type":"inject","z":"892eaca92f0b4509","name":"bedroom switch","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"bedroom\", \"domain\": \"switch\"}","payloadType":"json","x":256,"y":704,"wires":[["631d2715a44c822a"]]},{"id":"b5d2b2181056ad85","type":"inject","z":"892eaca92f0b4509","name":"kitchen light on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\":\"kitchen\",\"domain\":\"light\",\"rules\":[{\"property\":\"state\",\"logic\":\"is\",\"value\":\"on\",\"valueType\":\"str\"}]}","payloadType":"json","x":256,"y":608,"wires":[["631d2715a44c822a"]]},{"id":"4b59abd714c85582","type":"inject","z":"892eaca92f0b4509","name":"not an area","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"not an area\"}","payloadType":"json","x":242,"y":464,"wires":[["631d2715a44c822a"]]},{"id":"206de99787a0ba25","type":"inject","z":"892eaca92f0b4509","name":"bedroom kitchen lights","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"bedroom,kitchen\", \"domain\": \"light\"}","payloadType":"json","x":276,"y":752,"wires":[["631d2715a44c822a"]]},{"id":"cdf27cc16137e71a","type":"inject","z":"892eaca92f0b4509","name":"bedroom kitchen lights switches","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"area\": \"bedroom,kitchen\", \"domain\": \"light, switch\"}","payloadType":"json","x":306,"y":800,"wires":[["631d2715a44c822a"]]},{"id":"fa7ec3d063bfa2f3","type":"group","z":"892eaca92f0b4509","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["86c9e07cb01eec87","399cdb2310ef8964","03595b0f5a9a1d79","56092aa4390d93bf","8f6065e3a6ca3da7","9227dd2dc5056d39","072d3e5fd5e540cb","55b25821a65fcdde","2f3c8ac155fe3383","ba967a32b033e303","fe32233ac9184597","1afd1e966bd0aeb7","45e4dae99ba35745","17ea8e982fa2d280","d8335751137d9b1f","d096314b2fe43404"],"x":114,"y":103,"w":1078,"h":322},{"id":"86c9e07cb01eec87","type":"ha-api","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"areas","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\": \"config/area_registry/list\"}","dataType":"json","responseType":"json","outputProperties":[{"property":"areas","propertyType":"msg","value":"","valueType":"results"}],"x":446,"y":336,"wires":[["03595b0f5a9a1d79"]]},{"id":"399cdb2310ef8964","type":"inject","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"Manual Update","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":252,"y":336,"wires":[["86c9e07cb01eec87"]]},{"id":"03595b0f5a9a1d79","type":"ha-api","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"devices","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\": \"config/device_registry/list\"}","dataType":"json","responseType":"json","outputProperties":[{"property":"devices","propertyType":"msg","value":"","valueType":"results"}],"x":586,"y":336,"wires":[["56092aa4390d93bf"]]},{"id":"56092aa4390d93bf","type":"ha-api","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"entities","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\": \"config/entity_registry/list\"}","dataType":"json","responseType":"json","outputProperties":[{"property":"entities","propertyType":"msg","value":"","valueType":"results"}],"x":730,"y":336,"wires":[["9227dd2dc5056d39"]]},{"id":"8f6065e3a6ca3da7","type":"debug","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1076,"y":336,"wires":[]},{"id":"9227dd2dc5056d39","type":"function","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"","func":"const entities = {};\n\nmsg.entities.forEach(e => {    \n    const areaId = e.area_id ?? (e.device_id && msg.devices.find(d => d.id === e.device_id).area_id);    \n    if(!areaId) {        \n        return;\n    }\n\n    const area = msg.areas.find(a => a.area_id === areaId);\n    if(area) {\n        entities[e.entity_id] = {\n            areaId: area.area_id,\n            name: area.name,\n            domain: e.entity_id.split(\".\")[0]\n        };\n    }\n});\n\nmsg.payload = entities;\nmsg.update = true;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":896,"y":336,"wires":[["8f6065e3a6ca3da7","072d3e5fd5e540cb"]]},{"id":"072d3e5fd5e540cb","type":"function","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"data","func":"if(msg.update) {\n    node.status({fill:\"green\", shape: \"dot\", text: \"data loaded\", })\n    context.set(\"data\", msg.payload);\n    return;\n}\nconst data = context.get(\"data\");\n\nif(!data) {\n    node.status({fill:\"red\", shape: \"ring\", text: \"no data\", })\n    msg.payload = false;\n    return msg;\n}\n\nconst area = toArray(msg.payload.area);\nconst entityDomain = toArray(msg.payload.domain);\n\nif(area.length === 0) {\n    msg.payload = [];    \n    return [null, msg];\n}\n\nconst ids = Object.entries(data).filter(([entityId, {name, domain}]) => {    \n    if (entityDomain.length && !entityDomain.includes(domain)) return false;\n    if(!area.includes(name.toLowerCase())) return false;\n\n    return true;\n}).map(i => i[0]);\n\nif(ids.length === 0) {\n    msg.payload = [];\n    return [null, msg];\n}\n\nmsg.payload.rules ??= [];\n\nmsg.payload = { \n    rules:  [...msg.payload.rules, \n        {\n            \"property\": \"entity_id\",\n            \"logic\": \"includes\",\n            \"value\": ids.join(\",\"),\n            \"valueType\": \"str\"\n        }\n    ]\n};\n\nreturn msg;\n\nfunction toArray(str = \"\") {\n    return str.toLowerCase().split(\",\").map(a => a.trim()).filter(i => i) ?? [];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":446,"y":384,"wires":[["d096314b2fe43404"],["17ea8e982fa2d280"]]},{"id":"55b25821a65fcdde","type":"server-events","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"On Connect","server":"","version":2,"eventType":"home_assistant_client","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"},{"property":"event_type","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":210,"y":288,"wires":[["2f3c8ac155fe3383"]]},{"id":"2f3c8ac155fe3383","type":"switch","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"connected","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"connected","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":402,"y":288,"wires":[["86c9e07cb01eec87"]]},{"id":"ba967a32b033e303","type":"server-events","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"entity_registry_updated","server":"","version":2,"eventType":"entity_registry_updated","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"},{"property":"event_type","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":240,"y":144,"wires":[["45e4dae99ba35745"]]},{"id":"fe32233ac9184597","type":"server-events","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"device_registry_updated","server":"","version":2,"eventType":"device_registry_updated","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"},{"property":"event_type","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":250,"y":192,"wires":[["45e4dae99ba35745"]]},{"id":"1afd1e966bd0aeb7","type":"server-events","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"area_registry_updated","server":"","version":2,"eventType":"area_registry_updated","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"},{"property":"event_type","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":240,"y":240,"wires":[["45e4dae99ba35745"]]},{"id":"45e4dae99ba35745","type":"trigger","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"Update at most every 10 secs","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":542,"y":192,"wires":[["86c9e07cb01eec87"]]},{"id":"17ea8e982fa2d280","type":"link out","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"return entities by area","mode":"return","links":[],"x":751,"y":384,"wires":[]},{"id":"d8335751137d9b1f","type":"link in","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"get entities by area","links":[],"x":234,"y":384,"wires":[["072d3e5fd5e540cb"]],"l":true},{"id":"d096314b2fe43404","type":"ha-get-entities","z":"892eaca92f0b4509","g":"fa7ec3d063bfa2f3","name":"","server":"","version":0,"rules":[],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":618,"y":384,"wires":[["17ea8e982fa2d280"]]}]

Hi,

Thanks for all the great solutions.
I´ve been tinkering with the template node and got to the result of:
What I need help with now is to split the msg.payload into an array that shows me the entity_id.

Is there a way of using a wildcard (*) to seach for a string between the characters {' ,}?
Maybe im going about it the wrong way?
But splitting with \n does not seem to work and doing a specific char.lenght is also not an option.

Hope for someone to help me with a nice solution :slight_smile:

msg.payload.split(",") might work.

Update,

So far I´ve been able to get all entities into arrays.

Now I´m looking for a way of filtering out my arrays that contains “light.”
Is there a way of filtering arrays like this?

The flow I provided above will do everything you’re looking for.

[{"id":"bcecc44371771bde","type":"inject","z":"892eaca92f0b4509","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":196,"y":944,"wires":[["3a290f1cdc6f0aea"]]},{"id":"3a290f1cdc6f0aea","type":"api-render-template","z":"892eaca92f0b4509","name":"","server":"","version":0,"template":"{{area_entities(\"kitchen\")}}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":340,"y":944,"wires":[["c9db89b15c73267f"]]},{"id":"063f0e8bc5d223f3","type":"debug","z":"892eaca92f0b4509","name":"debug 31","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1244,"y":944,"wires":[]},{"id":"5fb0c165de1d4773","type":"json","z":"892eaca92f0b4509","name":"","property":"payload","action":"","pretty":false,"x":670,"y":944,"wires":[["0bd8a71aa0400c45"]]},{"id":"c9db89b15c73267f","type":"change","z":"892eaca92f0b4509","name":"replace ' with \"","rules":[{"t":"change","p":"payload","pt":"msg","from":"'","fromt":"str","to":"\"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":512,"y":944,"wires":[["5fb0c165de1d4773"]]},{"id":"0bd8a71aa0400c45","type":"split","z":"892eaca92f0b4509","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":802,"y":944,"wires":[["35e178b4c6ee2ca1"]]},{"id":"35e178b4c6ee2ca1","type":"switch","z":"892eaca92f0b4509","name":"matches light","property":"payload","propertyType":"msg","rules":[{"t":"regex","v":"^light\\.","vt":"str","case":false}],"checkall":"true","repair":true,"outputs":1,"x":950,"y":944,"wires":[["b24b40e9bc6c5edd"]]},{"id":"b24b40e9bc6c5edd","type":"join","z":"892eaca92f0b4509","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1102,"y":944,"wires":[["063f0e8bc5d223f3"]]}]
2 Likes

What a great solution!

Thank you so much Kermit :slight_smile:

Exactly what I was looking for! Works great for me too

Kermit,

Is there a way to inject an entity_id (i.e. light.office_lights) into the render template node so that it spits out the area that the entity came from?

Basically: {{area_id("ENTITY_ID_GOES_HERE")}} --> output: 'Kitchen' (EDITED)

I can get this template to work in a script within HA, but not in Node Red.

Here’s a further clarification.

When I activate a scene from a z-wave light switch, this is the payload. Is there a way for node-red to get the AREA name from the data here? The device_id seems to be the right thing to use, but I can’t get node red to get the area name from a render template.

The render template node accepts the template from the input msg.template. https://zachowj.github.io/node-red-contrib-home-assistant-websocket/node/render-template.html#template

Build the template in a change node and pass it to the render template node.

[{"id":"72b6188868eb2cbf","type":"inject","z":"c89d915bdff0f798","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": {\"device_id\": \"3d7f9bf737bb23d5e851be261fd2b355\"}}","payloadType":"json","x":334,"y":912,"wires":[["9f0d4ca25f74e739"]]},{"id":"42a3688bbe1c8c2b","type":"debug","z":"c89d915bdff0f798","name":"debug 103","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":842,"y":912,"wires":[]},{"id":"9f0d4ca25f74e739","type":"change","z":"c89d915bdff0f798","name":"","rules":[{"t":"set","p":"template","pt":"msg","to":"'{{area_id(\"' & payload.event.device_id & '\")}}'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":912,"wires":[["6f7d0fb35db215ab"]]},{"id":"6f7d0fb35db215ab","type":"api-render-template","z":"c89d915bdff0f798","name":"","server":"","version":0,"template":"","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":668,"y":912,"wires":[["42a3688bbe1c8c2b"]]}]

thank you Kermit! this change node was what i was really struggling with.

Kermit, with your help on that change node, I’ve also been able to extract the entities related to the device. So now, if there is a z-wave event:

  1. the area the event happened in - i have this now (thanks to you)
  2. the device that was used - i have this now (thanks to you)
  3. My Goal: the friendly name of the entity that triggered the event (i.e. “Office Lights”) - don’t have this yet.

If there is a better way to do what I’m trying to do, let me know. For now, here’s the output after your help:

These inovelli switches have several entities, the entity_id of the light switch is the last in the list…but it may not always be last in the list. How can I finally get to the holy grail of a friendly name!?

Cheers!

[{"id":"dc3b6a96aa52c9f8","type":"change","z":"7a48450ad19c27ab","name":"","rules":[{"t":"set","p":"template","pt":"msg","to":"'{{device_entities(\"' & payload.event.device_id & '\")}}'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":464,"y":1615,"wires":[["124aa5d83ba6c992"]]},{"id":"124aa5d83ba6c992","type":"api-render-template","z":"7a48450ad19c27ab","name":"","server":"181266bf.748f49","version":0,"template":"","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":638,"y":1615,"wires":[["b665677f6bc7e3ad"]]},{"id":"b665677f6bc7e3ad","type":"debug","z":"7a48450ad19c27ab","name":"debug 104","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":812,"y":1615,"wires":[]},{"id":"050b84e42281b4b2","type":"inject","z":"7a48450ad19c27ab","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": {\"device_id\": \"5897334658b0719fcfa8d7a9fc203e88\"}}","payloadType":"json","x":305,"y":1615,"wires":[["dc3b6a96aa52c9f8"]]},{"id":"181266bf.748f49","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]