NodeRED for beginners: 7. Tips & Tricks

Tips and Tricks to make your flows... flow better!

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.

Copy Nodes
[{"id":"d86084df.159b78","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"b672681a.7056b8","type":"inject","z":"d86084df.159b78","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":135,"wires":[["a525708e.8efe8"]]},{"id":"d4d819bd.156ff8","type":"inject","z":"d86084df.159b78","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":180,"wires":[["a525708e.8efe8"]]},{"id":"a525708e.8efe8","type":"change","z":"d86084df.159b78","name":"","rules":[{"t":"set","p":"controller","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":135,"wires":[[]]},{"id":"d3d3489c.863b58","type":"inject","z":"d86084df.159b78","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":240,"wires":[["8e00b4d0.f10a88"]]},{"id":"85e4ff93.c0c9d","type":"debug","z":"d86084df.159b78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":540,"y":240,"wires":[]},{"id":"8e00b4d0.f10a88","type":"switch","z":"d86084df.159b78","name":"","property":"controller","propertyType":"global","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":240,"wires":[["85e4ff93.c0c9d"]]}]

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.

Copy Nodes
[{"id":"66dc2714.310a18","type":"ui_text","z":"fd7807e.3ae83f8","group":"205fd73e.b5c718","order":2,"width":"2","height":"1","name":"Desktop Staus","label":"","format":"<font color= {{msg.color}} > {{msg.payload}} </font>","layout":"row-spread","x":1175,"y":90,"wires":[]},{"id":"543091d5.79da6","type":"function","z":"fd7807e.3ae83f8","name":"Color Mapping","func":"switch (msg.payload) {\n\ncase true : \n    msg.color = \"Green\";\n    msg.payload = \"online\";\n    break;\n    \ncase false : \n    msg.color = \"Red\";\n    msg.payload = \"offline\";\n    break;\n    \ndefault : \n    msg.color = \"Gray\";\n    msg.payload = \"not set\";\n    break;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":965,"y":90,"wires":[["66dc2714.310a18"]]},{"id":"205fd73e.b5c718","type":"ui_group","z":"","name":"Computers","tab":"95ed8000.75e99","disp":true,"width":"6","collapse":false},{"id":"95ed8000.75e99","type":"ui_tab","z":"","name":"DIY Smart Socket","icon":"dashboard","order":2}]

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.

Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE