Multi MQTT and function

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

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

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

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

1 Like

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

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

Thank you

[{"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":[]}]

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

1 Like

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.

[{"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":[]}]
1 Like

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

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