Convert string value to something to work with

Hello All,

With help from the community on here I managed to get my project up and working. Then unfortunately I have had to change the way the data comes into Node Red.

Somehow I need to split the values in the string and covert to numbers so I can work with then in a function node.

I've been trying substring without much joy.

Heres how they are coming in now from Mosquito mqtt node -

Screen Shot 2022-06-04 at 02.54.46

Most things I try result in - unexpected token in JSON position blah blah or empty payload?

Just change the payload from a string to an array with a comma as separator and at least starting with the second element, each value can easily converted nto a number, using the $number function

Thank you very much for your reply. I'm not the greatest at Node Red. How would you go about doing this please? I've had a few tries but I cant get it to work.

Copy the payload string here, then I can do a flow for you. But as screenshot I have to type your string manually.

Thank you very much.

2022/06/04 04:49:19,1,0.066,-0.16,2,22.2883,21.8716,3,1.238,21.0,0.498,101.95,4,1818.99,21.0,0.0

I was trying this var output = msg.payload.split(","); in a function node, but didn't seem to work.

Using your string - you can do something like this:

or instead of join - you could also directly access the values:

[
    {
        "id": "b58dd0fccc7f187b",
        "type": "switch",
        "z": "5b209b6d5006b849",
        "name": "",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "gt",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": true,
        "outputs": 2,
        "x": 470,
        "y": 500,
        "wires": [
            [
                "695bea8f95690585"
            ],
            [
                "bcca667b44d996d5"
            ]
        ]
    },
    {
        "id": "5ef6e01d5e727c8d",
        "type": "inject",
        "z": "5b209b6d5006b849",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "2022/06/04 04:49:19,1,0.066,-0.16,2,22.2883,21.8716,3,1.238,21.0,0.498,101.95,4,1818.99,21.0,0.0",
        "payloadType": "str",
        "x": 170,
        "y": 500,
        "wires": [
            [
                "4b7c3669c59df552"
            ]
        ]
    },
    {
        "id": "4b7c3669c59df552",
        "type": "split",
        "z": "5b209b6d5006b849",
        "name": "",
        "splt": ",",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 330,
        "y": 500,
        "wires": [
            [
                "b58dd0fccc7f187b"
            ]
        ]
    },
    {
        "id": "bcca667b44d996d5",
        "type": "change",
        "z": "5b209b6d5006b849",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$number($trim(payload))\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "values",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 670,
        "y": 520,
        "wires": [
            [
                "28980d0cccce3e3a"
            ]
        ]
    },
    {
        "id": "8fe60570c7c237ca",
        "type": "debug",
        "z": "5b209b6d5006b849",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1130,
        "y": 480,
        "wires": []
    },
    {
        "id": "28980d0cccce3e3a",
        "type": "join",
        "z": "5b209b6d5006b849",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "1",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 850,
        "y": 520,
        "wires": [
            [
                "6d6cd0183b92c966"
            ]
        ]
    },
    {
        "id": "695bea8f95690585",
        "type": "change",
        "z": "5b209b6d5006b849",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "date",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 660,
        "y": 480,
        "wires": [
            [
                "6d6cd0183b92c966"
            ]
        ]
    },
    {
        "id": "6d6cd0183b92c966",
        "type": "join",
        "z": "5b209b6d5006b849",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 990,
        "y": 480,
        "wires": [
            [
                "8fe60570c7c237ca"
            ]
        ]
    }
]

Just to split the string into an array - you do not need a function node as well - a change node already does this job - if you dont want to use a split node:

Thank you very much. For some reason I cannot import the flow?

Try again - i removed the chars at beginning and end.

Even a function node does work to convert into an array - but as function nodes are nontransparent - I avoid to code.

msg.payload = msg.payload.split(",");
return msg;

Excellent. That works!

Thank you very much for helping.

Now just to change my function node and should get it all graphing!

If you simply pass that through a csv node you should get the values as numbers. You can even name the object properties.

1 Like

Nice one Steve. Worked perfectly.

Thank you very much!