RegEx in function-node

Hi all Gurus:
I just threw my self into the node-red carousell, and imediately ran into problems.

I Created a flow where I SSH to all my Raspberry Pis, requested each and other CPU-temp.
Those temps are forwarded to a MQTT node and passed on th HomeBridge.
Finally my Apple Homekit will show every RPi tem in my Home app.

Everything good, the temps are collected and passed on to MQTT picked up by HomeBridge.
The Problem is that HomeBridge can not interpret the information since the valu is delivered in below form.temp=51.6'C.

Are there any easy way to remove the first 5 and the last 2 chars, so I only have the remaining value 51.6

My best to all of you
Ronnie

If the temp=51.6'C string is in msg.payload
you could split on = and get the 2nd element
then split again on 'C and get the first element
then convert to float

Example Flow

[{"id":"7d2de642b1178ba1","type":"function","z":"54efb553244c241f","name":"","func":"msg.payload = msg.payload.split(\"=\")[1]\nmsg.payload = msg.payload.split(\"'C\")[0]\nmsg.payload = parseFloat(msg.payload)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":1020,"wires":[["cac2c2a69f6f7a36"]]},{"id":"0b68eeee0c34998c","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"temp=51.6'C","payloadType":"str","x":410,"y":1020,"wires":[["7d2de642b1178ba1"]]},{"id":"cac2c2a69f6f7a36","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":1020,"wires":[]}]

No need to remove the end part. parseInt and parseFloat will ignore non numeric following characters.

1 Like

Thx for the reply @UnborN . Implementing your function I get
"TypeError: msg.payload.split is not a function"
Any idea why?

Where to put that argument?

We can't really help if you don't show us the payload before it is passed to HomeBridge

Just a FYI, I also parse some info from iwconfig and publish all of it to the broker from a bash script. It then gets stuffed into InfluxDB. Granted, I have to disseminate the script to each RPi and install Mosquitto on each (I choose to use the pub that comes with it).

Yeah, just another way to do it. In my case, it suits me fine to have one node-red rpi, to request the info and publish it.

Sure - bare over with me, I´m still a NooB

How do I paste in an exampleflow like UnborN did?

I only need a screenshot of the debug output (which you can paste directly into a reply)

As for exporting a flow - select the nodes of interest, hit CTRL-E , copy to clipboard, press the </> button in the reply & paste copied flow.

Ps, also show (screenshot) of the sidebar context view

[
    {
        "id": "46f2b21fc0a7b159",
        "type": "bigssh",
        "z": "c26b315aa04283b1",
        "name": "",
        "commandLine": "vcgencmd measure_temp",
        "commandArgs": "",
        "minError": 1,
        "minWarning": 1,
        "noStdin": false,
        "format": "",
        "payloadIsArg": false,
        "myssh": "d000d91ce608155d",
        "x": 570,
        "y": 480,
        "wires": [
            [
                "0c2aa01faef5cfc4"
            ],
            [],
            []
        ]
    },
    {
        "id": "0c2aa01faef5cfc4",
        "type": "mqtt out",
        "z": "c26b315aa04283b1",
        "name": "",
        "topic": "Home/rpis/nodered/temp",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "7540db4618164de1",
        "x": 830,
        "y": 480,
        "wires": []
    },
    {
        "id": "de04f0948503aa8c",
        "type": "bigtimer",
        "z": "c26b315aa04283b1",
        "outtopic": "",
        "outpayload1": "",
        "outpayload2": "",
        "name": "Big Timer",
        "comment": "",
        "lat": 0,
        "lon": 0,
        "starttime": 5001,
        "endtime": 1425,
        "starttime2": 0,
        "endtime2": 0,
        "startoff": 0,
        "endoff": 0,
        "startoff2": 0,
        "endoff2": 0,
        "offs": 0,
        "outtext1": "",
        "outtext2": "",
        "timeout": 1440,
        "sun": true,
        "mon": true,
        "tue": true,
        "wed": true,
        "thu": true,
        "fri": true,
        "sat": true,
        "jan": true,
        "feb": true,
        "mar": true,
        "apr": true,
        "may": true,
        "jun": true,
        "jul": true,
        "aug": true,
        "sep": true,
        "oct": true,
        "nov": true,
        "dec": true,
        "day1": 0,
        "month1": 0,
        "day2": 0,
        "month2": 0,
        "day3": 0,
        "month3": 0,
        "day4": 0,
        "month4": 0,
        "day5": 0,
        "month5": 0,
        "day6": 0,
        "month6": 0,
        "day7": 0,
        "month7": 0,
        "day8": 0,
        "month8": 0,
        "day9": 0,
        "month9": 0,
        "day10": 0,
        "month10": 0,
        "day11": 0,
        "month11": 0,
        "day12": 0,
        "month12": 0,
        "d1": 0,
        "w1": 0,
        "d2": 0,
        "w2": 0,
        "d3": 0,
        "w3": 0,
        "d4": 0,
        "w4": 0,
        "d5": 0,
        "w5": 0,
        "d6": 0,
        "w6": 0,
        "xday1": 0,
        "xmonth1": 0,
        "xday2": 0,
        "xmonth2": 0,
        "xday3": 0,
        "xmonth3": 0,
        "xday4": 0,
        "xmonth4": 0,
        "xday5": 0,
        "xmonth5": 0,
        "xday6": 0,
        "xmonth6": 0,
        "xday7": 0,
        "xmonth7": 0,
        "xday8": 0,
        "xmonth8": 0,
        "xday9": 0,
        "xmonth9": 0,
        "xday10": 0,
        "xmonth10": 0,
        "xday11": 0,
        "xmonth11": 0,
        "xday12": 0,
        "xmonth12": 0,
        "xd1": 0,
        "xw1": 0,
        "xd2": 0,
        "xw2": 0,
        "xd3": 0,
        "xw3": 0,
        "xd4": 0,
        "xw4": 0,
        "xd5": 0,
        "xw5": 0,
        "xd6": 0,
        "xw6": 0,
        "suspend": false,
        "random": false,
        "randon1": false,
        "randoff1": false,
        "randon2": false,
        "randoff2": false,
        "repeat": true,
        "atstart": true,
        "odd": false,
        "even": false,
        "x": 360,
        "y": 480,
        "wires": [
            [],
            [
                "46f2b21fc0a7b159"
            ],
            []
        ]
    },
    {
        "id": "d000d91ce608155d",
        "type": "SSH_Credentials",
        "host": "192.168.1.80",
        "port": "22",
        "userlabel": "pi@192.168.1.80"
    },
    {
        "id": "7540db4618164de1",
        "type": "mqtt-broker",
        "name": "Masked-Broker",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": "",
        "credentials": {
            "user": "alfa",
            "password": "beta"
        }
    }
]

Hmm Don´t look as fancy as the one from UnborN...
Anyhow - Ill paste the screendumps as well

Looks like the SSh node outputs what it should. and the problem is with the MQTT node, but who am I to say..

image

image

So you are getting an error in a function. What code is in the function and what are you feeding into it? (Hint use a debug node)

Also, provide a screenshot of the context values in the context viewer in the sidebar

Are you in the tab where you write to flow? Have you pressed the refresh button to the right of the flow title?

The code is UnborN´s suggestion

msg.payload = msg.payload.split("=")[1]
msg.payload = msg.payload.split("'C")[0]
msg.payload = parseFloat(msg.payload)

return msg;

in a function node

I set a BigSSH node as input to the function node.
The BigSSH makes a SSH call to a remote server commanding "vcgencmd measure_temp".
The output of the BigSSH is the "temp=51.6'C" reply from the remote server, going as input to the function node

@Steve-Mcl - Looks like I spent my "post-tokens" for today - Will not be able to reply anymore since editing a post is also a short comodity in here.

I am in the right tab, and have refreshed the context data menu

@Steve - no worries

As can see, the output from the SSH node is the "temp=53,0`C"

@Steve - You´re the man in gravy!
That did the trick - thank you for such a swift response.
I´ll keep you in a special place in the back of my head, for future assistance :slight_smile:
Take care
BR
Ronnie of Denmark

@Ropper

My apologies. I seem to have gotten your thread mixed with another.

Add a debug node BEFORE the function so we can see what is coming out of the big ssh node. That is the only way we can determine why the function @UnborN provided doesn't work.

Ok so the issue is you are getting a buffer not a string.

As I don't use big ssh I don't know it's capabilities. Does it have an option to convert the output to string?

If not, try...

var payload = msg.payload.toString();
payload = payload.split("=")[1];
msg.payload = parseFloat(payload);
return msg;