# Multi MQTT and function

**URL:** https://discourse.nodered.org/t/multi-mqtt-and-function/43537
**Category:** General
**Created:** [2 April 2021 08:11 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537 "2021-04-02T08:11:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![yingkhun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yingkhun/32/39651_2.png) [@yingkhun](https://discourse.nodered.org/u/yingkhun)
#### Post date: [2 April 2021 08:11 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/1 "2021-04-02T08:11:23Z")

</div>

Hi I just started learning NodeRED  
I have some question about Multi MQTT input to function

 ![Screen Shot 2564-04-02 at 15.01.58](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51192e7579a4a9a9deb3650f55abf64ebf0dbcb5.jpeg)

I have 5 MQTT subscribe topic

1. value (update every 5 sec)
2. stage (retained messages)
3. auto (retained messages)
4. max (retained messages)
5. min (retained messages)

my function

```auto
var relay_stage = String(msg.relay_stage);
var auto_mode = String(msg.auto_mode);
var soil_val = Number(msg.soil_val);
var soil_max = Number(msg.soil_max);
var soil_min = Number(msg.soil_min);

if (auto_mode == "on"){
    if (soil_val >= soil_max && relay_stage == "on"){
        msg.payload = "off";
        return msg;
    }
    else if (soil_val <= soil_min && relay_stage == "off"){
        msg.payload = "on";
        return msg;
    }
}

```

but it's not work please help

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [2 April 2021 08:21 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/2 "2021-04-02T08:21:17Z")

</div>

You will need to join the messages using a join node, then feed them into the function, as messages arrive at different times.  
Read this page to see an example of joining messages.  
[https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

### Author: ![yingkhun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yingkhun/32/39651_2.png) [@yingkhun](https://discourse.nodered.org/u/yingkhun)
#### Post date: [2 April 2021 09:41 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/3 "2021-04-02T09:41:11Z")

</div>

Now I have Object like

name/sensor/soil1/value : msg.payload : Object  
{ name/sensor/soil1/min: "23", name/power/relay1/auto: "on", name/power/relay1/stage: "off", name/sensor/soil1/max: "54", name/sensor/soil1/value: "0.0" }

Can you guide some example function code for use this Object?  
Sorry for noob question I'm really new here  
Thank you

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [2 April 2021 09:43 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/4 "2021-04-02T09:43:14Z")

</div>

Post your flow and i will edit it to give topics better names.  
when posting use bacticks around code, make sure backticks are on new line on their own.  
eg  
```  
code  
```

---

<div class="post-metadata">

### Author: ![yingkhun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yingkhun/32/39651_2.png) [@yingkhun](https://discourse.nodered.org/u/yingkhun)
#### Post date: [2 April 2021 09:47 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/5 "2021-04-02T09:47:27Z")

</div>

Thank you

```auto
[{"id":"882513f.6bf8af","type":"mqtt in","z":"8b1bcb5e.22a118","name":"","topic":"name/sensor/soil1/value","qos":"2","datatype":"auto","broker":"","x":410,"y":300,"wires":[["350850d1.a621"]]},{"id":"1f28993a.435c87","type":"mqtt in","z":"8b1bcb5e.22a118","name":"","topic":"name/sensor/soil1/min","qos":"2","datatype":"auto","broker":"","x":400,"y":420,"wires":[["350850d1.a621"]]},{"id":"52075a8c.092b54","type":"mqtt in","z":"8b1bcb5e.22a118","name":"","topic":"name/power/relay1/auto","qos":"2","datatype":"auto","broker":"","x":410,"y":240,"wires":[["350850d1.a621"]]},{"id":"60f23533.0a8bcc","type":"mqtt in","z":"8b1bcb5e.22a118","name":"","topic":"name/power/relay1/stage","qos":"2","datatype":"auto","broker":"","x":410,"y":180,"wires":[["350850d1.a621"]]},{"id":"3d97dff0.d1077","type":"mqtt in","z":"8b1bcb5e.22a118","name":"","topic":"name/sensor/soil1/max","qos":"2","datatype":"auto","broker":"","x":400,"y":360,"wires":[["350850d1.a621"]]},{"id":"350850d1.a621","type":"join","z":"8b1bcb5e.22a118","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":670,"y":300,"wires":[["7e7f42ee.4313cc"]]},{"id":"7e7f42ee.4313cc","type":"debug","z":"8b1bcb5e.22a118","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":300,"wires":[]}]

```

---

<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: [2 April 2021 09:47 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/6 "2021-04-02T09:47:46Z")

</div>

> [@yingkhun](#):
>
> Can you guide some example function code for use this Object?

Do what you did before but the path to the variables have changed.

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)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [2 April 2021 09:57 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/7 "2021-04-02T09:57:08Z")

</div>

Try this, I have split the topic and used the last value to create the payload, then in your function i have referenced these payload propeties  
eg.

```auto
[{"id":"729cda57.87488c","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$split(topic, \"/\")[-1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":3940,"wires":[["350850d1.a621"]]},{"id":"882513f.6bf8af","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/sensor/soil1/value","qos":"2","datatype":"auto","broker":"","x":130,"y":3940,"wires":[["729cda57.87488c"]]},{"id":"1f28993a.435c87","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/sensor/soil1/min","qos":"2","datatype":"auto","broker":"","x":120,"y":4060,"wires":[["729cda57.87488c"]]},{"id":"60f23533.0a8bcc","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/power/relay1/stage","qos":"2","datatype":"auto","broker":"","x":130,"y":3820,"wires":[["729cda57.87488c"]]},{"id":"3d97dff0.d1077","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/sensor/soil1/max","qos":"2","datatype":"auto","broker":"","x":120,"y":4000,"wires":[["729cda57.87488c"]]},{"id":"52075a8c.092b54","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/power/relay1/auto","qos":"2","datatype":"auto","broker":"","x":130,"y":3880,"wires":[["729cda57.87488c"]]},{"id":"350850d1.a621","type":"join","z":"5a245aa1.510164","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":610,"y":3940,"wires":[["15cf54a0.38e2e3","7e7f42ee.4313cc"]]},{"id":"15cf54a0.38e2e3","type":"function","z":"5a245aa1.510164","name":"","func":"var relay_stage = String(msg.payload.stage);\nvar auto_mode = String(msg.payload.auto);\nvar soil_val = Number(msg.payload.value);\nvar soil_max = Number(msg.payload.max);\nvar soil_min = Number(msg.payload.min);\n\nif (auto_mode == \"on\"){\n if (soil_val >= soil_max && relay_stage == \"on\"){\n msg.payload = \"off\";\n return msg;\n }\n else if (soil_val <= soil_min && relay_stage == \"off\"){\n msg.payload = \"on\";\n return msg;\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":770,"y":3940,"wires":[["7e7f42ee.4313cc"]]},{"id":"7e7f42ee.4313cc","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":4100,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![yingkhun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yingkhun/32/39651_2.png) [@yingkhun](https://discourse.nodered.org/u/yingkhun)
#### Post date: [2 April 2021 10:05 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/8 "2021-04-02T10:05:46Z")

</div>

> [@E1cid](#):
>
> ```auto
> [{"id":"729cda57.87488c","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$split(topic, \"/\")[-1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":3940,"wires":[["350850d1.a621"]]},{"id":"882513f.6bf8af","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/sensor/soil1/value","qos":"2","datatype":"auto","broker":"","x":130,"y":3940,"wires":[["729cda57.87488c"]]},{"id":"1f28993a.435c87","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/sensor/soil1/min","qos":"2","datatype":"auto","broker":"","x":120,"y":4060,"wires":[["729cda57.87488c"]]},{"id":"60f23533.0a8bcc","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/power/relay1/stage","qos":"2","datatype":"auto","broker":"","x":130,"y":3820,"wires":[["729cda57.87488c"]]},{"id":"3d97dff0.d1077","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/sensor/soil1/max","qos":"2","datatype":"auto","broker":"","x":120,"y":4000,"wires":[["729cda57.87488c"]]},{"id":"52075a8c.092b54","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"name/power/relay1/auto","qos":"2","datatype":"auto","broker":"","x":130,"y":3880,"wires":[["729cda57.87488c"]]},{"id":"350850d1.a621","type":"join","z":"5a245aa1.510164","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":610,"y":3940,"wires":[["15cf54a0.38e2e3","7e7f42ee.4313cc"]]},{"id":"15cf54a0.38e2e3","type":"function","z":"5a245aa1.510164","name":"","func":"var relay_stage = String(msg.payload.stage);\nvar auto_mode = String(msg.payload.auto);\nvar soil_val = Number(msg.payload.value);\nvar soil_max = Number(msg.payload.max);\nvar soil_min = Number(msg.payload.min);\n\nif (auto_mode == \"on\"){\n if (soil_val >= soil_max && relay_stage == \"on\"){\n msg.payload = \"off\";\n return msg;\n }\n else if (soil_val <= soil_min && relay_stage == \"off\"){\n msg.payload = \"on\";\n return msg;\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":770,"y":3940,"wires":[["7e7f42ee.4313cc"]]},{"id":"7e7f42ee.4313cc","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":4100,"wires":[]}]
> 
> ```

woww its worked perfectly,  
Thank you, you saved my day

---

<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: [16 April 2021 10:06 UTC](https://discourse.nodered.org/t/multi-mqtt-and-function/43537/9 "2021-04-16T10:06:27Z")

</div>

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