We talked about why you should get NodeRED in the first place, and how to get started with NodeRED, now it’s time for your first flow. The flow is not going to do anything amazing, other than illustrate the basics of how the nodes and flows work. You will be able to start your own automation setups. I’m using an example which won’t require any other devices or additional nodes to be installed.
NodeRED for beginners: 3. Your First Flow
It’s good to start somewhere and the input nodes are a good way to start your flow. The nodes will introduce some sort of data into the NodeRED server. You can use single or multiple nodes. In this example, I’m using 2 input nodes. These will enter a number (8 & 15) when a button is pressed, and this will represent a trigger.
When the button is pressed a message is created. This message often is referred to as the payload. I will cover the data processing in details in the next part of this series. Typically messages come with a payload and topic. You can assign these two in settings of the input node.
Both nodes are linked with a switch node which will evaluate the value and send it to correct output of the node. As you can see, it is possible to have multiple outputs from a single node depending on the outcome. You can do so by using “add” option at the bottom. I’m going to evaluate the numbers against the value 10 and send it one or the other direction.
The payload will reach one of the change nodes. These will, in this example, assign a new topic to the payload. You will see that you can simply overwrite the value stored as the topic. As we are only going to change the topic (msg.topic), the payload will be moved over further unchanged. It’s important to know when the payload is modified.
Both change nodes come together to function node. It’s one of the most powerful and complicated nodes to use. The node is blank by default and allows you to enter the JavaScript code. This node requires some programming experience, but for now, we will keep it very simple:
var value = msg.payload; msg.value = value; return msg;
Let’s quickly evaluate the script. I’m creating variable value, which will store the data from the payload. In this case, it’s the number. Then, I’m saving this value as a part of the payload which is not going to be modified by normal nodes.
Lastly, the payload and the topic will be used to set a string using a template message. Template node is easy to use, as all you have to do is enclose the payload and topic with {{…}} (mustache template mode) and entire message will be stored as a new payload.
Debugging
I have tested the flow and I know what will happen. To show you how the data is sent through the flows, I’m going to use debug nodes attached to each state of our transformations. The debug node can display the entire message (object) or a specific part of the message: msg.payload for payload and msg.topic for the topic.
We are going to monitor the topic and the payload. The last node will display the entire object message. The debug menu will let you know which message belongs to which node. Just highlight the node ID or give custom names to each node.
After deploying the flow, you can see when the inject node is activated, the number passes through each node, and the topic is changed accordingly. In addition, we have a payload as a result which is:
The new value is: high value and quals 15 !
or as an object:
{"_msgid":"18a7c16a.fd711f","topic":"high value","payload":"The new value is: high value and quals 15 !","value":"15"}
Conclusion
You should know the principles of working with NodeRED, In the next part, I’m going to focus specifically on the data structure, how information is processed, stored and recalled. That’s going to be a very important part to truly understand the NodeRED and underlying principles.