Comparing payloads of MQTT-strings

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.

msg.topic = ‘abesch’

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

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.

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

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

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

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

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.

Follow the instructions I posted earlier about "copy path"

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

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

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)

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

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

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

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

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

@Werner when you click (not hover) "copy path" it will copy the path in the appropriate square bracket notation payload["innen/WZ_Luftmonitor/temperature"] automatically for you to paste into your code.

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

{"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'.

Here is a more explicit demo

chrome_6imxhV1OvH

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 :person_shrugging:

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!

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

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

OK, understood.