The SET rule from the Change node allows you to create a new property (key, value) or set (change) the value of an existing property.
The value to be set can be taken from a variety of different sources: a value from another object, a hard-coded string, number, buffer or boolean, a JSON object, a timestamp or from the result of a JSONata expression.
This makes the Change rule to be very useful.
Be careful: important
There will be always two fields in the configuration dialog of the Set rule.
The first one, side by side with the rule name Set is the recipient of the action.
The second field is from where Node-RED will take the new value. I rather prefer to think of the name of this field as "Taken from " instead of "to". Some old school guys may take a wrong guess and think this dialog page is something like "Copy first field to the second field". This would be a mistake. It should be understood as "Store in the path given in the first field the value taken from the second field".
Example #1
Change the value of an existing object, in this case: msg.payload.ms1.father
After the rule is applied:
Example #2
Create a new property: msg.payload.msg1.uncle and set its value to "Uri"
Node-RED msg object after the rule is applied:
Example #3
Just to reinforce how to understand the dialog page from the rule Set: Store in the first field the value taken from the second field.
Before the rule is applied:
After the rule is applied:
Example #4
It is possible to use the rule to store properties in the flow context (or global context).
We expect that below rule stores in the property flow.payload the value taken from the second field.
It probably happened but it is not possible to check the end result with a debug node (it will not show the flow object). We could add an auxiliary function node just to display (using the statement node.warn) the content of the object flow. Instead, I will add a second rule to create a new property in the object msg (easy to check the full content of the msg object with the debug node) with the value taken from the flow object.
The second rule will store in msg.payload.msg5 the property taken from flow.payload
Resulting:
Example #5
The change node can be used to inject in the flow a large JSON object.
Let´s say you need to inject the following data into your flow:
[
{
"_id": "58e39679e4b024931d65c440",
"ap": "80:2a:a8:c0:b7:d3",
"channel": "44",
"datetime": "2017-04-04T12:49:47Z",
"hostname": "android-2c54f069416770eb",
"key": "EVT_WU_Connected",
"msg": "User[e8:50:8b:a5:83:39] has connected to AP[80:2a:a8:c0:b7:d3] with ssid \"ssid\" on \"channel 44(na)\"",
"radio": "na",
"site_id": "58a18811e4b0f7d0d14435bc",
"ssid": "ssid",
"subsystem": "wlan",
"time": 1491310187878,
"user": "e8:00:00:00:00:01"
},
{
"_id": "58e39679e4b024931d65c440",
"ap": "80:2a:a8:c0:b7:d3",
"channel": "44",
"datetime": "2017-04-04T12:49:47Z",
"hostname": "android-2c54f069416770eb",
"key": "EVT_WU_Disconnected",
"msg": "User[e8:50:8b:a5:83:39] has connected to AP[80:2a:a8:c0:b7:d3] with ssid \"ssid\" on \"channel 44(na)\"",
"radio": "na",
"site_id": "58a18811e4b0f7d0d14435bc",
"ssid": "ssid",
"subsystem": "wlan",
"time": 1491310187878,
"user": "e8:00:00:00:00:02"
},
{
"_id": "58e39679e4b024931d65c440",
"ap": "80:2a:a8:c0:b7:d3",
"channel": "44",
"datetime": "2017-04-04T12:49:47Z",
"hostname": "android-2c54f069416770eb",
"key": "EVT_WU_Disconnected",
"msg": "User[e8:50:8b:a5:83:39] has connected to AP[80:2a:a8:c0:b7:d3] with ssid \"ssid\" on \"channel 44(na)\"",
"radio": "na",
"site_id": "58a18811e4b0f7d0d14435bc",
"ssid": "ssid",
"subsystem": "wlan",
"time": 1491310187878,
"user": "e8:00:00:00:00:03"
},
{
"_id": "58e39679e4b024931d65c440",
"ap": "80:2a:a8:c0:b7:d3",
"channel": "44",
"datetime": "2017-04-04T12:49:47Z",
"hostname": "android-2c54f069416770eb",
"key": "EVT_WU_Connected",
"msg": "User[e8:50:8b:a5:83:39] has connected to AP[80:2a:a8:c0:b7:d3] with ssid \"ssid\" on \"channel 44(na)\"",
"radio": "na",
"site_id": "58a18811e4b0f7d0d14435bc",
"ssid": "ssid",
"subsystem": "wlan",
"time": 1491310187878,
"user": "e8:00:00:00:00:04"
},
{
"_id": "58e39679e4b024931d65c440",
"ap": "80:2a:a8:c0:b7:d3",
"channel": "44",
"datetime": "2017-04-04T12:49:47Z",
"hostname": "android-2c54f069416770eb",
"key": "EVT_WU_Connected",
"msg": "User[e8:50:8b:a5:83:39] has connected to AP[80:2a:a8:c0:b7:d3] with ssid \"ssid\" on \"channel 44(na)\"",
"radio": "na",
"site_id": "58a18811e4b0f7d0d14435bc",
"ssid": "ssid",
"subsystem": "wlan",
"time": 1491310187878,
"user": "e8:00:00:00:00:05"
}
]
You can do it by using the SET option from the change node.