OPCUA data parsing issue

Ok I set up two streams. One with just getting the raw output and then the other with the change node
First I get the raw output and copy:

Then Paste it in the test field of the change node:

Is there a command I can send to the OPC server to ask it what type of character encoding its using?
How would I pass an environment variable to the opc server to test with vt100 etc?

Javascript strings handle all UTF-8 characters, so the "squares" will not affect the string handling -- and the regex will be ignoring those characters anyway.

The error message is because you need a valid msg object in the example data, as @struland mentioned. This is why I suggested that you replace (only) the test string "hello world" with your data, wrapped in double quotes, and with \n replacing any newline characters. This is required, so the test data is valid JSON, and matches what would have been in the original debug msg.

You can either use the debug sidebar "Copy value" button to grab the entire msg (if you are showing the complete msg), OR put your data in an object wrapper, like so:

{
    "payload": "... your data goes here \n all on one line \n with \n in place of newlines..."
}

Now that I look at your data in the debug messages, I think you will also have issues with the regex using that e\[3 syntax I wrote earlier -- it works with your sample data because that little square char is an escape and it copied as the letter 'e'. You can match any one char before the left square brackets by changing that section of the regex to .\[3

Edit:
I found that to match the ANSI escape char (hex 1B) the correct regex syntax would be:
.$match(/^\x1B33m (\w+) .*: (\S+)/) WITHOUT the square bracket
(since it is part of the escape sequence) although I have not tried it. Good luck!

Thank you for that! I am sorry I borked the replace only the hello world section.
I've tried with the correct regex syntax but have run into another issue though. I cant seem to get any results, I end up with this unexpected token error:

Invalid example JSON message:
SyntaxError: Unexpected token e in JSON at position 19

I too am having trouble with the JSONata example tester, while trying to use data with non-printable esc characters in it... this was not problem when I was using the copied/pasted ascii string.

If you don't mind attaching your data as a file, I can re-write the regex processing in plain JS for use in a function node. That would bypass any funny string handling between node-red and the jsonata library.

Attached is the copied output as well as the flow.
Should I save the text file in a different format or does this work?
flows.json (19.4 KB)
MachineInfo.txt (831 Bytes)

Using a file read node, I was able to get your data with esc chars into the payload. The good news is that a regex CAN work in a JSONata expression -- but the test tab requires a valid JSON string, and so cannot work with your data directly.

Here is the exported change node I used:

[{"id":"38562d4f8250e38d","type":"change","z":"7875ce28fe8aee2d","name":"J:$match","rules":[{"t":"set","p":"props","pt":"msg","to":"payload\t    .$split('\\n')\t    .$match(/\\x1B\\[33m (\\w+) .*: (.*)/m)\t    .[groups] {\t        $[0]: $[1].$replace(/\\x1B\\[\\d\\dm/, '')\t    }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":560,"wires":[["521b07ccb0e6c506"]]}]

and here is the equivalent function code:

let lines = msg.payload.split('\n');
msg.props = {}; // empty object
for (const line of lines) {
    let parts = line.match(/\x1B\[33m (\w+) .*: (.*)/);
    if (parts) {
        msg.props[parts[1]] = parts[2].replace(/\x1B\[\d\dm/g, '');
    }
}
return msg;

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