Hello!
Is it possible in Node Red to pass a function to a command that is then executed?
For example: msg.payload = msg.payload.1 + msg.payload.2
I hope someone can help me.
Thanks in advance.
Hello!
Is it possible in Node Red to pass a function to a command that is then executed?
For example: msg.payload = msg.payload.1 + msg.payload.2
I hope someone can help me.
Thanks in advance.
Well yes, of course it is possible, provided that a single msg.payload has properties 1 and 2.
But if you are talking about two discrete messages then you will need to look at a Join node to combine them into one.
Unless you show us an example of your message.payload it is impossible to give definite advice.
Hello!
Here please.
[
{
"id": "517ed4eac9dc7e2e",
"type": "inject",
"z": "e503d20f125dba49",
"name": "",
"props": [
{
"p": "topic",
"vt": "str"
},
{
"p": "payload.start",
"v": "msg.payload = msg.payload.1 + msg.payload.2",
"vt": "str"
},
{
"p": "payload.1",
"v": "10",
"vt": "num"
},
{
"p": "payload.2",
"v": "5",
"vt": "num"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 210,
"y": 280,
"wires": [
[
"0e4ccf8e5d36cde0"
]
]
},
{
"id": "6c2d9be5cd2ab4c7",
"type": "debug",
"z": "e503d20f125dba49",
"name": "debug 5",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 660,
"y": 280,
"wires": []
},
{
"id": "0e4ccf8e5d36cde0",
"type": "function",
"z": "e503d20f125dba49",
"name": "function 1",
"func": "msg.payload = msg.payload.start;\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 280,
"wires": [
[
"6c2d9be5cd2ab4c7"
]
]
}
]
Yes, I see it now.
msg.payload.start is a string. It contains "msg.payload = msg.payload.1 + msg.payload.2"
msg.payload.1 and msg.payload.2 are numbers. If your function node was
msg.payload= msg.payload[1] + msg.payload[2]
return msg
then the output would be 15.
You need to use square bracket notation because 1 and 2 are not alphabetical.
If you had used msg.payload.a and msg.payload.b then msg.payload.a + msg.payload.b would work too.
Hello!
I have uploaded the example.
I edited my post above to reply.
You might find the Template node slightly neater for setting msg properties than the inject node, like this
[{"id":"bd36e7c30c416c6d","type":"template","z":"11165ecbf36dadc8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n \"start\": \"This will give msg.payload three properties\",\n \"a\": 10,\n \"b\": 5\n}","output":"json","x":280,"y":420,"wires":[["01d173805bdf03f3"]]},{"id":"8c5398417bdd4b55","type":"inject","z":"11165ecbf36dadc8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":420,"wires":[["bd36e7c30c416c6d"]]},{"id":"48ed13bfdf237b56","type":"debug","z":"11165ecbf36dadc8","name":"debug 476","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":420,"wires":[]},{"id":"01d173805bdf03f3","type":"function","z":"11165ecbf36dadc8","name":"function 58","func":"msg.payload = msg.payload.a + msg.payload.b\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":420,"wires":[["48ed13bfdf237b56"]]}]
You can use eval() to do this, BUT please be careful and make sure the input into eval() is sanitised as eval() can be used to inject code that you may not want run on your system
example
[{"id":"517ed4eac9dc7e2e","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload.start","v":"msg.payload = msg.payload[\"1\"] + msg.payload[\"2\"]","vt":"str"},{"p":"payload[\"1\"]","v":"10","vt":"num"},{"p":"payload[\"2\"]","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":7680,"wires":[["0e4ccf8e5d36cde0"]]},{"id":"0e4ccf8e5d36cde0","type":"function","z":"d1395164b4eec73e","name":"function 1","func":"msg.payload = eval(msg.payload.start);\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":7680,"wires":[["6c2d9be5cd2ab4c7"]]},{"id":"6c2d9be5cd2ab4c7","type":"debug","z":"d1395164b4eec73e","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":7680,"wires":[]}]
p.s. When using numbers as property names it is better to use square bracket notation, as JS number are not really correct syntax
Hello!
Many thanks for the answer.
That's exactly what I was looking for.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.