I am a beginner in Node-Red and I have created the following example. Where I want to replace the data's value from "numeric" into "payload".
The message from "Payload" is the following:
{
"deviceId" : "MyAzureWebApp";
"key" : "xxx";
"protocol" : "mqtt",
"data" : "{tem:25, wind:20}"
}
The message from "numeric" is the following:
{
"data":"{tem: 10, wind: 10}"
}
In the function block I add:
msg.payload.replace((msg.payload.data), (msg.numeric.data));
return msg;
Unfortunately, what I have done so far has not worked and I still do not understand how to solve this problem. I tried to use the change module but it only accepts one entry. So I would appreciate if someone can help me. :)
Messages travel through the flow independently of each other. This means when you inject the "Payload" message it will arrive at the function node, which will run and then forward it on to the debug node.
When you inject the "numeric" message it will do the same.
The function node (and all nodes) only works on one message at once, it holds no "state" about the previous message.
If you want to transform a message based on a previous message you need to learn to use the context objects to store information. You can use the context to store values and retrieve them later in the function node and the change node.
You will also need a way in the function node to distinguish where the message has come from. The usual way to do this is to use the msg.topic
value.
A very rough example to meet your needs would be:
if (msg.topic == 'numeric') {
context.set('foo', msg.payload);
return null;
} else if (msg.topic == 'payload') {
msg.payload = context.get('foo');
return msg;
}
This makes all kings of assumptions like that numeric
will always arrive before the payload
and the messages will always arrive in pairs. But it should give you something to start with.