Create function node to send msg.payload.variableValue from existing msg.payload

Hey,
i'm building a connection between an MQTT Input and an OPC-UA Server (node-red-contrib-opcua).

To give the opc-ua server node a value i need to send my value from the MQTT input as "msg.payload.variableValue". I'm trying to achieve this by using the function node. My OPC-UA Server debugger tells me the variableValue is missing and it seems i can't figure out how to give this value to the server node.

This is the flow i'm using:

[{"id":"cfb91e4987f2c163","type":"tab","label":"Flow 1 Paul","disabled":false,"info":"","env":[]},{"id":"abf7079a.653be8","type":"mqtt in","z":"cfb91e4987f2c163","name":"","topic":"esp32/temperature","qos":"2","datatype":"utf8","broker":"10e78a89.5b4fd5","nl":false,"rap":false,"rh":"0","inputs":0,"x":150,"y":260,"wires":[["470a98984a913228","d65592b613ad7c29"]]},{"id":"ee410d56cb73180b","type":"OpcUa-Server","z":"cfb91e4987f2c163","port":"4841","name":"NROPC","endpoint":"","users":"users.json","nodesetDir":"","folderName4PKI":"","autoAcceptUnknownCertificate":true,"registerToDiscovery":false,"constructDefaultAddressSpace":true,"allowAnonymous":true,"endpointNone":true,"endpointSign":true,"endpointSignEncrypt":true,"endpointBasic128Rsa15":true,"endpointBasic256":true,"endpointBasic256Sha256":true,"maxNodesPerBrowse":"1000","maxNodesPerHistoryReadData":0,"maxNodesPerHistoryReadEvents":0,"maxNodesPerHistoryUpdateData":0,"maxNodesPerRead":"1000","maxNodesPerWrite":"1000","maxNodesPerMethodCall":0,"maxNodesPerRegisterNodes":0,"maxNodesPerNodeManagement":0,"maxMonitoredItemsPerCall":0,"maxNodesPerHistoryUpdateEvents":0,"maxNodesPerTranslateBrowsePathsToNodeIds":0,"maxConnectionsPerEndpoint":"1000","maxMessageSize":4096,"maxBufferSize":4096,"x":560,"y":140,"wires":[["3e828e8b98926363"]]},{"id":"3e828e8b98926363","type":"debug","z":"cfb91e4987f2c163","name":"debug 3","active":false,"tosidebar":false,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":140,"wires":[]},{"id":"9a4ac34d270b0fa5","type":"inject","z":"cfb91e4987f2c163","name":"add BME688_TEMP (FLOAT)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=1;s=BME688_TEMP;datatype=Float","payload":"{\"opcuaCommand\":\"addVariable\"}","payloadType":"json","x":180,"y":140,"wires":[["ee410d56cb73180b"]]},{"id":"854f1b9146b1e3fe","type":"inject","z":"cfb91e4987f2c163","d":true,"name":"Set TestVariable2 23.4","props":[{"p":"payload.messageType","v":"Variable","vt":"str"},{"p":"payload.variableName","v":"BME688_TEMP","vt":"str"},{"p":"payload.datatype","v":"Float","vt":"str"},{"p":"payload.namespace","v":"1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":200,"wires":[["ee410d56cb73180b"]]},{"id":"470a98984a913228","type":"debug","z":"cfb91e4987f2c163","name":"debug 4","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":260,"wires":[]},{"id":"d65592b613ad7c29","type":"function","z":"cfb91e4987f2c163","name":"function 1","func":"var newMsg = { payload : msg.payload.variableValue = msg.payload };\nreturn newMsg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":300,"wires":[["3266d2d1ac095ebe","ee410d56cb73180b"]]},{"id":"3266d2d1ac095ebe","type":"debug","z":"cfb91e4987f2c163","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":740,"y":320,"wires":[]},{"id":"10e78a89.5b4fd5","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I'd be happy about any help, thanks in advance.

You can certainly do it with a function node, better to use a change node.

Here are both approaches below your own function node.

[{"id":"d65592b613ad7c29","type":"function","z":"cfb91e4987f2c163","name":"function 1","func":"var newMsg = { payload : msg.payload.variableValue = msg.payload };\nreturn newMsg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":60,"wires":[["3266d2d1ac095ebe"]]},{"id":"3266d2d1ac095ebe","type":"debug","z":"cfb91e4987f2c163","name":"Function 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":850,"y":60,"wires":[]},{"id":"53731864e178231d","type":"inject","z":"cfb91e4987f2c163","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":440,"y":120,"wires":[["d65592b613ad7c29","a344f3fc27b494e1","d3d2b006a4a4c2b7"]]},{"id":"a344f3fc27b494e1","type":"function","z":"cfb91e4987f2c163","name":"function 18","func":"msg.payload = {variableValue: msg.payload };\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":120,"wires":[["e3822e7944ecdf5f"]]},{"id":"d3d2b006a4a4c2b7","type":"change","z":"cfb91e4987f2c163","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.variableValue","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":180,"wires":[["85f31e28068135b9"]]},{"id":"e3822e7944ecdf5f","type":"debug","z":"cfb91e4987f2c163","name":"Function 18","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":850,"y":120,"wires":[]},{"id":"85f31e28068135b9","type":"debug","z":"cfb91e4987f2c163","name":"Change node","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":860,"y":180,"wires":[]}]

hey, thanks for your help. I've tried to figure it out with a change node but it doesn't work like intended. Any ideas on what i'm doing wrong? I think it might rather be an issue with how i give the variableValue to the OPC-UA Server at this point. When i observe the Server with a client i get 0 as a value for the temperature.

This is the code i'm using:

[{"id":"cfb91e4987f2c163","type":"tab","label":"Flow 1 Paul","disabled":false,"info":"","env":[]},{"id":"c802b424839907e3","type":"junction","z":"cfb91e4987f2c163","x":400,"y":200,"wires":[["ee410d56cb73180b"]]},{"id":"abf7079a.653be8","type":"mqtt in","z":"cfb91e4987f2c163","name":"","topic":"esp32/temperature","qos":"2","datatype":"utf8","broker":"10e78a89.5b4fd5","nl":false,"rap":false,"rh":"0","inputs":0,"x":150,"y":260,"wires":[["470a98984a913228","e54464b5614c69fc"]]},{"id":"ee410d56cb73180b","type":"OpcUa-Server","z":"cfb91e4987f2c163","port":"4841","name":"NROPC","endpoint":"","users":"users.json","nodesetDir":"","folderName4PKI":"","autoAcceptUnknownCertificate":true,"registerToDiscovery":false,"constructDefaultAddressSpace":true,"allowAnonymous":true,"endpointNone":true,"endpointSign":true,"endpointSignEncrypt":true,"endpointBasic128Rsa15":true,"endpointBasic256":true,"endpointBasic256Sha256":true,"maxNodesPerBrowse":"1000","maxNodesPerHistoryReadData":0,"maxNodesPerHistoryReadEvents":0,"maxNodesPerHistoryUpdateData":0,"maxNodesPerRead":"1000","maxNodesPerWrite":"1000","maxNodesPerMethodCall":0,"maxNodesPerRegisterNodes":0,"maxNodesPerNodeManagement":0,"maxMonitoredItemsPerCall":0,"maxNodesPerHistoryUpdateEvents":0,"maxNodesPerTranslateBrowsePathsToNodeIds":0,"maxConnectionsPerEndpoint":"1000","maxMessageSize":4096,"maxBufferSize":4096,"x":620,"y":140,"wires":[["3e828e8b98926363"]]},{"id":"3e828e8b98926363","type":"debug","z":"cfb91e4987f2c163","name":"debug 3","active":false,"tosidebar":false,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":790,"y":140,"wires":[]},{"id":"9a4ac34d270b0fa5","type":"inject","z":"cfb91e4987f2c163","name":"add BME688_TEMP (FLOAT)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=1;s=BME688_TEMP;datatype=Float","payload":"{\"opcuaCommand\":\"addVariable\"}","payloadType":"json","x":180,"y":140,"wires":[["ee410d56cb73180b"]]},{"id":"854f1b9146b1e3fe","type":"inject","z":"cfb91e4987f2c163","d":true,"name":"Set TestVariable2 23.4","props":[{"p":"payload.messageType","v":"Variable","vt":"str"},{"p":"payload.variableName","v":"BME688_TEMP","vt":"str"},{"p":"payload.datatype","v":"Float","vt":"str"},{"p":"payload.namespace","v":"1","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":200,"wires":[["c802b424839907e3"]]},{"id":"470a98984a913228","type":"debug","z":"cfb91e4987f2c163","name":"debug 4","active":false,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":260,"wires":[]},{"id":"e54464b5614c69fc","type":"change","z":"cfb91e4987f2c163","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.variableValue","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":300,"wires":[["8e2cc42f5cd0e5b6","c802b424839907e3"]]},{"id":"8e2cc42f5cd0e5b6","type":"debug","z":"cfb91e4987f2c163","name":"debug 9","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":340,"wires":[]},{"id":"10e78a89.5b4fd5","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

If you want to make the property name variable you would do it like this

[{"id":"53731864e178231d","type":"inject","z":"cfb91e4987f2c163","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":440,"wires":[["a344f3fc27b494e1","d3d2b006a4a4c2b7"]]},{"id":"a344f3fc27b494e1","type":"function","z":"cfb91e4987f2c163","name":"function 18","func":"let variableValue = \"test\";\nmsg.payload = {[variableValue]: msg.payload };\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":440,"wires":[["e3822e7944ecdf5f"]]},{"id":"d3d2b006a4a4c2b7","type":"change","z":"cfb91e4987f2c163","name":"","rules":[{"t":"set","p":"variableValue","pt":"msg","to":"test","tot":"str"},{"t":"move","p":"payload","pt":"msg","to":"payload[msg.variableValue]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":500,"wires":[["85f31e28068135b9"]]},{"id":"e3822e7944ecdf5f","type":"debug","z":"cfb91e4987f2c163","name":"Function 18","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":610,"y":440,"wires":[]},{"id":"85f31e28068135b9","type":"debug","z":"cfb91e4987f2c163","name":"Change node","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":620,"y":500,"wires":[]}]
let variableValue = "test";
msg.payload = {[variableValue]: msg.payload };
return msg;
1 Like

Hey thanks for the help. I still can't figure out how to give the msg.payload.variableValue to the OPC Server. Could you show me how to implement your solution into my flow?

Edit: I think the problem is how to join the messages from the node which sets the Variable and the MQTT node from which i change msg.payload to msg.payload.variableValue but i might be wrong...

See this article in the cookbook for an example of how to join messages into one object.

Just for others who have interested discussion was also here:

Thanks colin, i tried to join a complete message to create a merged object but i still don't get any value in UAExpert (OPC-UA Client).

I also added string to number conversion for my MQTT input value which then is inserted to the change node, moving msg.payload to msg.payload.variableValue

I got it to work: I used a function node to solve my issue. The function node sets messageType, variableName, datatype and namespace and it takes the old msg.payload and set's it as the variableValue.

The code i used in the function node:

var temperature =msg.payload;
msg.payload = { messageType : "Variable", variableName : "BME688_TEMP", datatype: "Float", namespace: "1", variableValue: temperature };
return msg;

Thanks for all the help.

1 Like

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