Implementation of two inputs in Node-RED

In my current project, we are trying to implement the current application functionality using Node -RED . The following is the functionality. Here, the Fire state receives two inputs: (1) TemperatureSensor (2) SmokeDetector . Both sensors publish data using MQTT publishers. and the Firestate component can receive data through the MQTT subcyber.

The fire condition can produce a result based on these two parameters, which if temperaturevalue > 70 and Smokevalue == true . In this regard, my question is: Does Node-RED support the functionality of two inputs? If so, how can we implement this functionality? If not, then .. Can I say that two input functions cannot be implemented with Node -RED ???? As we have seen, Node-RED provides several outputs, but not inputs .

enter image description here

+5
source share
2 answers

You will need to use the node function and use the context variable to maintain state between messages and use the subject of the message to determine what input the message came from.

Something like that:

 context.temp = context.temp || 0.0; context.smoke = context.smoke || false; if (msg.topic === 'smokeDetector') { context.smoke = msg.payload; } else if (msg.topic === 'tempSensor') { context.temp = msg.payload; } if (context.temp >= 70.0 && context.smoke) { return {topic: 'fireState', payload: 'FIRE!'} } else { return null } 

More information can be found in the node doc function here

+10
source

You can connect any number of inputs to any node - just keep in mind that your node will only see one input message at a time. There is no built-in msg aggregation simply because there are several input wires.

Instead, the task of aggregating multiple input msgs is handled by specific nodes, some of which are built into the main node server, and some of them have been contributed by the community. Which one to choose depends on the specific use case. For example, should two objects be added to an array or combined into one large object? Only you will know what you want - node -red makes no assumptions, but gives you different nodes to handle many common use cases. For any other use case, there is always a generic function node in which you can use javascript to implement whatever behavior you need.

For your initial question, you are looking for a way to merge 2 useful data from different sensors into one object. The main join and change nodes, as well as the node-red-contrib-bool-gate and node-red-contrib-aggregator found in the thread library, can be used for this.

Here's an example of combining two sensor inputs using a join node, and then using a switch node with the expression payload.temp > 70 and payload.smoke to determine whether to send a message by tag:

[{"ID": "87df68f8.51ad58", "type": "inject", "g": "f9a2eec9.c2e26", "name": "," theme ":" smoke "," payload ":" true "," PayloadType ":" BOOL "," repeat ":", "crontab": "," once "false" onceDelay ": 0.1" x ": 160," y ": 1180," wires " : [["da4182a8.47939"]]}, {"identifier": "3ad419ec.1453a6", "type": "inject", "g": "f9a2eec9.c2e26", "name": "," topic " : "smoke", "payload": "false", "PayloadType": "BOOL", "repeat": "," crontab ":", "when" false "onceDelay": 0.1 "x": 170 , "y": 1140, "wires": [["da4182a8.47939"]]}, {"identifier": "a45b3cb0.f3312", "type": "inject", "g": "f9a2eec9.c2e26" , "name": "," topic ":" pace "," payload ":" 65 "," PayloadType ":" number "," repeat ":", " crontab ":", "once" false "onceDelay": 0.1 "x": 160, "y": 1220, "wires": [["da4182a8.47939"]]}, {"identifier": " a3b07d81.e6b17 "," type ":" inject "," g ":" f9a2eec9.c2e26 "," name ":", "theme": "pace", "payload": "75", "PayloadType": "Num" "repeat": "," crontab ":", "once" false "onceDelay": 0.1 "x": 160, "y": 1260, "wires": [["da4182a8. 47939 "]]}, {" id ":" da4182a8.47939 "," type ":" join "," z ":" f9a2eec9.c2e26 "," name ":" join payloads "," mode ":" custom "" Buoy ld ":" object "" property ":" payload "," PropertyType ":" message "," key ":" topic "," carpenter ":" \ n "," joinerType ":" str "," accumulate " : true, timeout: "," counting ":" 2 "," reduceRight "false" reduceExp ":", "reduceInit": "," reduceInitType ":", "reduceFixup": "," x " : 430, "y": 1200, "wires": [["" 315c9ce3.570d64 "," 50f981b4.be654 "]]}, {" identifier ":" 315c9ce3.570d64 "," type ":" switch "," z ":" f9a2eec9.c2e26 "," name ":" Trigger Alarm? "," property ":" payload.temp> 70 and payload.smoke "," propertyType ":" jsonata ", rules": [{"T ":" true "}]," checkall ":" true "," repair "false" outputs ": 1," X ": 640," y ": 1200," wires ": [[" "50f981b4.be654"] ]}, {"identifier": "50f981b4.be654", "type": "debug", "g": "f9a2eec9.c2e26", "name": "," ac ivny ": true," tosidebar ": true," console "false" tostatus "false" full ":" false "," x ": 690," y ": 1260," Wire ": []}]

0
source

Source: https://habr.com/ru/post/1239260/


All Articles