A subflow is a collection of nodes that appear as a single node in the workspace.
They also appear in the node palette and can be added to a flow like any other node.
They are effectively reusable groups of nodes much like sub routines in traditionally programs.
Any changes to the subflow are effective immediately in all flows using that subflow
Creating a Subflow
You can create a subflow by selecting the Subflow -> Create subflow option in the menu.
This creates a blank subflow and opens it in the workspace for editing.
You can also create a subflow by selecting a group of nodes and then selecting the Subflow -> Create subflow from selection option in the menu.
Editing The Subflow
The name of the subflow will default to subflow and you should edit this name to something meaningful.
Below is a screen shot of a subflow that simulates and sensor and I’ve called it sensor.
You can also edit the subflow properties,change the number of inputs and outputs and enable status. There is also a tab to delete the subflow.
If you click on the edit properties tab you can
- add environmental variables
- add module properties
- add a description
- change the colour,category and icon for the node.
The edit Properties tab is used to define a set of properties for the subflow.These properties can be customised for each instance of the subflow.
The screen shot below shows my sensor subflow with it’s properties.
The subflow properties are available as environmental variables and can be accessed in a function node using the syntax:
and in other nodes like the change node by selecting the environmental variables option
These environmental variables can only be used within the subflow.
Context,Flow and Global Variables in Subflows
Context variables are not available in subflows but flow variables can be accessed using the $parent syntax.
So if for example we have a flow variable called counter we can access it in a subflow function node using the following:
Global variables are available using the standard syntax
The module properties tab lets you add additioninformationa about the subflow and you can also turn a subflow into a node-red module the instructions are here.
This is shown below:
By default subflows appear in the node menu under the subflow category but you can create new categories as well as place it in an existing category.
Subflows and Flow Files
When you import or create a sublfow in a workspace then it is stored in the JSON file associated with that workspace.
So if a start node red using the command
then import a sub flow into the workspace I can use it in the house.json workspace.
If I now start node-red using
then the subflow will not be available, and I would need to import it.
This is the official video by the node-red team
This is my video which I did before I discovered the official video
In a way they are similar to subflows but they are more of a way of combining nodes so that they can be moved around and copied in chunks.
They do not appear in the node palette like subflows do and do not have their own environmental variables.
Nodes can be added to existing groups,removed from existing groups and groups can also be nested.
Combining nodes into groups can make flows easier to navigate. This videos how you how to create and edit them.
Subflows are a very powerful and useful feature as they allow you to package frequently used functionality into a reusable node that appears on the node palette menu.
Groups are an organisational feature that allows you to combine nodes so that you can move and copy them as a single unit.
Related Tutorials and resources:
- Using Environmental Variables
- Using The Node-Red Library
- Using the Node Red Status Node
- Using the Node-Red Function Node- Beginners Guide
- subflow online docs