Forward message based on a dropdown menu status

Basically I want a co2 value that comes from an mqtt message and converted into a number and then set a pin true depending on the value of the co2 value. But this should only happen if a dropdown menu is set to true. I tried this with a logical and node, but never got it right. Unfortunately, the function nodes cannot process 2 inputs. Does anyone perhaps have an idea how I could implement this?

See this article in the cookbook for an example of how to join messages into one object.

Once you have both values in one msg, you can do logic checks.


Alternatively, store both values in context, then you can retrieve both values from context and do your logic tests.

1 Like

Hello and welcome to the Forum.

Various ways to implement this. To get you started, here's a simple example using flow-context.


The 'logic' this flow implements is... you need an input greater than 10 AND dropdown must be 'true'.

[{"id":"87a6eab4f305fa45","type":"tab","label":"Forward_message","disabled":false,"info":"","env":[]},{"id":"4647dc7fd8b4052a","type":"change","z":"87a6eab4f305fa45","name":"","rules":[{"t":"set","p":"dropdown","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":120,"wires":[[]]},{"id":"b0742a532562b986","type":"inject","z":"87a6eab4f305fa45","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"num","x":170,"y":260,"wires":[["29adf4552a70cfde"]]},{"id":"62a5f0d60d72a2e5","type":"debug","z":"87a6eab4f305fa45","name":"Check output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":300,"wires":[]},{"id":"285f6c971c616361","type":"inject","z":"87a6eab4f305fa45","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":170,"y":140,"wires":[["4647dc7fd8b4052a"]]},{"id":"638a252deb52d314","type":"inject","z":"87a6eab4f305fa45","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":170,"y":100,"wires":[["4647dc7fd8b4052a"]]},{"id":"cceab04efcd4c834","type":"comment","z":"87a6eab4f305fa45","name":"This simulates an input from your MQTT-In node","info":"","x":280,"y":220,"wires":[]},{"id":"e583ca42228eb519","type":"comment","z":"87a6eab4f305fa45","name":"Simulate your dropdown ","info":"","x":210,"y":60,"wires":[]},{"id":"29adf4552a70cfde","type":"function","z":"87a6eab4f305fa45","name":"check values and states","func":"let dropdown = flow.get(\"dropdown\") || false;\nlet threshold = 10;\n\nif (msg.payload > threshold && dropdown === true) {\n    msg.payload = true;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Turn Pin ON\"});\n    return msg;\n}\nelse {\n    msg.payload = false;\n    node.status({fill:\"green\",shape:\"dot\",text:\"Turn Pin OFF\"});\n    return msg;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":300,"wires":[["62a5f0d60d72a2e5","3a626d0bafc76940"]]},{"id":"ccb70d851742d6c7","type":"inject","z":"87a6eab4f305fa45","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":170,"y":340,"wires":[["29adf4552a70cfde"]]},{"id":"b9f006b3634576ea","type":"comment","z":"87a6eab4f305fa45","name":"Threshold is set to 10 inside function node","info":"","x":420,"y":360,"wires":[]},{"id":"3a626d0bafc76940","type":"rpi-gpio out","z":"87a6eab4f305fa45","name":"","pin":"24","set":"","level":"0","freq":"","out":"out","bcm":true,"x":580,"y":240,"wires":[]},{"id":"3346a85ea7b60b37","type":"comment","z":"87a6eab4f305fa45","name":"Change PIN number to suit","info":"","x":630,"y":200,"wires":[]}]
1 Like

thank you, that works. Is there a possibility that when the dropdown menu is set to false, that the function node does not give an output, currently it always gives false but I would like to continue to control the pin via a button when the dropdown menu is set to false.

I'm not sure exactly what you are after.
You could change the last line of the function node to return null; and see if that works for you.

let dropdown = flow.get("dropdown") || false;
let threshold = 10;

if (msg.payload > threshold && dropdown === true) {
    msg.payload = true;
    node.status({fill:"red",shape:"dot",text:"Turn Pin ON"});
    return msg;
}
else {
    msg.payload = false;
    node.status({fill:"green",shape:"dot",text:"Turn Pin OFF"});
    return null;  // <<<< change this line
}
1 Like

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