With the release of version 0.5.0 of node-red-contrib-home-assistant-websocket a new node was introduced.
Here are three examples of how one might use the new get entities
node.
Example #1
You have a presence detection of some sort running in Home Assistant and you want to get a notification when you leave if any doors or windows are left open.
Using the get entities
node here to get a possible list of entity ids [binary_sensor.front_door, binary_sensor.back_door, binary_sensor.front_window, binary_sensor.back_window] if their state is equal to open
. The entities are returned with the output Split
. This means that a message is sent for each valid entity. We then are using a template node to format the payload into the entity friendly name and joining them back into one payload using the join
node.
[{"id":"22439fca.156c1","type":"trigger-state","z":"74ddc16c.19655","name":"Left Home","server":"351c96ee.9dc3aa","entityid":"device_tracker.jason","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"9i2c9sz7d3e","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"3h3lghs8xsm","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"not_home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":102,"y":928,"wires":[["24ecbd00.7147a4"],[]]},{"id":"24ecbd00.7147a4","type":"ha-get-entities","z":"74ddc16c.19655","server":"351c96ee.9dc3aa","name":"","rules":[{"property":"entity_id","logic":"includes","value":"binary_sensor.front_door,binary_sensor.back_door,binary_sensor.front_window,binary_sensor.back_window","valueType":"str"},{"property":"state","logic":"is","value":"open","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":268,"y":928,"wires":[["2fe88645.50ce3a"]]},{"id":"2fe88645.50ce3a","type":"template","z":"74ddc16c.19655","name":"Format Friendly Name","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":472,"y":928,"wires":[["792cffb3.55b65"]]},{"id":"792cffb3.55b65","type":"join","z":"74ddc16c.19655","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":",","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":642,"y":928,"wires":[["c829053e.128658"]]},{"id":"c829053e.128658","type":"api-call-service","z":"74ddc16c.19655","name":"Notify","server":"351c96ee.9dc3aa","service_domain":"notify","service":"push_jason","data":"{\"message\": \"The {{payload}} are open.\",\"title\": \"Left Open\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":770,"y":928,"wires":[[]]}]
Example #2
Sort of a Vacation or Away script to randomly turn on some lights around your home.
Using an inject
node here but you could use your own preference of timer node. The get entities
node is randomly choosing one entity from the criteria where entity_id starts with light.
.
[{"id":"66c50c00.a3c8c4","type":"inject","z":"74ddc16c.19655","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":112,"y":1056,"wires":[["87111b5d.423eb8"]]},{"id":"87111b5d.423eb8","type":"ha-get-entities","z":"74ddc16c.19655","server":"351c96ee.9dc3aa","name":"","rules":[{"property":"entity_id","logic":"starts_with","value":"light.","valueType":"str"}],"output_type":"random","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":262,"y":1056,"wires":[["f1234263.eed69"]]},{"id":"541ec87b.02a788","type":"delay","z":"74ddc16c.19655","name":"","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"20","randomLast":"30","randomUnits":"minutes","drop":false,"x":592,"y":1056,"wires":[["5cec6ee6.cfa5a"]]},{"id":"f1234263.eed69","type":"api-call-service","z":"74ddc16c.19655","name":"Turn on Light","server":"351c96ee.9dc3aa","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"{{payload.entity_id}}\"}","render_data":true,"mergecontext":"","output_location":"","output_location_type":"none","x":422,"y":1056,"wires":[["541ec87b.02a788"]]},{"id":"5cec6ee6.cfa5a","type":"api-call-service","z":"74ddc16c.19655","name":"Turn off Light","server":"351c96ee.9dc3aa","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"{{payload.entity_id}}\"}","render_data":true,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":758,"y":1056,"wires":[[]]}]
Example #3
On Reddit the other day a user posted this How can I join 1 to 4 pre-defined messages together based on 4 separate entity states? (Their solution can be found in the post)
Hereās my take on it using the get entities
and a function
node. Using the Array
output option here.
[{"id":"531d586f.103188","type":"server-state-changed","z":"74ddc16c.19655","name":"","server":"351c96ee.9dc3aa","entityidfilter":"binary_sensor.button_bedroom","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":196,"y":752,"wires":[["27db4cc4.71e694"]]},{"id":"27db4cc4.71e694","type":"ha-get-entities","z":"74ddc16c.19655","server":"351c96ee.9dc3aa","name":"","rules":[{"property":"entity_id","logic":"includes","value":"sensor.phon_charging,sensor.watch_charging,sensor.kaylas_phone_charging,lock.lock","valueType":"str"},{"property":"state","logic":"includes","value":"no,unlocked","valueType":"str"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":470,"y":752,"wires":[["f3fe6e2f.35266"]]},{"id":"f3fe6e2f.35266","type":"function","z":"74ddc16c.19655","name":"","func":"let lockFD = null;\nlet PC = false;\nlet WC = false;\nlet KP = false;\nlet FD = false;\n\nmsg.payload.forEach(entity => {\n switch(entity.entity_id) {\n case \"sensor.phone_charging\":\n PC = true;\n break;\n case \"sensor.watch_charging\":\n WC = true;\n break;\n case \"sensor.kaylas_phone_charging\":\n KP = true;\n break;\n case \"lock.lock\":\n FD = true;\n lockFD = { payload: true };\n break;\n }\n \n});\n\nlet message = \"Goodnight.\";\nif(PC && WC) {\n message = `${message} Donāt forget to charge your phone and watch.`;\n} else {\n message = `${message} Donāt forget to charge your ${PC ? 'phone' : 'watch'}.`\n}\nif(KP) {\n message = `${message} Remind Kayla to charge her phone${PC ? ' too':''}.`;\n}\nif(FD) {\n message = `${message} The door is now locked.`\n}\nmsg.payload = message;\nreturn [msg, lockFD];\n","outputs":2,"noerr":0,"x":610,"y":752,"wires":[["d0e80507.2ac3c8"],["52de5e56.e74c6"]]},{"id":"d0e80507.2ac3c8","type":"cast-to-client","z":"74ddc16c.19655","name":"","url":"","contentType":"","message":"","language":"en","ip":"192.168.1.29","port":"","volume":"40","x":770,"y":752,"wires":[[]]},{"id":"52de5e56.e74c6","type":"api-call-service","z":"74ddc16c.19655","name":"lock door","server":"351c96ee.9dc3aa","service_domain":"scene","service":"turn_on","data":"{\"entity_id\":\"scene.lock_door\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":780,"y":800,"wires":[[]]}]
Disclaimer: All these examples are untested but should give you a general idea on how to use it.