Change payload form

Hello,

I am trying to replace tags from s7 node with tags from modbus. In s7 tags are formed like this "{"I1":22.463211059570312,"I2":11.89554214477539,"I3":22.009639739990234,"L1":228.79904174804688,"L2":229.78207397460938,"L3":228.8221435546875,"Frequency":50.022239685058594,"I_avarage":18.789464950561523,"F1":0.969420850276947,"F2":0.8568284511566162,"F3":0.9685365557670593}"

and out of modbus I get all this
"{"payload":21.9,"topic":"currentL3","format":"float32","unit":"A","model":"pac3220","_msgid":"3385c039ec7c7315","messageId":"64186ee1e446920650695b20","modbusRequest":{"unitid":1,"fc":3,"address":17,"quantity":2,"emptyMsgOnFail":false,"keepMsgProperties":true,"messageId":"64186ee1e446920650695b20"},"values":[16815,11993]}"

So how can I transform payload from modbus "getter" to get just for example "{"currentL3" = 21.9}"

br

Aleksander

Use a Change node with an expression

image

[{"id":"01be8de27b6ec3b1","type":"inject","z":"54efb553244c241f","name":"modbus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"currentL3","payload":"21.9","payloadType":"num","x":310,"y":2520,"wires":[["9bc3391967f54dbe"]]},{"id":"425e438b793a5470","type":"debug","z":"54efb553244c241f","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":2520,"wires":[]},{"id":"9bc3391967f54dbe","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ topic: payload }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2520,"wires":[["425e438b793a5470"]]}]

Or without using any code.

[{"id":"01be8de27b6ec3b1","type":"inject","z":"65617ffeb779f51c","name":"modbus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"currentL3","payload":"21.9","payloadType":"num","x":920.0000228881836,"y":4122.000285148621,"wires":[["9bc3391967f54dbe"]]},{"id":"9bc3391967f54dbe","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload[msg.topic]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140.0000228881836,"y":4122.000285148621,"wires":[["425e438b793a5470"]]},{"id":"425e438b793a5470","type":"debug","z":"65617ffeb779f51c","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1370.0000228881836,"y":4122.000285148621,"wires":[]}]

... first thanks for reply. But I would like to erase all unnecessary infos from object. To get like on example (I:61.422...)
image

In that case you can use a Function node to create a new clean Object and use only the fields you need to be passed on.

let newMsg = { payload: { [msg.topic]: msg.payload } }
return newMsg;

Example

[{"id":"01be8de27b6ec3b1","type":"inject","z":"54efb553244c241f","name":"modbus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"currentL3","payload":"21.9","payloadType":"num","x":230,"y":2180,"wires":[["e093823c709e94ac"]]},{"id":"425e438b793a5470","type":"debug","z":"54efb553244c241f","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":2180,"wires":[]},{"id":"e093823c709e94ac","type":"function","z":"54efb553244c241f","name":"function","func":"let newMsg = { payload: { [msg.topic]: msg.payload } }\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":2180,"wires":[["425e438b793a5470"]]}]

ps. the reason you are seeing the full object in the top msg is probably because you have the Debug node set to show "complete msg object". you could set it to show only msg.payload and ignore the rest

Why do you want to do that?

... because I have all logic behind adopt to that type of data and now I would like to change acquisition type from S7 node to modbus node without changing anything else because system is tested and already in use.

When you said

What object did you mean? msg.payload or the complete message.

Additional properties in the message will not usually affect later nodes.

... I hope that additional properties will not affect. I adopt now payload with all infos and some more properties. I just want to have payload only with properties that I need but if that is not possible it is also OK as long as it works.

Of course it is possible, @UnborN showed you how to do it, but there is no point adding the additional overhead of creating a new message unless there is some benefit.

... maybe clarity for the user to see exactly what is now in the msg ?

I know for you and I that it doesn't really matter (and is often preferable) to just modify the part of the msg we need later - but it can make debugging messier, and clarity when you come back to it in 6 months may be more useful.

Just a word of caution, if I may. Messages sometimes have properties that we don't notice but are needed for the proper operation of a flow. For example, messages in a sequence produced by a split node have a msg.parts property that is used by downstream join, sort, and batch nodes. Also, if a message passes through a link-out node, it gets a msg._linkSource property that allows it to return to the calling flow. Various nodes use properties like msg.target, msg.reset, and others for specific purposes, so if any of these have been set before the message reaches your function node, there could be issues downstream. You might consider the balance between having a "clean" message structure in your current flow and creating bugs if you modify the flow or re-use it in new projects.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.