Using Node-RED Subflows to Keep Repeating Logic Consistent
Learn how to create, configure, and reuse Node-RED subflows so that updates propagate automatically across all instances, with a worked example and notes on limitations.
03 Dec 2025, 07:30 UTC

The problem: duplicated logic drifts out of sync
When you build several Node-RED flows that need the same piece of processing – for example, adding a timestamp, validating a payload, or calling a shared API – you often copy‑paste the same nodes. Over time, a bug fix or improvement made in one place is forgotten in the others, leading to inconsistent behavior and harder maintenance.
Thesis: subflows give you a single source of truth
A Node-RED subflow is a reusable, exportable group of nodes that behaves like a custom node. You define it once, expose configurable inputs and outputs, and every instance you drop onto a canvas inherits the same internal logic. When you edit the subflow definition, all instances update automatically after a redeploy.
Worked example: creating a timestamp‑adding subflow
- Build the core flow
In the Node-RED editor, drag an
injectnode (set to repeat every 5 s), afunctionnode, and adebugnode onto the canvas. Connect them in sequence.// function node code return { ...msg, payload: new Date().toISOString() }; - Export to a subflow
Select the three nodes, right‑click, and choose Export to Subflow. Node-RED creates a subflow entry in the palette and replaces the selected nodes with a single subflow instance.
- Configure inputs and outputs
Double‑click the subflow instance to open Edit subflow properties. Add an input property named
in and an output property namedout. Map the subflow’s input wire to theinjectnode and thedebugnode’s wire to the output.Inside the subflow editor (click the subflow name to open it), you’ll see the original nodes. The function node now receives
msgfrom the subflow’s input and sends its result to the subflow’s output. - Use the subflow multiple times
Drag two more copies of the subflow from the palette onto a new tab. Connect each to its own
injectnode (different topics) and adebugnode. Deploy.When any inject fires, the corresponding debug shows a timestamped payload. All subflows share the same function logic.
- Verify automatic propagation
Open the subflow editor, change the function node to also add a
counterfield that increments each call (you’ll need a context variable). Deploy again. Every instance of the subflow now outputs the counter without you touching each flow individually.
Trade‑offs and limitations
- Debug visibility – Breakpoints or debug nodes inside a subflow do not appear in the main flow’s debug sidebar. You must open the subflow editor to see its internal debug output.
- Payload size on constrained devices** – Each subflow instance carries a copy of the subflow’s node definitions in the flow JSON. Very large subflows can increase deployment time and memory usage on devices like Raspberry Pi Zero.
- Versioning coupling** – Because all instances share the same definition, you cannot have two different versions of the same subflow running side‑by‑side without renaming or creating a new subflow.
To check whether a subflow is causing startup delays, open the Node-RED console (node-red -v) and look for the line "Flow file loaded" – compare timestamps before and after removing the subflow from a flow.
Actionable closing
If you find yourself copying the same set of nodes more than twice, turn them into a subflow. Start small (as in the example), expose only the inputs and outputs you truly need, and keep the internal node count modest to avoid bloat on edge hardware. Use the palette’s Export option to share the subflow as an node-red-contrib-* package when you want to distribute it across teams.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.