Problems with mqtt node (msg.payload interrupt)

Hi, I'm trying to use some conditions with some readings from Arduino Mega, my issue is when I try to use the switch to send open, my condition don't allow me to send the msg, there is any solution to this?
I was thinking about a switch case function, but it will be better to see another options.

[{"id":"edb33bf1.6e7ef8","type":"ui_switch","z":"75f85cad.09ef84","name":"","label":"OPEN/CLOSE","tooltip":"","group":"361cda81.f95386","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"OPEN","onvalueType":"str","onicon":"","oncolor":"","offvalue":"CLOSE","offvalueType":"str","officon":"","offcolor":"","x":540,"y":140,"wires":[["d87e7247.67ab5"]]},{"id":"d87e7247.67ab5","type":"mqtt out","z":"75f85cad.09ef84","name":"","topic":"lab/device1","qos":"","retain":"","broker":"61996a40.9e6e44","x":710,"y":140,"wires":[]},{"id":"1034c79c.c09d78","type":"delay","z":"75f85cad.09ef84","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":330,"y":400,"wires":[["638a82bd.a4cc7c"]]},{"id":"638a82bd.a4cc7c","type":"range","z":"75f85cad.09ef84","minin":"0","maxin":"550","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"","x":480,"y":400,"wires":[["6b3c6b03.8dd224"]]},{"id":"6b3c6b03.8dd224","type":"function","z":"75f85cad.09ef84","name":"","func":"var humidityporcent=msg.payload;\n\nglobal.set('humidity',humidityporcent);\n\nvar humiditycondition = global.get(\"humidity\");\n\n    if (humiditycondition < 20) \n\t    {\n\t\tmsg.payload = \"OPEN\";\n\t\treturn msg;\n\t}\n\telse if (humiditycondition > 60) \n\t {\n\t\tmsg.payload = \"CLOSE\";\n\t\treturn msg;\n\t}","outputs":1,"noerr":0,"x":610,"y":400,"wires":[["487c49c5.c35488"]]},{"id":"a29be998.b916e8","type":"arduino in","z":"75f85cad.09ef84","name":"Mega","pin":"2","state":"ANALOG","arduino":"87b7cbc8.e9ff98","x":190,"y":400,"wires":[["1034c79c.c09d78"]]},{"id":"487c49c5.c35488","type":"mqtt out","z":"75f85cad.09ef84","name":"","topic":"lab/device1","qos":"","retain":"","broker":"61996a40.9e6e44","x":770,"y":400,"wires":[]},{"id":"361cda81.f95386","type":"ui_group","z":"","name":"control valvula","tab":"fdf8ba2e.2694f8","order":1,"disp":true,"width":"6","collapse":false},{"id":"61996a40.9e6e44","type":"mqtt-broker","z":"","name":"","broker":"192.168.43.47","port":"1883","clientid":"Hidro","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"87b7cbc8.e9ff98","type":"arduino-board","z":"","device":"/dev/ttyACM0"},{"id":"fdf8ba2e.2694f8","type":"ui_tab","z":"","name":"Home 1","icon":"dashboard","disabled":false,"hidden":false}]

Could you expand the explaination of your issue? You say:

when I try to use the switch to send open, my condition don't allow me to send the msg

However there is no switch node in your flow. If you are talking about your function node's code, your logic dictates that only input messages that ar between 21 and 59 will cause any output.

The best way to see what is happening in a flow s to adddebug nodes on the output of nodes along the flow. Then you can see what is happening. To debug a function node, add in node.warn() statments like
node.warn("debug1 - humiditycondition="+humiditycondition);
which will display the value of humiditycondition at that point in the flow.

Sin título
For switch I was meaning about that node, and did you mean something like this...

[{"id":"f55f90bc.039bc","type":"ui_switch","z":"d65f5b12.ac1c08","name":"","label":"OPEN/CLOSE","tooltip":"","group":"4e3ad1c4.e639e","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"OPEN","onvalueType":"str","onicon":"","oncolor":"","offvalue":"CLOSE","offvalueType":"str","officon":"","offcolor":"","x":500,"y":220,"wires":[["f45d0de4.b6ddc","b11c448a.9dd468"]]},{"id":"f45d0de4.b6ddc","type":"mqtt out","z":"d65f5b12.ac1c08","name":"","topic":"lab/device1","qos":"","retain":"","broker":"2e5472af.7f3cfe","x":870,"y":80,"wires":[]},{"id":"2c52a45b.7420cc","type":"delay","z":"d65f5b12.ac1c08","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":290,"y":480,"wires":[["72789955.7c4128"]]},{"id":"72789955.7c4128","type":"range","z":"d65f5b12.ac1c08","minin":"0","maxin":"550","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"","x":440,"y":480,"wires":[["9c692584.ea05d8"]]},{"id":"9c692584.ea05d8","type":"function","z":"d65f5b12.ac1c08","name":"","func":"var humidityporcent=msg.payload;\n\nglobal.set('humidity',humidityporcent);\n\nvar humiditycondition = global.get(\"humidity\");\n\n    if (humiditycondition < 20) \n\t    {\n\t\tmsg.payload = \"OPEN\";\n\t\treturn msg;\n\t}\n\telse if (humiditycondition > 60) \n\t {\n\t\tmsg.payload = \"CLOSE\";\n\t\treturn msg;\n\t}","outputs":1,"noerr":0,"x":570,"y":480,"wires":[["81383029.91914","327e1361.0b865c"]]},{"id":"9d83bd0.8a0824","type":"arduino in","z":"d65f5b12.ac1c08","name":"Mega","pin":"2","state":"ANALOG","arduino":"5bf4ba77.d85d84","x":150,"y":480,"wires":[["2c52a45b.7420cc"]]},{"id":"81383029.91914","type":"mqtt out","z":"d65f5b12.ac1c08","name":"","topic":"lab/device1","qos":"","retain":"","broker":"2e5472af.7f3cfe","x":750,"y":580,"wires":[]},{"id":"37487aac.81c626","type":"debug","z":"d65f5b12.ac1c08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":480,"wires":[]},{"id":"6b399a9c.b89154","type":"debug","z":"d65f5b12.ac1c08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1040,"y":240,"wires":[]},{"id":"b11c448a.9dd468","type":"function","z":"d65f5b12.ac1c08","name":"","func":"node.warn(\"debug1 - humiditycondition=\"+humiditycondition);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":880,"y":280,"wires":[["6b399a9c.b89154"]]},{"id":"327e1361.0b865c","type":"function","z":"d65f5b12.ac1c08","name":"","func":"node.warn(\"debug1 - humiditycondition=\"+humiditycondition);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":880,"y":400,"wires":[["37487aac.81c626"]]},{"id":"4e3ad1c4.e639e","type":"ui_group","z":"","name":"control valvula","tab":"f357030d.fc8b2","order":1,"disp":true,"width":"6","collapse":false},{"id":"2e5472af.7f3cfe","type":"mqtt-broker","z":"","name":"","broker":"192.168.43.47","port":"1883","clientid":"Hidro","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5bf4ba77.d85d84","type":"arduino-board","z":"","device":"/dev/ttyACM0"},{"id":"f357030d.fc8b2","type":"ui_tab","z":"","name":"Home 1","icon":"dashboard","disabled":false,"hidden":false}]

Try this, add an MQTT-in node and subscribe to the same topic you are publishing to, connect the mqtt-in node to a debug node and see if the result comes back .

If that doesn't work, replace the switch with anotehr version and see what happens.

I tried but it didn't work, I'm going to explain it again,I need a function that when I use my switch node in the dashboard, it invalidates my function, so that they do not interrupt each other

This is my function

var humidityporcent=msg.payload;

global.set('humidity',humidityporcent);

var humiditycondition = global.get("humidity");

    if (humiditycondition < 20) 
	    {
		msg.payload = "OPEN";
		return msg;
	}
	else if (humiditycondition > 60) 
	 {
		msg.payload = "CLOSE";
		return msg;
	}

so you want a switch to turn on and off the data going through MQTT ?
maybe something like this ? https://flows.nodered.org/flow/a985944e789d90e0cd2008d85b7aaa65
or this ? https://cookbook.nodered.org/basic/route-on-context

The first one is kind what I want but I'm still with problems could you help me?

This is my flow

[{"id":"c96ece6e.83056","type":"ui_switch","z":"4ce899f9.a739a8","name":"","label":"switch","tooltip":"","group":"a433ab48.55c318","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"gate","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":450,"y":620,"wires":[["5611098a.b34ba8"]]},{"id":"5611098a.b34ba8","type":"function","z":"4ce899f9.a739a8","name":"control","func":"if (msg.topic === \"statusoc\") {\n    context.pass = (msg.payload === true) ? true : false;\n    return null;  // exit out early as it's just the control\n}\nif (context.pass) {\n    return msg; // if enabled pass msg\n}\nreturn null; // or drop it\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":700,"wires":[["197fd51a.8ff1bb"]]},{"id":"c3ba2eb9.c793b","type":"inject","z":"4ce899f9.a739a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":524.5,"y":742,"wires":[["5611098a.b34ba8"]]},{"id":"197fd51a.8ff1bb","type":"debug","z":"4ce899f9.a739a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900.5,"y":701,"wires":[]},{"id":"48e83d25.745ce4","type":"ui_switch","z":"4ce899f9.a739a8","name":"","label":"OPEN/CLOSE","tooltip":"","group":"9bfcc815.c4fb78","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"OPEN","onvalueType":"str","onicon":"","oncolor":"","offvalue":"CLOSE","offvalueType":"str","officon":"","offcolor":"","x":120,"y":400,"wires":[["3cbd4160.3ec38e"]]},{"id":"7123b79.550c748","type":"mqtt out","z":"4ce899f9.a739a8","name":"","topic":"lab/device1","qos":"","retain":"","broker":"9d320b7f.df7158","x":530,"y":360,"wires":[]},{"id":"c88ae30f.18fa7","type":"delay","z":"4ce899f9.a739a8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":310,"y":980,"wires":[["db844c28.5e62f"]]},{"id":"db844c28.5e62f","type":"range","z":"4ce899f9.a739a8","minin":"0","maxin":"550","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"","x":460,"y":980,"wires":[["f069bbc6.ddd0e8"]]},{"id":"f069bbc6.ddd0e8","type":"function","z":"4ce899f9.a739a8","name":"","func":"var humidityporcent=msg.payload;\n\nglobal.set('humidity',humidityporcent);\n\nvar humiditycondition = global.get(\"humidity\");\n\n    if (humiditycondition < 20) \n\t    {\n\t\tmsg.payload = \"OPEN\";\n\t\treturn msg;\n\t}\n\telse if (humiditycondition > 60) \n\t {\n\t\tmsg.payload = \"CLOSE\";\n\t\treturn msg;\n\t}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":980,"wires":[["ff947ae2.915578"]]},{"id":"7eb5f2ab.316b1c","type":"arduino in","z":"4ce899f9.a739a8","name":"Mega","pin":"2","state":"ANALOG","arduino":"2962a660.2f6a3a","x":170,"y":980,"wires":[["c88ae30f.18fa7"]]},{"id":"ff947ae2.915578","type":"mqtt out","z":"4ce899f9.a739a8","name":"","topic":"lab/device1","qos":"","retain":"","broker":"9d320b7f.df7158","x":870,"y":980,"wires":[]},{"id":"3cbd4160.3ec38e","type":"function","z":"4ce899f9.a739a8","name":"","func":"var status = msg.payload;\n\nglobal.set('statusoc',status);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":400,"wires":[["9f39c5d.38c8a38","7123b79.550c748","92c14aa0.02a698"]]},{"id":"9f39c5d.38c8a38","type":"debug","z":"4ce899f9.a739a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":420,"wires":[]},{"id":"92c14aa0.02a698","type":"function","z":"4ce899f9.a739a8","name":"","func":"var statuscondition = global.get(\"statusoc\");\n\n    if (statuscondition == \"OPEN\") \n\t    {\n\t\tmsg.payload = true;\n\t\treturn msg;\n\t}\n\telse if (statuscondition == \"CLOSE\") \n\t {\n\t\tmsg.payload = false;\n\t\treturn msg;\n\t}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":480,"wires":[["5611098a.b34ba8","c74ab78a.dd93e8"]]},{"id":"c74ab78a.dd93e8","type":"debug","z":"4ce899f9.a739a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":480,"wires":[]},{"id":"a433ab48.55c318","type":"ui_group","z":"","name":"Default","tab":"5a7b2006.42f79","order":1,"disp":true,"width":"6"},{"id":"9bfcc815.c4fb78","type":"ui_group","z":"","name":"control valvula","tab":"f3d4548f.1572b8","order":1,"disp":true,"width":"6","collapse":false},{"id":"9d320b7f.df7158","type":"mqtt-broker","z":"","name":"","broker":"192.168.43.47","port":"1883","clientid":"Hidro","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"2962a660.2f6a3a","type":"arduino-board","z":"","device":"/dev/ttyACM0"},{"id":"5a7b2006.42f79","type":"ui_tab","z":"","name":"Home","icon":"dashboard"},{"id":"f3d4548f.1572b8","type":"ui_tab","z":"","name":"Home 1","icon":"dashboard","disabled":false,"hidden":false}]

My problem is with this function

var statuscondition = global.get("statusoc");

    if (statuscondition == "OPEN") 
	    {
		msg.payload = true;
		return msg;
	}
	else if (statuscondition == "CLOSE") 
	 {
		msg.payload = false;
		return msg;
	}

related with this another function

if (msg.topic === "gate") {
    context.pass = (msg.payload === true) ? true : false;
    return null;  // exit out early as it's just the control
}
if (context.pass) {
    return msg; // if enabled pass msg
}
return null; // or drop it


so I know that my problem is the mg.topic == "gate" how can I do to make my first funciton to msg.topic == "gate"

Where do you set msg.topic to 'gate'?

Sin título
from a switch dashboard node but I want to replace for a function

So let me see if I understand, you say

so are you saying you want to replace the ui-switch bode with a function node? If so, then what will feed the function node?

If not then what do you mean?

yeah that's right I will feed it with this function ...

var statuscondition = global.get("statusoc");

    if (statuscondition == "OPEN") 
	    {
		msg.payload = true;
		return msg;
	}
	else if (statuscondition == "CLOSE") 
	 {
		msg.payload = false;
		return msg;
	}

Well then you have teo set the msg.topic in that function.

I tried with this but it did't work

var statuscondition = global.get("statusoc");

    if (statuscondition == "OPEN") 
	    {
		msg.topic = true;
		return msg;
	}
	else if (statuscondition == "CLOSE") 
	 {
		msg.topic = false;
		return msg;
	}

So now you set msg.topic true or false. But up above you test ==="gate" so you need to sort out one or other idea.

Like this?

if (msg.topic) {
    context.pass = (msg.payload === true) ? true : false;
    return null;  // exit out early as it's just the control
}
if (context.pass) {
    return msg; // if enabled pass msg
}
return null; // or drop it

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