Hi, I'm looking to use arithmetic in payloads. For example, take a payload of 5 and put out 10.
I have been messing with a change node but cannot seem to find the correct expression.
Hi, I'm looking to use arithmetic in payloads. For example, take a payload of 5 and put out 10.
I have been messing with a change node but cannot seem to find the correct expression.
Why not use a function node to access the payload and modify it?
The standard nodes Change(Jsonata) and function(Javascript) can do this. There are also contributor nodes you can install.
Here is a change node example using JSONAta
[{"id":"c828b9168a3f213f","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":250,"y":3100,"wires":[["1fa7f070dcc73066"]]},{"id":"1fa7f070dcc73066","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number($$.payload) + 5","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":3080,"wires":[["a81d87f562b74deb"]]},{"id":"a81d87f562b74deb","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 225","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3080,"wires":[]}]
The Jsonata expression.
$number($$.payload) + 5
Sure google jsonata docs
Personally I suggest learning a bit of JavaScript and doing such things in a function node before trying to master jsonata. Unless you find that your brain copes naturally with jsonata that is.
In a function node it would be
msg.payload = msg.payload + 5
return msg
or if msg.payload is not already a number (but is a string):
msg.payload = Number(msg.payload) + 5
return msg
Thanks, I think i will go with
msg.payload = Math.round(Number(msg.payload)) + 5
return msg
Appreciate all the help
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.