Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing two inputs in Node-RED

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

The fire state can produce an output based on the these two parameters that is if temperaturevalue > 70 and Smokevalue == true. In view of this, my question is -- Does Node-RED support the two inputs functionality? If yes, then how can we implement this functionality? If no, then.. Can I say that two input functionality can not be implemented using Node-RED???? As we have seen that Node-RED provides multiple outputs, but not inputs.

enter image description here

like image 770
Pankesh Avatar asked Dec 26 '15 17:12

Pankesh


People also ask

How do you organize Node-RED flows?

The main method of organising flows in Node-RED is by separating them across multiple tabs within the editor. There are a few different strategies that can be used to do that. If you can identify separate logical components of your application, consider putting them on separate tabs.

What input format does Node-RED accept?

The function node is used to run JavaScript code against the msg object. The function node accepts a msg object as input and can return 0 or more message objects as output. This message object must have a payload property (msg.


1 Answers

You will need to use a function node and make use of the context variable to keep state between messages and use the message topic to determine which input a message came from.

Something like this:

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 details can be found in the function node doc here

like image 132
hardillb Avatar answered Oct 27 '22 03:10

hardillb