Node-RED Function Node Tutorial

Exploring the Power of Node-RED Function Node: A Practical Guide

Introduction

Node-RED, a popular flow-based programming tool, offers a wide range of nodes to facilitate seamless automation and integration. Among these nodes, the Function Node stands out as one of the most powerful ones, as it allows users to write their own JavaScript code. With the ability to manipulate data and perform calculations, the Function Node opens up endless possibilities for customization and automation within Node-RED.

In this article, we will explore the capabilities of the Function Node by walking through a practical example. We will create a flow that sends a daily weather forecast notification to a mobile phone, converting the temperature from Celsius to Fahrenheit using the Function Node. So let’s dive in!

How to Use the Function Node – Weather Example

To begin, let’s set up our flow step-by-step. First, we need an Inject Node, which will trigger the flow at a specific time. We can find the Inject Node in the Node-RED palette by searching for it and then dragging it onto the canvas.

Next, we require a Current State Node to fetch the weather information from a Home Assistant Entity. We can locate the Current State Node in the palette and add it to the canvas.

Now comes the star of the show, the Function Node. This node will allow us to perform JavaScript calculations and data manipulation. Let’s add it to the canvas.

Finally, we need a Service Node to send the weather notification to our mobile phone. Let’s search for the appropriate Service Node and add it to the flow.

Configuring the Nodes

Now that we have all the necessary nodes in place, let’s configure them to build our desired functionality.

  1. Inject Node: Double-click on the Inject Node, and in the configuration panel, set the schedule to trigger the flow every day at 8 AM.
  2. Current State Node: Configure the Current State Node to fetch the weather entity from Home Assistant. If you already have this entity set up, you can proceed. Otherwise, ensure you have the required entity configured in Home Assistant before proceeding.
  3. Function Node: Before diving into the JavaScript code, let’s examine the data returned by the weather entity. To do this, we’ll connect the Inject Node to the Current State Node and add a Debug Node to inspect the message contents. Double-click on the Debug Node, select the “Complete Message” option, and deploy the flow. By triggering the Inject Node, we can examine the debug output and identify the path to the temperature data. Copy the path, as it will be useful in the upcoming steps.
  4. Function Node (JavaScript): Double-click on the Function Node and replace the default code with the following:
var temperature = msg.data.attributes.temperature;
var tempFahrenheit = temperature * 1.8 + 32;
var message = "It's " + Math.round(tempFahrenheit) + " degrees fahrenheit outside.";
msg.message = message;
return msg;

The above code retrieves the temperature from the message, converts it to Fahrenheit, and constructs a new message with the converted temperature.

  1. Linking Nodes: Connect the Function Node to the Service Node, completing the flow. Deploy the changes.

Testing and Refining the Flow

Now it’s time to test our flow and see the results. Trigger the Inject Node by clicking on it, and observe the debug output. You should see a new message containing the converted temperature in Fahrenheit.

To enhance the readability of the temperature, you can modify the Function Node code to round the value to the nearest whole number. Replace the existing line temperature = temperature * 1.8 + 32; with temperature = Math.round(temperature * 1.8 + 32);. Deploy the changes and trigger the flow again to see the rounded Fahrenheit temperature in the debug output.

Sending Notifications to a Mobile Phone

To complete our weather notification flow, we need to configure the Service Node to send the message as a notification to our mobile phone.

  1. Service Node Configuration: Double-click on the Service Node and select the appropriate domain (e.g., Notify). Choose the service related to your phone (e.g., Home Assistant Mobile App) from the available options.
  2. Data Formatting: Within the message configuration, specify the required properties for the notification. In the message field, enter message.data.message, and in the title field, enter message.data.title. You can use the provided example or copy the values from the debug output.
  3. Function Node (JavaScript): Modify the Function Node code to accommodate the changes in the message structure. Replace the existing code with the following:
var temperature = msg.data.attributes.temperature;
var tempFahrenheit = temperature * 1.8 + 32;
var message = "It's " + Math.round(tempFahrenheit) + " degrees fahrenheit outside."
msg.message = message;
msg.payload = {};
msg.payload['data'] = {};
msg.payload.data.message = message;
msg.payload.data.title = "Outside Temperature";
return msg;

Test and Verify: Trigger the flow once again and verify that the notification is successfully sent to your mobile phone. By inspecting the debug output, you should see the formatted message being passed to the Service Node.

Utilizing Data from Multiple Entities

In the previous example, we focused on extracting data from a single entity. However, the Function Node allows us to work with data from multiple entities in a flow. Let’s briefly explore how to achieve this.

  1. Current State Node (Additional): Add another Current State Node to the flow, this time targeting a different entity, such as the sun entity. Configure the node accordingly, specifying the appropriate message path for the new data, e.g. sun.payload and sun/data.
  2. Function Node (JavaScript): Update the Function Node code to access the additional data. Modify the existing code to use message.sun.payload instead of message.payload when referencing the sun entity data.

By organizing the data within the message using distinct paths (e.g., message.weather.payload and message.sun.payload), you can work with multiple entities simultaneously within the Function Node.

Expanding Your Automation Capabilities

The examples covered here only scratch the surface of what you can achieve with the Function Node. By leveraging JavaScript, you can perform complex calculations, create arrays of messages, and customize your automation flows further. If you’re interested in learning more about these advanced topics, let us know in the comments, and we’ll consider covering them in future articles.

Conclusion

The Function Node in Node-RED provides immense flexibility and power for customizing automation flows. By harnessing JavaScript, you can manipulate data, perform calculations, and create personalized notifications. In this article, we explored a practical weather example on how to convert temperature units and send weather notifications. We also touched on utilizing data from multiple entities within the Function Node. Armed with this knowledge, you can take your Node-RED automation to new levels.

We hope you found this guide helpful. Stay tuned for more articles on Node-RED and its capabilities. Happy automating!

Scroll to Top