Home Home Automation NodeRED for beginners: 3. Your First Flow

NodeRED for beginners: 3. Your First Flow

Time to build the first flow!

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.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated in style with crypto-currency

Smart Ideas with

Automate your space in with these ecosystems and integrate it with other automation services

Learn NodeRED

NodeRED for beginners: 1. Why do you need a NodeRED server?

0
To server or not to server? That's a very silly question!

Best Automation Projects

ESP8266 WIFI lights under $5

0
Your Sonoff device can't do this!

Free wireless doorbell (Amazon Dash button hack)

0
The most terrifying situation I can imagine* is missing a parcel

New Amazon Dash Buttons

0
You can teach new buttons old tricks!

ESP8266 ceiling light – demo

0
Testing the ESP8266 as a WIFI light controller

Try Tasker and Raspberry Pi for your home automation needs!

0
Raspberry Pi can help you with your Tasker profiles

Smart Home

Best USB-C hack ever!

0
How to add USB-C charging to your old laptop without ruining it. USB-C hack for Lenovo Yoga

Sonoff Micro PLUS (USB-C hardware mod)

0
Sonoff Micro PLUS is a small modification that adds an USB-C port to Sonoff Micro - to make it even more useful!

Nora – Google Assistant in NodeRED

0
Integrate Google Assistant with NodeRED thanks to Nora - NodeRED home automation

“Inching” like a PRO (nodeRED edition)

0
Learn how to add inching to anything in NodeRED

Smart overnight charging with Tasker

0
Still keeping your phone plugged in overnight? Try smarter overnight charging with this profile