Often you want your flow to start with known conditions.
For example you may have a flow that records various sensors, and have alarms set when the sensors exceed a given value.
As an example you may want to trigger an alarm when the temperature falls below 3 degrees or exceeds 30 degrees Centigrade.
So how do you set the max/min temperatures in the flow on start up?
There are several methods available:
- Hard code the values in the Flow.
- Read settings from a File.
- Use the config node
- Use environmental variables
Hard Code in the Flow
This is by far the easiest method and is very portable as when you export the flow the settings are included.
You can use a function node or a change node to do this.
The main drawback is that it isn’t very flexible as you need to edit the flow to change the settings.
In addition, it is not very good when you need to set lots of values.
Read In From a File
This is the method used most often on computer systems and everyone is familiar with configuration files in Windows and Linux.
These files are usually text files and can be in various formats.
Common formats are:
- Plain Text
Ref: The case for standard configuration file formats
Node-red has nodes that can read in the various formats (parser nodes), but the main factor when choosing is how and who will create the files.
I prefer CSV or JSON files.
CSV is easier when you have table like data.
As an example when you have say 100 sensors and you need to set the max/min of each sensor then we have a table like this.
CSV files are easy to create as you can use a simple text editor or a spreadsheet program like excel.
If you are creating JSON files then use an online tool like JSON Editor which is also available as a plugin for Google Chrome.
When using config files you will need to trigger reading at startup this is usually done with the inject node.
Be careful of setting the inject once after setting too low as it may not trigger correctly.
Using The Config Node
This is a node-red node that you can install and allows you to set global and flow variables at start.
It looks very similar to the change node as shown below.
The main advantage is that the data is stored in the flow so no external files are needed, and the main disadvantage is that you need to edit the flow to make changes.
The config node doesn’t require an external trigger to work, whereas all other methods do.
Using Environmental Variables
Node-red has access to the system environmental variables as well as flow and group environmental variables.
These can be used to hold initialisation data. See Using environmental variables for more details
When using config files you will need to trigger reading at start up. This is usually done with the inject node.
As mentioned previously beware of triggering too early as it may not work correctly.
Storing Flow Data
This is data that is created by the flow and needs to be stored so it is protected against restarts.
Again, as an example, we take the max/min temperature sensor values that have been configured on initial startup ,
What happens if we provide a way of changing these values in the flow.
These new values then need to be stored so that we don’t revert to the original or default values when we restart the flow.
By far the easiest way of doing this is to store the data as JSON data in a file.
Video– Initialising Node-Red Flows
Flows often require initialising with default values.
If the data is unlikely to ever be changed then it is easier to store it as part of the flow using a function,change, config node or flow or group environmental variables.
If you have lots of variables and they are likely to need changing from time to time then use a config file.
If the flow also needs to store updated variables then use a JSON data file.
Related Tutorials and Resources:
- Storing Data in Node-Red Variables
- Video Node-Red –Guide to Using The CSV Node
Thank you for the insights.
Have you ever seen such a related problem:
No I’ve never tried it but it is an interesting way of making large changes to a flow so I will give it a try