# Get string from serial port and convert to separate key

**URL:** <https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858>\
**Category:** General\
**Created:** [23 February 2022 05:23 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858 "2022-02-23T05:23:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinesh920](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@dinesh920](https://discourse.nodered.org/u/dinesh920)\
**Post date:** [23 February 2022 05:23 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/1 "2022-02-23T05:23:28Z")

</div>

I am getting "Humidity: 64.40%, Temperature: 32.00 Celsius" which is in string from my serial port. I wish to change it to key value data. This is so that I can do gauge for humidity and temperature separately. Do advice. Do I have to use function node or some other node. I am not familiar with function node.

 ![Node Temp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c00416f382b4d77e8a627cb2236120c5568d4d1.jpeg)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 February 2022 06:48 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/2 "2022-02-23T06:48:04Z")

</div>

This should work in a function node:

```auto
const input = msg.payload.split(",")
msg.payload = {}
msg.payload.humidity = parseFloat(input[0].split(":")[1])
msg.payload.temperature = parseFloat(input[1].split(":")[1])

return msg

```

output:

```auto
{"payload":{"humidity":64.4,"temperature":32}}

```

In the gauge node you can now use `{{payload.humidity}}`  
The gauge node requires a number input, so the % and Celsius was removed.

---

<div class="post-metadata">

**Author:** ![dinesh920](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@dinesh920](https://discourse.nodered.org/u/dinesh920)\
**Post date:** [23 February 2022 07:21 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/3 "2022-02-23T07:21:38Z")

</div>

Thank you sir. It really works. Can I know what does the second and third line means? what is msg.payload = {}? I understand the first third line but how do you know the input is 0 or 1 then you split into [1]. I do not understand this particular part. If I can understand, I can try to do on my own in near future for other device also.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 February 2022 07:39 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/4 "2022-02-23T07:39:22Z")

</div>

The input is "Humidity: 64.40%, Temperature: 32.00 Celsius"

`const input = msg.payload.split(",")` will create an array of 2 elements, each with their own string:

0: "Humidity: 64.40%"  
1: "Temperature: 32.00 Celsius"

These strings can in turn be split by the ":" symbol

So `input[0]` = the humidity string, split the string by ":" and you get another array:

0:"Humidity"  
1: "64.40%"

We create a new `msg.payload = {}` object  
We assign a new property "humidity": `msg.payload.humidity` and assign the value:  
`input[0].split(":")[1]` = "64.40%"

I hope this somewhat clarifies.

---

<div class="post-metadata">

**Author:** ![dinesh920](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@dinesh920](https://discourse.nodered.org/u/dinesh920)\
**Post date:** [23 February 2022 08:38 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/5 "2022-02-23T08:38:43Z")

</div>

Thank you sir. I understand now. If I wish to insert chart node, must I use switch node after the function to separate the humidity data and temperature data? Because like Gauge node, we can just use payload.temperature but in chart node, I do not think we can right? Btw thanks for guiding me for the previous question.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2022 09:33 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/6 "2022-02-23T09:33:22Z")

</div>

> [@dinesh920](#):
>
> must I use switch node after the function to separate the humidity data and temperature data

A Change node, not a Switch node. You are correct in that the Chart must have the value in the payload and can have a line label in the topic. This is so that multiple lines can be plotted on one chart.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 March 2022 09:33 UTC](https://discourse.nodered.org/t/get-string-from-serial-port-and-convert-to-separate-key/58858/7 "2022-03-09T09:33:33Z")

</div>

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