That’s the last post of this series with a couple of tips and tricks you might find handy while building your own flows. These will be totally random, this is why I left it to the end. Without further ado let’s jump to the tips
Tips & Tricks
Node Search
With time, your collection of nodes will grow and it’s going to be harder and harder to find all the nodes quickly. There are shortcuts you can use, especially one that no beginners tutorials mention it.
Ctrl+Left Mouse Click
This key shortcut will open an instant search window where nodes can be searched for and added to the flow. By the way, if you end up with the connecting line stuck to your mouse cursor, press Ctrl again to cancel the line.
Enable/Disable Flow
There might be a time when you need to enable or disable a particular flow for a reason. There is a very clever way of toggling this using only a couple of nodes. That toggle consists of two elements:
- controller – it’s a node embedded into an existing flow that lets the messages go through based on a variable’s value
- controlling flow – it’s a separate flow that sets the value, which is used to enable or disable the flow of the messages
Controlling flow
The controlling flow can be set in any way that suits you. The idea is to set a property: global.controller with a set node to true|false. You can use MQTT, HTTP, Timers or simple inject nodes to achieve this.
Once set, the property global.controller will be monitored by the controller node.
Controller node
This node should be inserted in such way, so it interrupts the stream of messages. It’s good to do this at the beginning of the flow to spare yourself processing an information that you will never send forward.
Based on the value of global.controller the change node lets the message true if the property of that variable is set to true.
NodeRED Dashboard – set text colours dynamically
NodeRED Dashboard is great if you want to visualise your content quickly. One of the nodes available is a text node. Apart from setting the text node dynamically, you can change the colour of the text based on the property of the message sent.
You could use a Function node to select the values that Text Node should have:
switch (msg.payload) { case true : msg.color = "Green"; msg.payload = "online"; break; case false : msg.color = "Red"; msg.payload = "offline"; break; default : msg.color = "Gray"; msg.payload = "not set"; break; } return msg;
Formatted this way values can be passed over to the text node which has the Value Format set in the following way:
<font color= {{msg.color}} > {{msg.payload}} </font>
Doing so will change not only the text but also the colour of the text in your dashboard.
Comments
Never underestimate the power of comments. Things that are obvious to you while working on a project, can become the puzzles again if you have to fix or improve a project year down the line.
Making appropriate comments in the Flow and using Comments Nodes to leave snippets of information, makes the troubleshooting and sharing your creations much easier. Double click on the Flow tab to open the Flow menu. There is plenty of space to explain how your flow should work, and what key features it contains.
Working with flows imported from the internet is so much easier once it consists the information about the structure.
Building your own library
Flow library
Once you get better with NodeRED, you will start copying previously solved problems into your new flows. While the copy and paste technique works, it might be easier for you to simply save the interesting flow into a library.
This way you can bring it back to any Flow space in no time! Select the nodes you want to save, then go to the export menu and pick library option to save it for a later use.
Function Library
The same can be done for all the snippets of code. The Function node allows you to export your snippets and save it in the library. If you forgot how the code should look like for composing an HTTP POST, or case snippet for the text colour, you can simply save the code for later.
End of the NodeRED Tutorial
Feel free to check out my other NodeRED resources, there is a designated menu just for the NodeRED projects! I hope this tutorial helped you, and you are ready to build awesome home automation setups.