Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Message Node-RED | Ahmad Fauzi Firmansyah

Set a message property to a fixed value


1. Problem

You want to set a message property to a fixed value.

2. Solution

Use the Change node to set the property of the message.

Example

Flows:

[{"id":"d72dc4ce.89b368","type":"inject","z":"535331d8.55c1f","name":"","topic":"","payload":"","p
ayloadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":80,"wires":[["78075f19.e0174"
]]},{"id":"78075f19.e0174","type":"change","z":"535331d8.55c1f","name":"","rules":[{"t":"set","p":"
payload","pt":"msg","to":"Hello
World!","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":80,"wires":[["
78dc7c25.b90d54"]]},{"id":"78dc7c25.b90d54","type":"debug","z":"535331d8.55c1f","name":"","acti
ve":true,"console":"false","complete":"false","x":550,"y":80,"wires":[]}]

3. Discussion

The Change node can be used to set properties of a message. The node supports setting various
JavaScript types as well as some Node-RED specific types.

• strings: "hello world"


• numbers: 42
• boolean: true/false
• timestamp: the current time, in milliseconds, since epoch (January 1st, 1970)
• JSON: a JSON string that will be parsed to its Object representation
• Buffer: a Node.js Buffer object

It also supports setting a property to a value based on the value of context properties, other message
properties or a JSONata expression..
Message Node-RED | Ahmad Fauzi Firmansyah

Delete a message property


1. Problem

You want to delete a message property.

2. Solution

Use the Change node to delete the property.

Example

Flows:

[{"id":"91cd2fa9.e0a96","type":"inject","z":"535331d8.55c1f","name":"","topic":"","payload":"","pay
loadType":"date","repeat":"","crontab":"","once":false,"x":140,"y":180,"wires":[["54ec03e4.5714bc"
]]},{"id":"54ec03e4.5714bc","type":"change","z":"535331d8.55c1f","name":"","rules":[{"t":"delete","
p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":180,"wir
es":[["321900de.3cbea"]]},{"id":"321900de.3cbea","type":"debug","z":"535331d8.55c1f","name":"",
"active":true,"console":"false","complete":"false","x":550,"y":180,"wires":[]}]

3. Discussion

The Change node can be used to delete properties of a message.


Message Node-RED | Ahmad Fauzi Firmansyah

Move a message property


1. Problem

You want to move a message property to a different property.

2. Solution

Use the Change node to move a property.

Example

Flows:

[{"id":"d11f7311.77c15","type":"inject","z":"535331d8.55c1f","name":"","topic":"Hello","payload":""
,"payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":280,"wires":[["13c01487.eb
13cb"]]},{"id":"13c01487.eb13cb","type":"change","z":"535331d8.55c1f","name":"","rules":[{"t":"mo
ve","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","r
eg":false,"x":360,"y":280,"wires":[["89cc4fb1.9b208"]]},{"id":"89cc4fb1.9b208","type":"debug","z":"
535331d8.55c1f","name":"","active":true,"console":"false","complete":"false","x":550,"y":280,"wires
":[]}]

3. Discussion

The Change node can be used to move a property of a message. It can be done as two separate
actions in the Change node; first using a Set action to copy the property to its new location and then
a Delete action to remove the original.

Alternatively, the node supports a Move action that does it in one step.
Message Node-RED | Ahmad Fauzi Firmansyah

Map a property between different numeric ranges


1. Problem

You want to scale a number from one numeric range to another. For example, a sensor reading in the
range 0 - 1023 should be mapped to a voltage range of 0 - 5.

2. Solution

Use the Range node to map between the defined ranges.

Example

Flows:

[{"id":"80dae67d.b4d8f8","type":"inject","z":"535331d8.55c1f","name":"","topic":"","payload":"0","
payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":380,"wires":[["81f13534.4563
48"]]},{"id":"81f13534.456348","type":"range","z":"535331d8.55c1f","minin":"0","maxin":"1023","m
inout":"0","maxout":"5","action":"clamp","round":false,"name":"","x":350,"y":420,"wires":[["e80b6
1d7.4b399"]]},{"id":"cb21de23.75a2f","type":"inject","z":"535331d8.55c1f","name":"","topic":"","pa
yload":"512","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":420,"wires":[["
81f13534.456348"]]},{"id":"342552de.255a1e","type":"inject","z":"535331d8.55c1f","name":"","topi
c":"","payload":"1023","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":460,"
wires":[["81f13534.456348"]]},{"id":"e80b61d7.4b399","type":"debug","z":"535331d8.55c1f","name
":"","active":true,"console":"false","complete":"false","x":550,"y":420,"wires":[]}]

3. Discussion

The Range node can be used to linearly scale between two different numeric ranges. By default, the
result is not constrained to the range defined in the node. This means using the voltage example
above, a value of 2046 would map to a result of 10.

The node can be configured to constrain the result to the target range, or apply simple modulo
arithmetic so the value wraps within the target range..

You might also like