How-to
Combine two MQTT sensor inputs in one Node-RED calculation
Short answer
Node-RED accepts many wires into one node but delivers each message separately, so combine the two MQTT streams explicitly. Use a Join node in manual mode with a message count of 2 and msg.topic as the key, or a Function node that stores the latest temperature and smoke flag in context and evaluates both on every arrival.

On this page
- Two wires into one node still arrive one message at a time
- Build the flow end to end
- Configure a Join node in manual mode
- Apply the rule with a Switch node
- Hold the pair in a Function node with context
- Worked example: temperature 75 with smoke true, then exactly 70
- Test the combined decision before you rely on it
- Questions asked
Two wires into one Node-RED node do not make a two-input record: the node still receives one message per arrival, so the fire-state logic has to combine the two MQTT streams explicitly. Combine them in one of two ways. A Join node in manual mode builds one object from both topics — set the mode to manual, build a key/value object with msg.payload as the value and msg.topic as the key, and set the message count to 2. A Function node holds the latest temperature (number) and smoke flag (boolean) in its context and evaluates the rule on every arrival. This article builds both, works the fire rule (temperature above 70, smoke flag true) at a real reading, and lists the inject tests that prove the combined decision before the flow publishes.
Two wires into one node still arrive one message at a time #
Node-RED delivers each arriving message separately, even when several wires enter the same node; no wiring pattern aggregates by itself. The combining job belongs to a node whose purpose is combining: the core Join node, or a Function node that keeps its own state.
The Join node's automatic mode attempts to reverse a previous Split node, so it is the wrong tool for two unrelated sensor streams; manual mode lets you set the value, the key and the trigger yourself. A Function node exposes three predefined scopes — context local to the node, flow for the tab, global for the whole runtime — and the example below uses the node-local scope so the stored pair stays with the node that evaluates it.
Both publishers reach Node-RED through an MQTT broker: each publisher sends to the broker, the broker fans every publication out to matching subscriptions, and each MQTT input node receives its messages one at a time. Nothing publishes directly from a sensor into a Join or Function node.
Build the flow end to end #
- Point two MQTT input nodes at your broker, one subscribed to the temperature topic and one to the smoke topic. Read the broker address, listener port and credentials from the installation's own configuration.
- Prove both streams arrive before adding logic: wire each input to a Debug node set to show the complete message, publish once from each sensor, and confirm msg.topic and msg.payload look right. If a topic never arrives, sort the subscription out first — Fix MQTT sensor data missing in Node-RED walks through that fault.
- Normalize the inputs. If the broker topics are longer than the key names you want downstream, put a Change node after each subscription to set msg.topic to the short key — temp or smoke in this example. That renames the flow's message field, not the broker topic. Convert payloads so the temperature arrives as a number and the smoke flag as a boolean.
- Aggregate the two streams: a Join node in manual mode, or the Function node with context shown below.
- Evaluate the rule on the combined pair and let only the true result leave the branch.
- Run the inject tests in the worked example and the Function section, then connect the output to the MQTT output node or whatever consumes the fire state.
Configure a Join node in manual mode #
Set the Join node to manual mode, combine the messages into a key/value object, take each value from msg.payload and each key from msg.topic, and trigger after a fixed count of 2 messages. Leave the timeout unset: a timeout would push out a half-filled object when one sensor goes quiet.
| Setting | Value | Effect |
|---|---|---|
| Mode | Manual | Joins unrelated streams without a Split upstream |
| Output | Key/value object | Named fields instead of array positions |
| Value | msg.payload | Stores each sensor reading |
| Key | msg.topic | Splits temperature and smoke into separate keys |
| Count | 2 messages | Emits once both arrivals are in |
| Timeout | unset | Emits nothing early when a sensor goes quiet |
Counting counts arrivals, not sensors: two temperature updates in a row satisfy the count, and the emitted object then carries a temperature key and no smoke key, so the rule evaluates false. Treat silence from the rule as unknown, not as a confirmed no-fire.
Join send behavior after the count changed in Node-RED 4.0 and 4.0.1, so prove it on your own release: inject a numeric 65 on the temperature topic and boolean false on the smoke topic, and confirm one combined object leaves the node with payload.temp at 65 and payload.smoke false. Then inject a second temperature update alone. A build set to emit on each subsequent arrival sends an updated object that keeps the stored flag — the accumulating latest-value behavior this rule wants. A build that re-arms for a fresh pair sends nothing until smoke publishes again; if that is yours, change the subsequent-message setting in the node or use the Function path, which never waits for pairs.
Apply the rule with a Switch node #
Wire the Join output into a Switch node, set the node to evaluate a JSONata expression, and let the message through when the expression is true:
payload.temp > 70 and payload.smoke
The expression is strict about the threshold — a reading exactly at it evaluates false — and it passes only when the smoke key holds boolean true. The reading's unit comes from the sensor publisher's documentation: the comparison is unit-blind, so a threshold written for degrees Celsius means something else against a Fahrenheit publisher.
Check types before this node. The boolean false fails the expression, but the string "false" is a nonempty string and passes as truthy — that would announce a fire on a false flag.
| Temperature | Smoke flag | Switch passes | Why |
|---|---|---|---|
| 65 | true | No | Below the threshold |
| 70 | true | No | Strict comparison: equal is not above |
| 75 | false | No | Flag is false |
| 75 | true | Yes | Both conditions hold |
A Switch routes the message; it does not rewrite it. Add a Change node on the true branch to set msg.topic to fireState and msg.payload to FIRE!, then wire that to the MQTT output node after the tests below.
Hold the pair in a Function node with context #
Wire both MQTT input nodes into the same Function node. It sees one message per arrival, identifies the sender by msg.topic, stores that value in context, and evaluates the stored pair on every arrival. The example expects topic tempSensor with a numeric payload and topic smokeDetector with a boolean payload — the two component names, case-sensitive like any topic and any JavaScript comparison:
if (msg.topic === 'tempSensor') {
if (typeof msg.payload !== 'number' || !Number.isFinite(msg.payload)) {
return null;
}
context.set('temp', msg.payload);
} else if (msg.topic === 'smokeDetector') {
if (typeof msg.payload !== 'boolean') {
return null;
}
context.set('smoke', msg.payload);
} else {
return null;
}
if (context.get('temp') > 70 && context.get('smoke') === true) {
return { topic: 'fireState', payload: 'FIRE!' };
}
return null;
Both context keys start unset; temp becomes a number and smoke a boolean only when a valid message sets them. Until then the comparison reads undefined and stays false, so the node emits nothing until both sensors have reported — no assumed zero temperature and no assumed clear flag doing the deciding. The type checks earn their place too: a string "false" is rejected as not a boolean, and a non-numeric reading is rejected by the finite check.
The test that proves it: inject tempSensor with payload 75 and watch nothing leave the node, because smoke is unset; inject smokeDetector with payload true and the fireState message appears; inject smokeDetector with payload false and it goes quiet.
Worked example: temperature 75 with smoke true, then exactly 70 #
Inputs: a temperature reading of 75, taken in degrees Celsius for this example — the publisher's documentation is what fixes the real unit, published on the temperature topic through the broker; a smoke flag of true, boolean, on the smoke topic; the threshold 70, strictly above, from the fire-state requirement.
Rule in words: publish the fire state when the stored temperature is strictly above the threshold and the stored smoke flag equals boolean true.
Arithmetic: 75 > 70 gives true; true equals true gives true; true and true gives true — both conditions hold, so the flow emits the fireState message with the FIRE! payload. Run the same pair at exactly the threshold: 70 > 70 gives false, and false and true gives false, so nothing is emitted.
Result: one fire-state message at 75 with the flag true; silence at exactly 70 with the flag true under the strict comparison.
Decision: above 70 with the smoke flag true, publish the fire state; at exactly 70, stay silent — unless the requirement counts the threshold itself, in which case change the comparison to >= and that one reading flips to fire. Decide it once and write the comparison into the flow.
Test the combined decision before you rely on it #
- Inspect the complete message at a Debug node, not the compact payload view: both keys present, temperature a number, smoke flag a boolean.
- Test the false path with real types: boolean false must leave the rule false, and the string "false" must not sneak through as truthy.
- Test the half pair on the Join path: two temperature updates in a row must not produce a rule output.
- Test the stale case: stop one publisher and watch the rule. Context and the accumulated object keep the last value, so a stored above-threshold reading with a true flag keeps firing after the sensor dies — the staleness mechanism behind why a Node-RED dashboard shows stale MQTT sensor values.
- Keep the boundary in view: this flow is supervisory logic on the PC side — an alarm, a dashboard entry, a log line. It is not a certified fire-detection or protection system, and life-safety stays with the equipment designed and certified for it.
Decision across both paths: Join plus Switch when you want the combination visible as node configuration and the broker topics already carry the exact key names; Function with context when you want type validation, per-topic handling of long broker topic strings inside one node, and output only after both sensors have reported. Neither path emits until both values exist and are typed, and both fire on the same condition: the reading strictly above the threshold with the flag true.
Questions asked
Does Node-RED support a node with multiple inputs?
You can wire any number of input wires into one node, but the node processes one message per arrival and does not aggregate them by itself. Combining two streams is the job of a Join node in manual mode or a Function node that stores state in context and reads msg.topic to tell which sensor each message came from.
Why does my Join node emit nothing after I wire both sensors?
In manual mode with a fixed count of 2 messages, the node waits for two arrivals and counts arrivals, not distinct topics. Two temperature updates in a row satisfy the count, and the emitted object then lacks the smoke key, so the rule stays false. Send one payload from each sensor and inspect the combined object at a Debug node.
What happens when the temperature sits exactly at the threshold?
With the strict greater-than comparison the condition is false at exactly 70 and the flow stays silent, which matches a rule written as above 70. If your requirement includes the threshold value itself, use greater-than-or-equal in the expression instead; the two versions differ only at that one reading, so settle it before the flow goes live.
How do I handle sensors that publish at different rates?
The Function path handles this naturally: each arrival updates only its own context key, and every arrival re-evaluates the stored pair, so a fast temperature sensor cannot overwrite or delay the smoke flag. With the Join path, choose the setting that keeps the accumulated object and emits on each subsequent arrival, and confirm the behavior with an inject test on your release, because Join send behavior changed in Node-RED 4.0.

