# RegEx in function-node

**URL:** <https://discourse.nodered.org/t/regex-in-function-node/58763>\
**Category:** General\
**Tags:** function-node\
**Created:** [21 February 2022 19:06 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763 "2022-02-21T19:06:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 19:06 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/1 "2022-02-21T19:06:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [21 February 2022 19:15 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/2 "2022-02-21T19:15:35Z")

</div>

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**

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 19:18 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/3 "2022-02-21T19:18:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 20:37 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/4 "2022-02-21T20:37:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 20:38 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/5 "2022-02-21T20:38:25Z")

</div>

Where to put that argument?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 20:40 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/6 "2022-02-21T20:40:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [21 February 2022 20:44 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/7 "2022-02-21T20:44:03Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 20:46 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/8 "2022-02-21T20:46:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 20:53 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/9 "2022-02-21T20:53:35Z")

</div>

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

How do I paste in an exampleflow like UnborN did?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 21:04 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/10 "2022-02-21T21:04:50Z")

</div>

> [@Ropper](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 22:12 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/11 "2022-02-21T22:12:56Z")

</div>

```auto
[
    {
        "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"
        }
    }
]

```

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 22:14 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/12 "2022-02-21T22:14:28Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 22:17 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/13 "2022-02-21T22:17:12Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67e2bcb94cb1202e14d94a5c0d957498d7d0dd24.png)

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 22:18 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/14 "2022-02-21T22:18:37Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aaf4155718c11a15fe0c6a32045726d41da4e2a6.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 22:19 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/15 "2022-02-21T22:19:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 22:21 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/16 "2022-02-21T22:21:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 22:29 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/17 "2022-02-21T22:29:50Z")

</div>

The code is UnborN´s suggestion

```auto
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.

---

<div class="post-metadata">

**Author:** ![Ropper](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ropper](https://discourse.nodered.org/u/Ropper)\
**Post date:** [21 February 2022 22:31 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/18 "2022-02-21T22:31:51Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42988b9bd5cc8a717e1f69f85afd6881e45ff468.png)

@Steve - no worries

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/61075bad596e413d31c6fa3efc70e32a66231306.png)

@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 🙂  
Take care  
BR  
Ronnie of Denmark

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 22:33 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/19 "2022-02-21T22:33:48Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2022 22:57 UTC](https://discourse.nodered.org/t/regex-in-function-node/58763/20 "2022-02-21T22:57:02Z")

</div>

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...

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

```

[Next page](https://discourse.nodered.org/t/regex-in-function-node/58763.md?page=2)
