How to enumerate device rooms, areas, labels, etc in Node-red?

With this great update reagrding rooms/lables is there a way that we can enumerate all of these rooms, floors, labels in Node-red?

Would be great to be able to label many devices as “standby”, filter them all with that and execute one action…

Can someone point me in the correct direction to archive this please?

Building on the following post:

https://community.home-assistant.io/t/query-hass-labels-in-node-red/719254

This flow should get you an array of all labels (alphabetically sorted), and for each label, an array of all entities (alphabetically sorted).

Should be just a case of picking out the label to get an array of entities…

[{"id":"9d6d7802931a9809","type":"ha-api","z":"95dab86a0bf42ced","name":"entities","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\": \"config/entity_registry/list\"}","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":360,"y":2680,"wires":[["8d5767b31ae2f3e6"]]},{"id":"7a2d563e5a739c19","type":"inject","z":"95dab86a0bf42ced","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":2680,"wires":[["9d6d7802931a9809"]]},{"id":"902ab6d88884d28c","type":"debug","z":"95dab86a0bf42ced","name":"debug 173","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":2680,"wires":[]},{"id":"8d5767b31ae2f3e6","type":"change","z":"95dab86a0bf42ced","name":"List of labels with matching entities","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t    $withlab:=payload[$count(labels)>0];\t    $labs:=$distinct($withlab.labels)^($);\t    \t    $labs.(\t        $l:=$;\t        {\"label\": $l,\t        \"entities\": $withlab[$l in labels].entity_id^($)}\t        );\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":2680,"wires":[["902ab6d88884d28c"]]}]
1 Like

Thank You! Amazing!

Is there a way to do the same for rooms please?

HA does not have ‘rooms’ but rather areas.

There are functions in the WebSocket nodes for returning areas, area devices, and area entities.

These extra functions, and how to use them, are documented at

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/cookbook/jsonata/functions.html#using-areas-areadevices-and-areaentities

including an example flow with JSONata to count devices and entities for each area.

The following, however, will return an array of areas, each with a count of devices, and an array of entities.

The possibilities are endless…

[{"id":"c296756ac22d70ec","type":"api-current-state","z":"e6f8ea0317d4f26a","name":"Get areas and entities","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sun.sun","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$areas()^(name).{\t    \"Area\": name,\t    \"Devices\" : $count($areaDevices(area_id)),\t    \"Entities\": $areaEntities(area_id).entity_id^($)\t    }","valueType":"jsonata"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":2560,"wires":[["2dca7b1b1b75c7ee"]]},{"id":"f96a201664c37de7","type":"inject","z":"e6f8ea0317d4f26a","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":2560,"wires":[["c296756ac22d70ec"]]},{"id":"2dca7b1b1b75c7ee","type":"debug","z":"e6f8ea0317d4f26a","name":"debug 449","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":2560,"wires":[]}]

Thank you so much!
Appreciated!

Here’s more discussion on how to filter by labels or add them to get entities results.

So beginning to play with this flow which is intended to find all items with device_class: door whose label is NOT “not_hvac”

[{"id":"0a7869dabd2e4832","type":"subflow","name":"Add on labels","info":"","category":"","in":[{"x":64,"y":80,"wires":[{"id":"6ada5d0d666e7b3f"}]}],"out":[{"x":528,"y":80,"wires":[{"id":"2c328fb139b08339","port":0}]}],"env":[{"name":"property","type":"str","value":"payload","ui":{"label":{"en-US":"entities property"},"type":"input","opts":{"types":["str","env"]}}}],"meta":{},"color":"#46B1EF","icon":"node-red/debug.svg"},{"id":"6ada5d0d666e7b3f","type":"ha-api","z":"0a7869dabd2e4832","name":"entities","server":"6e751b1b.8f17c4","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\": \"config/entity_registry/list\"}","dataType":"json","responseType":"json","outputProperties":[{"property":"registry","propertyType":"flow","value":"","valueType":"results"}],"x":196,"y":80,"wires":[["2c328fb139b08339"]]},{"id":"2c328fb139b08339","type":"function","z":"0a7869dabd2e4832","name":"add on labels","func":"msg[env.get(\"property\")] = msg[env.get(\"property\")].map((entity) => {\n    const labels = flow.get(\"registry\").find((ele) => ele.entity_id === entity.entity_id).labels;\n    return {...entity, labels};\n});\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":364,"y":80,"wires":[[]]},{"id":"6e751b1b.8f17c4","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},{"id":"6fe02475225be313","type":"debug","z":"d5845786be3062dc","name":"debug 180","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"abc","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":500,"wires":[]},{"id":"7dd21d25f0626608","type":"subflow:0a7869dabd2e4832","z":"d5845786be3062dc","name":"","env":[{"name":"property","value":"abc","type":"str"}],"x":508,"y":500,"wires":[["6fe02475225be313"]]},{"id":"8fd5df25b95541f4","type":"ha-get-entities","z":"d5845786be3062dc","name":"","server":"6e751b1b.8f17c4","version":1,"rules":[{"property":"attributes.device_class","logic":"is","value":"door","valueType":"str"}],"outputType":"array","outputEmptyResults":false,"outputLocationType":"msg","outputLocation":"abc","outputResultsCount":1,"x":310,"y":500,"wires":[["7dd21d25f0626608"]]},{"id":"e6dfe0bcc40a2e92","type":"inject","z":"d5845786be3062dc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":161,"y":500,"wires":[["8fd5df25b95541f4"]]}]

When I run the above flow, I see the label as shown below, so my question is how do I filter those devices without this label?

image

[{"id":"0a7869dabd2e4832","type":"subflow","name":"Add on labels","info":"","category":"","in":[{"x":64,"y":80,"wires":[{"id":"6ada5d0d666e7b3f"}]}],"out":[{"x":528,"y":80,"wires":[{"id":"2c328fb139b08339","port":0}]}],"env":[{"name":"property","type":"str","value":"payload","ui":{"label":{"en-US":"entities property"},"type":"input","opts":{"types":["str","env"]}}}],"meta":{},"color":"#46B1EF","icon":"node-red/debug.svg"},{"id":"6ada5d0d666e7b3f","type":"ha-api","z":"0a7869dabd2e4832","name":"entities","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\"type\": \"config/entity_registry/list\"}","dataType":"json","responseType":"json","outputProperties":[{"property":"registry","propertyType":"flow","value":"","valueType":"results"}],"x":196,"y":80,"wires":[["2c328fb139b08339"]]},{"id":"2c328fb139b08339","type":"function","z":"0a7869dabd2e4832","name":"add on labels","func":"msg[env.get(\"property\")] = msg[env.get(\"property\")].map((entity) => {\n    const labels = flow.get(\"registry\").find((ele) => ele.entity_id === entity.entity_id).labels;\n    return {...entity, labels};\n});\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":364,"y":80,"wires":[[]]},{"id":"6fe02475225be313","type":"debug","z":"120358abd7c22d30","name":"debug 180","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"abc","targetType":"msg","statusVal":"","statusType":"auto","x":1242,"y":7328,"wires":[]},{"id":"7dd21d25f0626608","type":"subflow:0a7869dabd2e4832","z":"120358abd7c22d30","name":"","env":[{"name":"property","value":"abc","type":"str"}],"x":656,"y":7328,"wires":[["ea0d829f0ec59cec"]]},{"id":"8fd5df25b95541f4","type":"ha-get-entities","z":"120358abd7c22d30","name":"","server":"","version":1,"rules":[{"property":"attributes.device_class","logic":"is","value":"door","valueType":"str"}],"outputType":"array","outputEmptyResults":true,"outputLocationType":"msg","outputLocation":"abc","outputResultsCount":1,"x":470,"y":7328,"wires":[["7dd21d25f0626608"]]},{"id":"e6dfe0bcc40a2e92","type":"inject","z":"120358abd7c22d30","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":297,"y":7328,"wires":[["8fd5df25b95541f4"]]},{"id":"ea0d829f0ec59cec","type":"split","z":"120358abd7c22d30","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"abc","x":830,"y":7328,"wires":[["399ee147186fe366"]]},{"id":"399ee147186fe366","type":"switch","z":"120358abd7c22d30","name":"","property":"abc.labels","propertyType":"msg","rules":[{"t":"cont","v":"not_hvac","vt":"str"},{"t":"else"}],"checkall":"true","repair":true,"outputs":2,"x":958,"y":7328,"wires":[[],["cf720f7518cd7f82"]]},{"id":"cf720f7518cd7f82","type":"join","z":"120358abd7c22d30","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1086,"y":7328,"wires":[["6fe02475225be313"]]}]