Creating your first flow : Node-RED (2024)

Overview

This tutorial introduces the Node-RED editor and creates a flow that demonstratesthe Inject, Debug and Function nodes.

1. Access the editor

With Node-RED running, open the editor in a web browser.

If you are using a browser on the same computer that is running Node-RED, you canaccess it with the url: http://localhost:1880.

If you are using a browser on another computer, you will need to use the ip addressof the computer running Node-RED: http://<ip-address>:1880.

2. Add an Inject node

The Inject node allows you to inject messages into a flow, either by clickingthe button on the node, or setting a time interval between injects.

Drag one onto the workspace from thepalette.

Select the newly added Inject node to see information about its properties and adescription of what it does in the Information sidebar pane.

3. Add a Debug node

The Debug node causes any message to be displayed in theDebug sidebar. Bydefault, it just displays the payload of the message, but it is possible todisplay the entire message object.

4. Wire the two together

Connect the Inject and Debug nodes together by dragging betweenthe output port of one to the input port of the other.

5. Deploy

At this point, the nodes only exist in the editor and must be deployed to theserver.

Click the Deploy button.

6. Inject

With the Debug sidebar tab selected, click the Inject button (the small square button next to your inject node). You should seenumbers appear in the sidebar. By default, the Inject node uses the number ofmilliseconds since January 1st, 1970 as its payload.

7. Add a Function node

The Function node allows you to pass each message though a JavaScript function.

Delete the existing wire (select it and press delete on the keyboard).

Wire a Function node in between the Inject and Debug nodes.

Double-click on the Function node to bring up the edit dialog. Copy the followingcode into the function field:

// Create a Date object from the payloadvar date = new Date(msg.payload);// Change the payload to be a formatted Date stringmsg.payload = date.toString();// Return the message so it can be sent onreturn msg;

Click Done to close the edit dialog and then click the deploy button.

Now when you click the Inject button, the messages in the sidebar will now beformatted is readable timestamps.

Summary

This flow demonstrates the basic concept of creating a flow. It shows how theInject node can be used to manually trigger a flow, and how the Debug node displaysmessages in the sidebar. It also shows how the Function node can be used towrite custom JavaScript to run against messages.

Source

The flow created in this tutorial is represented by the following json. To importit into the editor, copy it to your clipboard and then paste it into the Import dialog.

[{"id":"58ffae9d.a7005","type":"debug","name":"","active":true,"complete":false,"x":640,"y":200,"wires":[]},{"id":"17626462.e89d9c","type":"inject","name":"","topic":"","payload":"","repeat":"","once":false,"x":240,"y":200,"wires":[["2921667d.d6de9a"]]},{"id":"2921667d.d6de9a","type":"function","name":"Format timestamp","func":"// Create a Date object from the payload\nvar date = new Date(msg.payload);\n// Change the payload to be a formatted Date string\nmsg.payload = date.toString();\n// Return the message so it can be sent on\nreturn msg;","outputs":1,"x":440,"y":200,"wires":[["58ffae9d.a7005"]]}]

Next Steps

Related reading

Creating your first flow : Node-RED (2024)
Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5577

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.