Node-RED: Lecture 3 – Example 3.3 Using a change node to change or manipulate a message payload

Another useful node is the change node, which will allow you to change a message payload or add new properties. You can use this node to affect the properties in a message, either by changing existing ones, deleting them or adding new properties.

In this example, you’ll continue with your MQTT theme and see how, now that you have successfully ‘switched’ the message flow based on the incoming MQTT message, you can add a new message property msg.payload.note.

If you aren’t following these examples sequentially, you may want to review Example 3.1 to see how to setup a free MQTT service and configure the incoming MQTT node used in these examples.

First, let’s drag and drop a change node and connect it to the second output of the switch node (Fig 3.7). As you will remember, this is the output that fires when msg.payload.analyze is set to false.

Figure 3.7 Adding in a change node to set a new message property.

Now configure it to set the property msg.payload.note to “this is not being analyzed” as shown in Fig 3.8.

Figure 3.8 Using a change node to add a new message property

When you receive a message that the switch node sends on the 2nd output, it will be modified to contain a “note” element with the string “this is not being analyzed”. If you deploy and test the flow by sending the MQTT message from HiveMQ, you’ll see the output as shown in Fig 3.9.

Figure 3.9 The result of switching and then changing the message

You can copy the flow for this example here:

Copy flow code
[{"id":"bf4087aae928924f","type":"tab","label":"NRguide-L3-Ex3-3","disabled":false,"info":"","env":[]},{"id":"17c79c88f2f10cc1","type":"mqtt in","z":"bf4087aae928924f","name":"","topic":"noderedlecture/sensor","qos":"0","datatype":"auto-detect","broker":"bbe24556bc1690f5","nl":false,"rap":false,"inputs":0,"x":320,"y":240,"wires":[["3b80ac17c5e8962a"]]},{"id":"fe940c14fa04844d","type":"switch","z":"bf4087aae928924f","name":"","property":"payload.analyze","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":220,"wires":[["8d6d4a51638abcc1"],["ac9d97948b331526"]]},{"id":"3b80ac17c5e8962a","type":"json","z":"bf4087aae928924f","name":"","property":"payload","action":"obj","pretty":false,"x":491.5993957519531,"y":162.09091186523438,"wires":[["fe940c14fa04844d"]]},{"id":"8d6d4a51638abcc1","type":"debug","z":"bf4087aae928924f","name":"isTrue","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":703.9999847412109,"y":166,"wires":[]},{"id":"139fd99b43835656","type":"debug","z":"bf4087aae928924f","name":"isFalse","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":280,"wires":[]},{"id":"ac9d97948b331526","type":"change","z":"bf4087aae928924f","name":"","rules":[{"t":"set","p":"payload.note","pt":"msg","to":"not being analysed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":300,"wires":[["139fd99b43835656"]]},{"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.