Implementing two inputs in Node-RED

Pankesh picture Pankesh · Dec 26, 2015 · Viewed 30.3k times · Source

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

Answer

hardillb picture hardillb · Dec 26, 2015

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