# How can I split a MQTT string

**URL:** https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258
**Category:** Developing Nodes
**Created:** [26 January 2022 13:40 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258 "2022-01-26T13:40:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![puba](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@puba](https://discourse.nodered.org/u/puba)
#### Post date: [26 January 2022 13:40 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/1 "2022-01-26T13:40:23Z")

</div>

How can I splitt a string, that I recive from a MQTT sendor.  
I need the the sensor name and the temperature.

"(  
"Time":"2022-01-25T20:02:42","DS18B20-1":{"Id":"03168C0093FF","Temperature":47.7},"DS18B20-2":{"Id":"03168C212DFF","Temperature":46.6},"TempUnit":"C"}"

---

<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: [26 January 2022 13:45 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/2 "2022-01-26T13:45:35Z")

</div>

Hi.

First, could you please format your post correctly - it is important to surround your code with three backticks

````auto
```
like this
```

````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

* * *

secondly, when copying a value from debug, please use the "copy value2 button that appears under your mouse when you hover over the message in the debug window

* * *

Lastly, can you post a screen snip of the debug output so we can see how it looks.

* * *

PS: Welcome to the forum.

---

<div class="post-metadata">

### Author: ![puba](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@puba](https://discourse.nodered.org/u/puba)
#### Post date: [26 January 2022 15:50 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/3 "2022-01-26T15:50:22Z")

</div>

Hi  
Thank you for you answer.  
That is all corect.  
But i must post it with my mobile.  
Thatsway is not so easy to do this.  
So I must wayt some days, than i can write it new  
with a pc.  
Best wishes from a hispital in germany.

puba

---

<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: [26 January 2022 15:56 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/4 "2022-01-26T15:56:06Z")

</div>

> [@puba](#):
>
> Best wishes from a hispital in germany.

Oh dear, sorry to hear that.

In the meantime, some light reading & sort videos might help you find the answer yourself 🙂

### Canned Post 1

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

> **[Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)**

### Canned Post 2

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify the difference between Javascript objects and JSON.

### Canned Post 3

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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: [26 January 2022 22:02 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/5 "2022-01-26T22:02:27Z")

</div>

> [@puba](#):
>
> How can I splitt a string, that I recive from a MQTT sendor

In the MQTT node select Output Parsed JSON object. Then it will parse the JSON string that you showed and convert it to a javascript object, which will make life much easier.

---

<div class="post-metadata">

### Author: ![puba](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@puba](https://discourse.nodered.org/u/puba)
#### Post date: [2 February 2022 19:01 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/6 "2022-02-02T19:01:36Z")

</div>

Hallo,

i'm back from the hospital,

Now i can copy the MQTT message :

```auto
{ Time: "2022-02-02T19:39:51", DS18B20-1: object, DS18B20-2: object, TempUnit: "C" }

```

an split:

...............

object

Time: "2022-02-02T19:39:51"  
DS18B20-1: object  
Id: "03168C0093FF"  
Temperature: 47.8

DS18B20-2: object  
Id: "03168C212DFF"  
Temperature: 46.7

TempUnit: "C"

..................

I like to extract the temperature from DS188B20-1 an from DB18B20-2 plus the time oft the  
measurement.

Attached is my previous attempt

```auto
[
{
"id": "08f3f1f3f456ef79",
"type": "tab",
"label": "Flow 3",
"disabled": false,
"info": "",
"env": []
},
{
"id": "c9e78fda7c373df6",
"type": "mqtt in",
"z": "08f3f1f3f456ef79",
"name": "",
"topic": "Heizung/Temperatur/SENSOR",
"qos": "1",
"datatype": "json",
"broker": "91280bb83b9e18be",
"nl": false,
"rap": false,
"rh": 0,
"inputs": 0,
"x": 220,
"y": 120,
"wires": [
[
"869f8c430801be9b",
"71678c95db116ff8"
]
]
},
{
"id": "e1216c6e0aa2ee2f",
"type": "json",
"z": "08f3f1f3f456ef79",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 170,
"y": 400,
"wires": [
[
"6198253746a49072",
"12e0d1652cda9fc2"
]
]
},
{
"id": "6198253746a49072",
"type": "debug",
"z": "08f3f1f3f456ef79",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 330,
"y": 340,
"wires": []
},
{
"id": "869f8c430801be9b",
"type": "debug",
"z": "08f3f1f3f456ef79",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 690,
"y": 120,
"wires": []
},
{
"id": "12e0d1652cda9fc2",
"type": "change",
"z": "08f3f1f3f456ef79",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.temperature",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 440,
"y": 420,
"wires": [
[
"eccfd14eb2550e6e"
]
]
},
{
"id": "eccfd14eb2550e6e",
"type": "debug",
"z": "08f3f1f3f456ef79",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 670,
"y": 420,
"wires": []
},
{
"id": "71678c95db116ff8",
"type": "change",
"z": "08f3f1f3f456ef79",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "msg.payload.DS18B20-1",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 210,
"y": 240,
"wires": [
[
"e1216c6e0aa2ee2f",
"d429e5954526e3c9"
]
]
},
{
"id": "d429e5954526e3c9",
"type": "debug",
"z": "08f3f1f3f456ef79",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 590,
"y": 240,
"wires": []
},
{
"id": "91280bb83b9e18be",
"type": "mqtt-broker",
"name": "docker:11883",
"broker": "172.17.0.1",
"port": "11883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "5",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
}
]

```

Can you help me to eliminate the correct values?

Best Regards

Armin

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [3 February 2022 11:53 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/7 "2022-02-03T11:53:47Z")

</div>

@puba you have two problems with your flow

1. In the first change node you have:  
 ![change-original](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f15eff1d7cecaeb143972d0d90dba5857f40410.jpeg)  
notice you are setting msg payload to the string `msg.payload.DS18B20-1`. You need to change it to use the value like this:  
 ![Change-new](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/61f399dd56c90b240615681fb650015e166eb162.jpeg)

2. your second change node you have:  
 ![change2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b4c5cee1f31a53307aefcf1a64028403de135302.png)  
but the incoming msg.payload has already been converted to a string so that won't work.

[NOTE: you posted on the wrong topic so I moved it to your original topic.]

---

<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: [27 March 2022 22:03 UTC](https://discourse.nodered.org/t/how-can-i-split-a-mqtt-string/57258/8 "2022-03-27T22:03:18Z")

</div>

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