How can I split a MQTT string

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"}"

Hi.

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

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


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.

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

Oh dear, sorry to hear that.

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

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://nodered.org/docs/user-guide/messages

Canned Post 2

This post should help to clarify the difference between Javascript objects and JSON.

Canned Post 3

I recommend watching this playlist: Node-RED Essentials. 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.

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.

Hallo,

i'm back from the hospital,

Now i can copy the MQTT message :

{ 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

[
{
"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

@puba you have two problems with your flow

  1. In the first change node you have:
    change-original
    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

  2. your second change node you have:
    change2
    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.]

1 Like

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