Node-RED: Lecture 3 – Example 3.5 Scaling input with the range node

When dealing with real world input from sensors and other devices, an ability to scale input data is often required. Node-RED provides the scale node to support this and allows you to scale (linearly) an input value.

This example continues our theme of manipulating MQTT input data. If you aren’t familiar with how to setup an MQTT node then refer to Example 3.1. Examples 3.2-4 explain the rest of the nodes in this flow.

Let’s assume you want to scale your value (originally in the range 0-10) to a range (0-255) when you aren’t doing any analysis. This means we are dealing with the lower part of the flow which is fired when the switch node evaluates the Analyze property as false.

To do this, select the change node you configured above (set msg.payload) and copy it with ctrl+c, then ctrl+v to create a third change node. Wire it to a range node as shown in Fig 3.14.

Figure 3.14 Scaling input values using the range node.

Double-click on it, and configure it to map the input from 0-10 to 0-255 as shown in Fig 3.15.

The scale node has three options set by the action field. The default will scale according to the mapping given but will happily scale values outside the given ranges, using the same mapping. Scale and limit to target range means that the result will never be outside the range specified within the result range. A third option, Scale and wrap within the target range means that the result will essentially be a “modulo-style” wrap-around within the result range.

Then return to the HiveMQ test page and post {“analyze”:false, “value”:10} as a new MQTT message to the same topic.

Figure 3.15 Setting the scaling ranges for input and output of the scale node

If you return to your Node-RED window, you will see that the debug node associated with the lower part of the flow has fired, showing that the msg.payload.value property that you set as 10 when you published it to MQTT, has been scaled up 255 as shown in Fig 3.16.

Figure 3.16 Final scaled output when analysis is turned off

You can copy the full flow below:

Open to view/copy flow
[{"id":"8be911e7df739406","type":"tab","label":"NRguide-L3-Ex3-4","disabled":false,"info":"","env":[]},{"id":"cd000bd7f7ffdb6e","type":"mqtt in","z":"8be911e7df739406","name":"","topic":"noderedlecture/sensor","qos":"0","datatype":"auto-detect","broker":"bbe24556bc1690f5","nl":false,"rap":false,"inputs":0,"x":260,"y":280,"wires":[["5e232344d2858b53"]]},{"id":"defddad0425099c9","type":"switch","z":"8be911e7df739406","name":"","property":"payload.analyze","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":200,"wires":[["2a6370c02862508b"],["8bc6f115a5d8778e","7b41ae36769f414c"]]},{"id":"5e232344d2858b53","type":"json","z":"8be911e7df739406","name":"","property":"payload","action":"obj","pretty":false,"x":430,"y":200,"wires":[["defddad0425099c9"]]},{"id":"59e1beb05e594212","type":"debug","z":"8be911e7df739406","name":"isTrue","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":160,"wires":[]},{"id":"6505cc0e9d029116","type":"debug","z":"8be911e7df739406","name":"isFalse","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":320,"wires":[]},{"id":"8bc6f115a5d8778e","type":"change","z":"8be911e7df739406","name":"","rules":[{"t":"set","p":"payload.note","pt":"msg","to":"not being analysed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":220,"wires":[["057f17c55047df76"]]},{"id":"2a6370c02862508b","type":"change","z":"8be911e7df739406","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":140,"wires":[["429772e5d694d6ea"]]},{"id":"7f4435dbec67d26e","type":"comment","z":"8be911e7df739406","name":"Analyze = true","info":"","x":630,"y":100,"wires":[]},{"id":"429772e5d694d6ea","type":"rbe","z":"8be911e7df739406","name":"if value change > 20%","func":"deadband","gap":"20%","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":920,"y":80,"wires":[["59e1beb05e594212"]]},{"id":"dfe389adec33c07b","type":"comment","z":"8be911e7df739406","name":"Analyze = false","info":"","x":640,"y":300,"wires":[]},{"id":"0c9fead001a7250b","type":"range","z":"8be911e7df739406","minin":"0","maxin":"10","minout":"0","maxout":"255","action":"scale","round":false,"property":"payload","name":"","x":920,"y":280,"wires":[["6505cc0e9d029116"]]},{"id":"7b41ae36769f414c","type":"change","z":"8be911e7df739406","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":260,"wires":[["0c9fead001a7250b"]]},{"id":"057f17c55047df76","type":"debug","z":"8be911e7df739406","name":"isFalse","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":240,"wires":[]},{"id":"bbe24556bc1690f5","type":"mqtt-broker","name":"test mqtt","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"noderedlecture/sensor","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

Back to Lecture 3

                                   PREVIOUS example                        NEXT example


© Lea, Blackstock, Calderon

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Author: Rodger Lea

Currently CEO of Internet of Things startup, Sense Tecnic, Dr. Lea has over 25 years experience spanning academic, large corporations and startups. For the last 10 years, he has started or helped start 4 new companies while managing an active research program (University of British Columbia, Canada and Lancaster University, UK) into distributed and ubiquitous computing, the IoT and Smart Cities.