# Comparing payloads of MQTT-strings

**URL:** https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538
**Category:** General
**Created:** [13 August 2023 17:27 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538 "2023-08-13T17:27:15Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [13 August 2023 17:27 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/1 "2023-08-13T17:27:15Z")

</div>

I know how to change topics with the change-node, and how to compare their payloads afterwards. But I want to use the function-node for this. I have this topic: "home/auto/beschattung"  
and want to replace it with "abesch" and keep the payload as it is.  
Sounds very simple, but I cannot find a solution.

---

<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: [13 August 2023 18:08 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/2 "2023-08-13T18:08:29Z")

</div>

msg.topic = ‘abesch’

---

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [14 August 2023 09:46 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/3 "2023-08-14T09:46:53Z")

</div>

Thank you. But this is not working when you have more than one flow. For example:

```auto
msg.topic = "power";
node.status({fill:"red",shape:"dot",text:"Leistung:" + msg.payload.power});
return msg;

```

gives an 'undefined' in the node status (when coding just 'msg.payload', the node status is OK). But I will calculate and display different payloads.

---

<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: [14 August 2023 10:26 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/4 "2023-08-14T10:26:28Z")

</div>

Without seeing the rest of the code I can’t comment on what you are doing.

---

<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: [14 August 2023 10:39 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/5 "2023-08-14T10:39:31Z")

</div>

Reading between the lines, it sounds like you aim is to get multiple values from different sources into 1 message so that you can compare values.

On that assumption, consider this:

- MQTT in 1 - `home/auto/beschattung/power`
- MQTT in 2 - `home/auto/abesch/power`

Pass these to a join node, set for 2 elements & then you will get both values in one message. Then you will see both values from the 2 sources in one msg.payload.xxx.

  

Here are some canned texts to help you:

### Joining messages

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

### Getting data from the msg

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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)

---

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [14 August 2023 10:57 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/6 "2023-08-14T10:57:40Z")

</div>

Understand. For example, this is the flow for my blinds:

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

active is depending on auto on/off, temperature and brightness. Here is the working function:

```auto
if (msg.payload.Sonne > '47000' && msg.payload.Twz >= 21 && msg.payload.ABesch === "on"){
    node.status({fill:"red",shape:"dot",text:"Sonne:" + msg.payload.Sonne +" Twz:" + msg.payload.Twz +" AUTO:" + msg.payload.ABesch});
    msg.payload = 100;
    
}else{
    node.status({fill:"green",shape:"ring",text:"Sonne:" + msg.payload.Sonne +" Twz:" + msg.payload.Twz + " AUTO:" + msg.payload.ABesch});
    msg.payload = 0;
}
msg.topic = "Sbes";
return msg;

```

I want to get rid of all change-nodes to convert the long MQTT-string before computing into the AND-gate.

---

<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: [14 August 2023 11:07 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/7 "2023-08-14T11:07:50Z")

</div>

> [@Werner](#):
>
> I want to get rid of all change-nodes

Follow the instructions I posted earlier about "copy path"

---

<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: [14 August 2023 11:14 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/8 "2023-08-14T11:14:43Z")

</div>

> [@Werner](#):
>
> Here is the working function:
> 
> ```auto
> if (msg.payload.Sonne > '47000'
> 
> ```

not related to your question, however here you are comparing a value against a string  
eg.  
`msg.payload.Sonne > 47000`  
or if it arrives as a string  
`parseInt(msg.payload.Sonne) > 47000`

---

<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: [14 August 2023 11:31 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/9 "2023-08-14T11:31:41Z")

</div>

the old phrase 'a picture is worth a thousand words' does not apply when you need to see the actual code.

> [@Werner](#):
>
> I want to get rid of all change-nodes to convert the long MQTT-string

What do you have the `mqtt-in` node's `output` option set to? if it is a JSON object in teh form of a string and you want to convert it to an object you should set output to `auto-detect (parsed JSON object, string or buffer)`

---

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [14 August 2023 12:14 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/10 "2023-08-14T12:14:04Z")

</div>

@steve-McI: when I hover to copy path, the content is just 'topic' - nothing else. Maybe it is easier to explain it like this: a string like "msg.payload.innen/WZ\_Luftmonitor/temperature" leads to an error message (temperature is not defined). Anything without "/" works.

---

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [14 August 2023 12:15 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/11 "2023-08-14T12:15:51Z")

</div>

@zenof: I had chosen auto-detect (string or buffer).

---

<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: [14 August 2023 12:34 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/12 "2023-08-14T12:34:27Z")

</div>

Please connect a debug node(set to display the complete msg object) to the output of an `mqtt-in` node and show us what it looks like. (copy and paste it to a reply)

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

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)

1. Why not use `auto-detect (parsed JSON object, string or buffer)`?
2. what version of NR and node.js are you running? (see NR startup log) \

---

<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: [14 August 2023 13:33 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/13 "2023-08-14T13:33:59Z")

</div>

> [@Werner](#):
>
> when I hover to copy path, the content is just 'topic' - nothing else. Maybe it is easier to explain it like this: a string like "msg.payload.innen/WZ\_Luftmonitor/temperature" leads to an error message (temperature is not defined). Anything without "/" works.

@Werner when you **click** (not hover) "copy path" it will copy the path in the appropriate [square bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors) `payload["innen/WZ_Luftmonitor/temperature"]` automatically for you to paste into your code.

---

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [14 August 2023 13:34 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/14 "2023-08-14T13:34:20Z")

</div>

Thanks for your patience. Here we are:  
Node Red v 2.2.2  
Node.js: v16.16.0

```auto
{"topic":"innen/WZ_Luftmonitor","payload":25.65,"qos":0,"retain":false,"battery":0,"humidity":63.11,"linkquality":126,"temperature":25.65,"voc":85,"voltage":2800,"_msgid":"e7cec83bf577f853"}

```

But I used the node 'MQTT json' with property 'temperature'.

---

<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: [14 August 2023 13:40 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/15 "2023-08-14T13:40:30Z")

</div>

Here is a more explicit demo

![chrome_6imxhV1OvH](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1cb6fbdffdb74a2bae3b200877f7819a5ab84396.gif)

---

<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: [14 August 2023 13:41 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/16 "2023-08-14T13:41:38Z")

</div>

> [@Werner](#):
>
> Node Red v 2.2.2

You are missing out on full developer like env with Monaco editor (the code editor in the function node) and many other new nice-to-haves in V3.x 🤷

---

<div class="post-metadata">

### Author: ![Werner](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Werner](https://discourse.nodered.org/u/Werner)
#### Post date: [14 August 2023 13:55 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/17 "2023-08-14T13:55:25Z")

</div>

great! I have to learn so much. I didn't know the usage of square bracket notation. So I have to use variables to combine the payloads. Sure I will update to v3 first. Need some time, I will report. Thanks to all!

---

<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: [14 August 2023 13:57 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/18 "2023-08-14T13:57:13Z")

</div>

Looking at the message posted, is it not just msg.payload that is required?

---

<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: [14 August 2023 14:00 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/19 "2023-08-14T14:00:02Z")

</div>

@colin  
Yes, in that single msg posted 4 up, but the overall theme is to join messages for later comparison (so I read it)

> [@Werner](#):
>
> For example, this is the flow for my blinds:
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/834bbea840d74396c1dcf87821c5c1f9a9b3e875.png)

---

<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: [14 August 2023 14:26 UTC](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538/20 "2023-08-14T14:26:18Z")

</div>

OK, understood.

[Next page](https://discourse.nodered.org/t/comparing-payloads-of-mqtt-strings/80538.md?page=2)
