Strange Array/Object behaviour DataValue

I am using the node-red-contrib-opcua client node to read multiple variables at once. When i use node.warn() to debug the whole msg.payload array, i can access the different objects in it how i am used to it:


But when i try to access a single object directly, i get the following result:
image
Is there a way to convert this to an object?

try

node.warn(msg.payload[1])

This is what i tried. The unexpected result is in the second screenshot.

Hi,

  • What version of Node RED are you using?
  • Any chance you can share the whole payload array JSON?

Hi, i am using Node-RED version 2.2.2. The payload array looks like this:

[{"value":{"dataType":"Boolean","arrayType":"Scalar","value":true},"statusCode":{"value":0},"sourceTimestamp":"2022-09-13T05:29:42.492Z","sourcePicoseconds":897000000,"serverTimestamp":"2022-09-13T05:29:42.492Z","serverPicoseconds":897000000},{"value":{"dataType":"Boolean","arrayType":"Scalar","value":false},"statusCode":{"value":0},"sourceTimestamp":"2022-09-13T05:29:42.492Z","sourcePicoseconds":997100000,"serverTimestamp":"2022-09-13T05:29:42.492Z","serverPicoseconds":997100000}]

I see no problem (NR 3.0.2).

Nor do I see any data element called DataValue.
Can you show us the code that produces this output?
image

What do you mean by " access a single object directly"?

By acces a single object directly i mean msg.payload[1] instead of the whole array msg.payload. This is the Flow:

[{"id":"97628b22cec87e27","type":"tab","label":"OPCUA Excample","disabled":false,"info":"","env":[]},{"id":"c0bad6bc2c4de37a","type":"function","z":"97628b22cec87e27","name":"serialize","func":"let outputArr = msg.out;\nmsg = [outputArr];\n\nnode.warn(msg)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":180,"wires":[["10e5cce26a093390"]]},{"id":"f9c934ec5832f40b","type":"function","z":"97628b22cec87e27","name":"create target array","func":"\nconst targetArray = [\"ns=3;s=\\\"DB_OPCUA\\\".\\\"Roboter2\\\".\\\"Ampel\\\".\\\"rot\\\"\", \"ns=3;s=\\\"DB_OPCUA\\\".\\\"BinPicking\\\".\\\"Ampel\\\".\\\"gelb\\\"\"]\n\nconst outputArray = targetArray.map(elem => {\n    return {\n        topic: elem.toString()\n    }\n})\n\nmsg.out = outputArray;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":180,"wires":[["c0bad6bc2c4de37a"]]},{"id":"667ae29a94ccd7d3","type":"inject","z":"97628b22cec87e27","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":180,"wires":[["f9c934ec5832f40b"]]},{"id":"10e5cce26a093390","type":"OpcUa-Client","z":"97628b22cec87e27","endpoint":"d36ff8a2b9f61387","action":"readmultiple","deadbandtype":"a","deadbandvalue":1,"time":10,"timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","folderName4PKI":"","name":"","x":700,"y":180,"wires":[["0c48a5bbf1a793bb"]]},{"id":"0c48a5bbf1a793bb","type":"function","z":"97628b22cec87e27","name":"output","func":"node.warn(msg.payload)\nnode.warn(msg.payload[1])","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":180,"wires":[[]]},{"id":"d5f0659805b3e9c4","type":"function","z":"97628b22cec87e27","name":"readmultiple","func":"msg.payload = \"ALL\";\nmsg.topic = \"readmultiple\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":260,"wires":[["10e5cce26a093390"]]},{"id":"27dff7c1a8644d14","type":"inject","z":"97628b22cec87e27","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":260,"wires":[["d5f0659805b3e9c4"]]},{"id":"d36ff8a2b9f61387","type":"OpcUa-Endpoint","endpoint":"opc.tcp://192.168.1.110:4840","secpol":"None","secmode":"None","login":false}]

I'm leaning towards some overload on the node internally.
it uses the flatted package - that does some stuff to aid in JSON structures.

its quite possible - when Node RED is parsing it - the node is overloading something, to produce the output.

doesn't explain when debugging payload though.

very strange :man_shrugging:
I tested under 2.2.2 - and I don't get the same problem

Using the data you posted and your "output" function
Untitled 1

[{"id":"f76632171fc65c2e","type":"inject","z":"afdab898a6a23bf5","name":"Fabi's array","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"value\":{\"dataType\":\"Boolean\",\"arrayType\":\"Scalar\",\"value\":true},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2022-09-13T05:29:42.492Z\",\"sourcePicoseconds\":897000000,\"serverTimestamp\":\"2022-09-13T05:29:42.492Z\",\"serverPicoseconds\":897000000},{\"value\":{\"dataType\":\"Boolean\",\"arrayType\":\"Scalar\",\"value\":false},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2022-09-13T05:29:42.492Z\",\"sourcePicoseconds\":997100000,\"serverTimestamp\":\"2022-09-13T05:29:42.492Z\",\"serverPicoseconds\":997100000}]","payloadType":"jsonata","x":210,"y":100,"wires":[["8c3b3cb619a849ad"]]},{"id":"1d0a83ed83fd300f","type":"debug","z":"afdab898a6a23bf5","name":"debug 257","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":100,"wires":[]},{"id":"8c3b3cb619a849ad","type":"function","z":"afdab898a6a23bf5","name":"output","func":"node.warn(msg.payload)\nnode.warn(msg.payload[1])","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":100,"wires":[["1d0a83ed83fd300f"]]}]

I get this

What does this flow output for you?

With your flow i get the exact same output as you.

Then perhaps a debug node set to show the complete message coming out of your opcua node might shed some light?

I think @marcus-j-davies knows more about this than me since I don't use opcua.

that is the complete message of the opcua node after i read the stored nodeIds

{"topic":"ALL","payload":[{"value":{"dataType":"Boolean","arrayType":"Scalar","value":true},"statusCode":{"value":0},"sourceTimestamp":"2022-09-13T06:10:17.878Z","sourcePicoseconds":972500000,"serverTimestamp":"2022-09-13T06:10:17.878Z","serverPicoseconds":972500000},{"value":{"dataType":"Boolean","arrayType":"Scalar","value":false},"statusCode":{"value":0},"sourceTimestamp":"2022-09-13T06:10:17.879Z","sourcePicoseconds":113400000,"serverTimestamp":"2022-09-13T06:10:17.879Z","serverPicoseconds":113400000}],"items":[{"nodeId":"ns=3;s=\"DB_OPCUA\".\"Roboter2\".\"Ampel\".\"rot\"","attributeId":13,"TimestampsToReturn":2},{"nodeId":"ns=3;s=\"DB_OPCUA\".\"BinPicking\".\"Ampel\".\"gelb\"","attributeId":13,"TimestampsToReturn":2}],"_msgid":"4cc97809d157ba26"}

I don't see any problem when I feed that payload into your output node, sorry.

Thank you anyways for your help and your time!

I'm pretty sure, the node is overloading the output during stringify.

The below demos it - it doesn't fully explain the behaviour, but its all I have.

[{"id":"a7efc2d9be2f1751","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"1cd906e5aef9b6b5","type":"inject","z":"a7efc2d9be2f1751","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":165,"y":150,"wires":[["0ed656291449cc9b"]]},{"id":"0ed656291449cc9b","type":"function","z":"a7efc2d9be2f1751","name":"Array","func":"return {\n  \"topic\": \"ALL\",\n  \"payload\": [\n    {\n      toJSON: function(){\n        return{\n          payload: \"Not what you expect\"\n        }\n      },\n      \"value\": {\n        \"dataType\": \"Boolean\",\n        \"arrayType\": \"Scalar\",\n        \"value\": true\n      },\n      \"statusCode\": {\n        \"value\": 0\n      },\n      \"sourceTimestamp\": \"2022-09-13T06:10:17.878Z\",\n      \"sourcePicoseconds\": 972500000,\n      \"serverTimestamp\": \"2022-09-13T06:10:17.878Z\",\n      \"serverPicoseconds\": 972500000\n    },\n    {\n      \"value\": {\n        \"dataType\": \"Boolean\",\n        \"arrayType\": \"Scalar\",\n        \"value\": false\n      },\n      \"statusCode\": {\n        \"value\": 0\n      },\n      \"sourceTimestamp\": \"2022-09-13T06:10:17.879Z\",\n      \"sourcePicoseconds\": 113400000,\n      \"serverTimestamp\": \"2022-09-13T06:10:17.879Z\",\n      \"serverPicoseconds\": 113400000\n    }\n  ],\n  \"items\": [\n    {\n      \"nodeId\": \"ns=3;s=\\\"DB_OPCUA\\\".\\\"Roboter2\\\".\\\"Ampel\\\".\\\"rot\\\"\",\n      \"attributeId\": 13,\n      \"TimestampsToReturn\": 2\n    },\n    {\n      \"nodeId\": \"ns=3;s=\\\"DB_OPCUA\\\".\\\"BinPicking\\\".\\\"Ampel\\\".\\\"gelb\\\"\",\n      \"attributeId\": 13,\n      \"TimestampsToReturn\": 2\n    }\n  ],\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":235,"y":340,"wires":[["3d6261a6c24b0a21"]]},{"id":"3d6261a6c24b0a21","type":"function","z":"a7efc2d9be2f1751","name":"node warn (0)","func":"node.warn(msg.payload[0])","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":465,"y":340,"wires":[[]]}]

Thank you also for your answer. My temporary solution is to do node.warn(msg.payload[1].toJSON())
to get the expected result.

Are you actually using node-red and not some kind of variant ?

I am using it in a Docker Container but should be the normal version of Node-RED.

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