Node-RED guide series

Node-RED, the "range" node

The "range" node takes in a number, which belongs to a particular scale range, and converts it to a new number that belongs to a different range.

The range node works like the map function in the Arduino programming language. It takes a number, which belongs to a particular range, and it will re-map it into a new number in a new range.

You will find the "range" node in the "function" group of the left toolbar.

The "range" node.

Setup the "range" node

To explain how to use the "range" node, I have create this simple flow:

This flow contains a "range" node.

I am using an "inject" node to send a number to the "range" node titled "scale input".

The "range" node takes the number from its input and re-maps it.

Below you can see how I have configured the "range" node.

The input range is 0 to 1023, and the output range is 0 to 100:

The edit properties of the "range" node.

Let's try out a couple of input values. There's already "1023" set in the "inject" node. Deploy the flow, and click on the "inject" node button.

Look at the output in the debug pane. It should look like this:

The arrow shows the scaled value of the input number. 1023 becomes 100.

The input "1023" belongs to the range 0 to 1023. Remapping this to the range of 0 to 100 yields "100".

Let's try another input number: "500".

Edit the "inject" node, redeploy and click on the button to start the flow.

The result is below:

500 out of 1023 is remapped to 48.87 out of 100.

The remapped value of 500 out of 1023 is 48.875855... out of 100. If you don't need all these decimals, you can round the result to the nearest integer by selecting the appropriate checkbox in the "range" node's properties:

Scale the mapped value to the nearest integer.

Re-deploy and click on the inject button. The result is easier to read:

The mapped number is rounded to the nearest integer.

The "range" node is very useful, especially when you work with numbers exist within a specific range. For example, I'll be using this node in the terrarium project to scale the analog input that comes from the ESP32 for the humidity of the soil into a number from zero to 100 to represent humidity as a percentage.

Learn Node-RED and how to use it with the ESP32

This course will guide you through the construction of an automated control system. The deliverable is a Terrarium controller.

Along the way, you will learn a great deal about useful technologies such as the Node Red programming environment and MQTT.

Browse this article

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

Image
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}