Using Subflows and Groups in Node-Red

subflowsA 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.

create-subflow

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.

subflows

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.

subflow-edit

If you click on the edit properties tab you can

subflow-properties

  • add environmental variables
  • add module properties
  • add a description
  • change the colour,category and icon for the node.

Properties Tab

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.

subflow-edit-properties

The subflow properties are available as environmental variables and can be accessed in a function node using the syntax:

let sensor_name=env.get("sensor_name");

 

and in other nodes like the change node by selecting the environmental variables option

subflow-properties-change-node

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:

let counter=flow.get("$parent.counter");

Global variables are available using the standard syntax

 Module Tab

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.

Appearance Tab

This is shown below:

subflow-appearance
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

node-red house.json

then import a sub flow into the workspace I can use it in the house.json workspace.

If I now start node-red using

node-red videos.json

then the subflow will not be available, and I would need to import it.

Subflow videos

This is the official video by the node-red team

This is my video which I did before I discovered the official video

Status node in Subflows

You can display status information from subflows but it isn’t really obvious how this works so I created this video to illustrate it.

The status node was added to subflows in version 0.2 here is the link to the release notes.

Groups

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.

Summary

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:

 

Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *