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.