Node-Red Initialising Flow Data on Startup

flow-initializationOften 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

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

node-red-parserThis 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
  • YAML
  • CSV
  • XML
  • .ini
  • JSON

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.

Sensor max min
sensor1 3 30
sensor2 4 35

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.

JSON-Editor

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 to low as it may not trigger correctly.

inject-on-start

Using The Config Node

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.config-node-set

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.

External Triggers

When using config files you will need to trigger reading at start up. This is usually done with the inject node

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.

Summary

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 or the config node.

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 needs to store variables then use a JSON data file.

Video– Initialising Node-Red Flows

Related Tutorials and Resources:

Please rate? And use Comments to let me know more
[Total: 0   Average: 0/5]

2 comments

Leave a Reply

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