Split msg.payload "P-131" to Letters and Numbers

Hello Forum,
i want to process Data from a external Device and the Data looks like "P-131_SPO=-200W"

i want to process the Filed "P-131_SPO" -> -131
and The Value "-200W" -> -200 (W)

split at "=" and "_" works well, but i can´t find a splitter / seperator to the letters form the Numbers ( in String)

thanks for your help in advanced
olby2

My Flow:
( I split it here fixed with "P" but that is not a allover solution )

[
    {
        "id": "1ab3bd40d14573ed",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "df6d6c66.b6c4e",
        "type": "split",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "splt": "=",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "xy",
        "x": 550,
        "y": 260,
        "wires": [
            [
                "3ba2aff1.e8af5",
                "9052efa8.e5f0a"
            ]
        ]
    },
    {
        "id": "d9822859.20a7b8",
        "type": "inject",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "",
        "topic": "",
        "payload": "P-131_SPO=-200W",
        "payloadType": "str",
        "x": 320,
        "y": 260,
        "wires": [
            [
                "bc23045.f5103f8",
                "df6d6c66.b6c4e"
            ]
        ]
    },
    {
        "id": "2f990562.44d8ea",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "out 1",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 100,
        "wires": []
    },
    {
        "id": "fe502aa1.ee37c8",
        "type": "change",
        "z": "1ab3bd40d14573ed",
        "name": "to number",
        "rules": [
            {
                "t": "set",
                "p": "payload[1]",
                "pt": "msg",
                "to": "$number(msg.payload)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1090,
        "y": 580,
        "wires": [
            [
                "d6f23ad6.490eb8"
            ]
        ]
    },
    {
        "id": "9052efa8.e5f0a",
        "type": "switch",
        "z": "1ab3bd40d14573ed",
        "name": "route",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 710,
        "y": 260,
        "wires": [
            [
                "2f990562.44d8ea",
                "c2ce6b8c765f31cc"
            ],
            [
                "35825546.228baa"
            ]
        ]
    },
    {
        "id": "35825546.228baa",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "out 2",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 860,
        "y": 400,
        "wires": []
    },
    {
        "id": "bc23045.f5103f8",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "source data",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 450,
        "y": 600,
        "wires": []
    },
    {
        "id": "3ba2aff1.e8af5",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "spltted",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 680,
        "y": 500,
        "wires": []
    },
    {
        "id": "d6f23ad6.490eb8",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "converted",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1390,
        "y": 580,
        "wires": []
    },
    {
        "id": "c2ce6b8c765f31cc",
        "type": "split",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "splt": "_",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 910,
        "y": 240,
        "wires": [
            [
                "b37d5ea810ae500e",
                "d26639cda9cc46fb"
            ]
        ]
    },
    {
        "id": "81c9a0927cc47837",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "Feldnummer",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1660,
        "y": 340,
        "wires": []
    },
    {
        "id": "ab6aafad6bfcafec",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1310,
        "y": 200,
        "wires": []
    },
    {
        "id": "05e66712e76b09f5",
        "type": "switch",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "$split(msg.payload,\"P\" )",
                "vt": "jsonata"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1490,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "b37d5ea810ae500e",
        "type": "switch",
        "z": "1ab3bd40d14573ed",
        "name": "separator",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1120,
        "y": 260,
        "wires": [
            [
                "ab6aafad6bfcafec",
                "6f51716ebffcbe6a"
            ],
            [
                "0f8f4bed7a12472e"
            ]
        ]
    },
    {
        "id": "0f8f4bed7a12472e",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1310,
        "y": 380,
        "wires": []
    },
    {
        "id": "d26639cda9cc46fb",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1110,
        "y": 200,
        "wires": []
    },
    {
        "id": "6f51716ebffcbe6a",
        "type": "split",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "splt": "P",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 1320,
        "y": 280,
        "wires": [
            [
                "05e66712e76b09f5",
                "81c9a0927cc47837"
            ]
        ]
    }
]

Looking at your flow this is definitely one time I would recommend a single function node with a regex or plain ole JavaScript.

Do you know how to do that?

thanks for your quick reply
I have read "regex" in the description of jsonata, but cannot yet implement it

olby2

Here is a working demo...

Flow (CTRL+I to import)

[{"id":"50e54099284d1dd1","type":"inject","z":"1ab3bd40d14573ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"","topic":"","payload":"P-131_SPO=-200W","payloadType":"str","x":1940,"y":100,"wires":[["99794fc892202adf"]]},{"id":"99794fc892202adf","type":"function","z":"1ab3bd40d14573ed","name":"","func":"const regex = /([-\\d]*)_(\\w*)=([-\\d]*)(\\w*)/gm;\nconst str = msg.payload\nlet m;\nlet result;\n\nwhile ((m = regex.exec(str)) !== null) {\n    // This is necessary to avoid infinite loops with zero-width matches\n    if (m.index === regex.lastIndex) {\n        regex.lastIndex++;\n    }\n    result = m;\n}\nif (result && result.length == 5) {\n    msg.payload = {\n        v1: Number(result[1]),\n        v1UOM: result[2],\n        v2: Number(result[3]),\n        v2UOM: result[4],\n    }\n    return msg;\n} else {\n    node.warn(`cannot parse '${str}'`);\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2140,"y":100,"wires":[["6a848f4a0f89af00"]]},{"id":"6a848f4a0f89af00","type":"debug","z":"1ab3bd40d14573ed","name":"converted","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":2130,"y":160,"wires":[]}]

And this is where I tested the regex...

thanks a lot for your quick help. this works for this string exactly as I imagined.
I also played something more complicated and it probably doesn't work with "<>" and floating point numbers are also a problem

I have the formatting on ([- \ d] *) _ (\ D *) = ([- \ d /.] *) (\ w *)
changed and can now also process this "P131_ SPO = -200.87W"
Many Thanks
, that helped me alot
olby2

1 Like

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