Binary sensor node with variable Entity Config

Hi,

I’m trying to update the status of binary sensors based on incoming UDP packages.
Given the high amount of sensors (> 100), I want to optimize my flow, avoiding switches with many items.

This is what I currently have:

I created my flow with 4 binary sensors (I will add all the others when everything is fully working).

I am currently using the switch-node and the binary sensor node. The switch node will update the proper sensor based on “payload.zone_id”.

But what I actually want to achieve is that, using the “Get Entities” node, the right binary sensor is identified (based on zone_id attribute), and that this sensor is updated with the right status.

Theoretically, it would be possible if the output of the “Get Entities” node could be passed to the “Entity Config” field of the binary sensor.

Is anything like this possible?

Look at this flow to get the data from the global context variable.

[{"id":"f57d7589dd5a9b5c","type":"inject","z":"be03f9025a1f137a","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":235,"y":75,"wires":[["fcb8501ba4e0c02b"]]},{"id":"fcb8501ba4e0c02b","type":"function","z":"be03f9025a1f137a","name":"function 1","func":"msg.sun = global.get('homeassistant').homeAssistant.states[\"sun.sun\"];\nmsg.sun_state = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].state;\nmsg.sun_attr_next_dawn = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].attributes.next_dawn;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":375,"y":75,"wires":[["36f148efe227cbac"]]},{"id":"36f148efe227cbac","type":"debug","z":"be03f9025a1f137a","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":525,"y":75,"wires":[]}]

You can see the values yourself by opening up the Context Data menu in the upper right corner of Node Red and then click the refresh icon next to the Global headline.

You might also be able to use a dynamically set up sensor.
I have no idea if this can just be made a binary sensor by adding binary to its name, but else a normal sensor might also work in your case.
Just remember this is not a permanent sensor, so it will not be available in HA before it has been populated by NodeRed.

[{"id":"25ef1c9c74581477","type":"ha-api","z":"be03f9025a1f137a","name":"sensor","server":"541ade28.b4a62","version":1,"debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":800,"y":660,"wires":[[]]},{"id":"06ac8e03a8a85876","type":"function","z":"be03f9025a1f137a","name":"make final payload","func":"msg.entity_id = `sensor.weather_alert`;\n\nmsg.payload = {\n    data: {\n        state: '2',\n        attributes: {\n            'name': 'test_entity3',\n            'entries': [\n                {'title':'WEATHER ADVISORY, Barrie'},\n                {'title': 'SPECIAL WEATHER STATEMENT, Barrie'}\n            ],\n            friendly_name: 'Test Entity 3',\n            icon: 'mdi:pulse'\n        }\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":660,"wires":[["25ef1c9c74581477","fee8ca78ceac8578"]]},{"id":"39963ed0628d85b4","type":"inject","z":"be03f9025a1f137a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":450,"y":660,"wires":[["06ac8e03a8a85876"]]},{"id":"541ade28.b4a62","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
1 Like

Thanks, using the API node that could indeed work.
(the example above was with the “Binary Sensor Node” from HA.

I’m also experimenting with sending updates using the MQTT integration. It allows me to define the entities in a permanent way in HA, and update them easily via NR.