Needing help with manual override switch

Hi All,

Im switching a fan on and off with a function node 'fan control' between to set points and now i would like to add a manual override function node 'Auto/Manual switch' but i just can't get it to work. would some one be able to show me where i am going wrong.

Cheers
Jez

[{"id":"4a78ae1a.0ffd8","type":"rpi-dht22","z":"ecea2ab1.faa898","name":"DHT22","topic":"rpi-dht22","dht":22,"pintype":"2","pin":"7","x":260,"y":180,"wires":[["e1353e0e.1a8d8"]]},{"id":"5f1b2230.c0db7c","type":"inject","z":"ecea2ab1.faa898","name":"Read DHT22","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":180,"wires":[["4a78ae1a.0ffd8"]]},{"id":"e1353e0e.1a8d8","type":"function","z":"ecea2ab1.faa898","name":"temp","func":"msg.payload = msg.payload\nmsg.topic = \"Temp\"\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":180,"wires":[["5a17bd63.59e55c"]]},{"id":"5a17bd63.59e55c","type":"function","z":"ecea2ab1.faa898","name":"Fan control","func":"var Temp=msg.payload;\nif (Temp <= 24) {\n  msg.payload = 0\n} else if (Temp >= 27) {\n  msg.payload = 1\n} else {\n  msg = null\n}\nreturn msg","outputs":1,"noerr":0,"x":570,"y":180,"wires":[["91b7acb7.cbeaa","c70a9133.6c9768"]]},{"id":"91b7acb7.cbeaa","type":"function","z":"ecea2ab1.faa898","name":"Auto/Manual switch","func":"var manual_switch = global.get(\"AutoManual_switch\");\nvar fan_control = global.get(\"fan_control\");\nvar OnOff_switch = global.get(\"OnOff_switch\");\n\nif (manual_switch === 0) {\n    if (fan_control == 1) {\n        msg.payload == 1;\n    }\n    else if (fan_control === 0) {\n        msg.payload === 0;\n    }\n}\nelse if (manual_switch == 1) {\n    if (OnOff_switch === 0){\n        msg.payload === 0;\n    }\n    else if (OnOff_switch == 1) {\n        msg.payload == 1;\n    }\n}\nreturn msg;\n    ","outputs":1,"noerr":0,"x":780,"y":160,"wires":[["91f1d00a.bee9d8"]]},{"id":"c70a9133.6c9768","type":"change","z":"ecea2ab1.faa898","name":"Fan Control var","rules":[{"t":"set","p":"fan_control","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":200,"wires":[[]]},{"id":"91f1d00a.bee9d8","type":"rpi-gpio out","z":"ecea2ab1.faa898","name":"Fan","pin":"19","set":true,"level":"0","freq":"","out":"out","x":970,"y":180,"wires":[]},{"id":"624ce528.839134","type":"ui_switch","z":"ecea2ab1.faa898","name":"","label":"Fan Auto/Manual switch","tooltip":"","group":"e4fc1fd3.c1044","order":4,"width":5,"height":1,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":110,"y":280,"wires":[["b04b1d9b.fd94a8","e7acc5e1.ab269"]]},{"id":"d3640ec8.fe883","type":"ui_switch","z":"ecea2ab1.faa898","name":"","label":"Fan On/Off switch","tooltip":"","group":"e4fc1fd3.c1044","order":4,"width":5,"height":1,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":130,"y":380,"wires":[["db849e29.46fb1","866440c.b4269c"]]},{"id":"b04b1d9b.fd94a8","type":"change","z":"ecea2ab1.faa898","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"Auto","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"Manual","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[["2f24b0ea.9d2cb8"]]},{"id":"e7acc5e1.ab269","type":"change","z":"ecea2ab1.faa898","name":"Auto/Manual var","rules":[{"t":"set","p":"AutoManual_switch","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":300,"wires":[[]]},{"id":"866440c.b4269c","type":"change","z":"ecea2ab1.faa898","name":"OnOff var","rules":[{"t":"set","p":"OnOff_switch","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":400,"wires":[[]]},{"id":"db849e29.46fb1","type":"change","z":"ecea2ab1.faa898","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"Off","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"On","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":360,"wires":[["d0a70e40.d38838"]]},{"id":"d0a70e40.d38838","type":"ui_text","z":"ecea2ab1.faa898","group":"e4fc1fd3.c1044","order":5,"width":2,"height":1,"name":"","label":"","format":"{{msg.payload}}","layout":"row-spread","x":530,"y":380,"wires":[]},{"id":"2f24b0ea.9d2cb8","type":"ui_text","z":"ecea2ab1.faa898","group":"e4fc1fd3.c1044","order":5,"width":2,"height":1,"name":"","label":"","format":"{{msg.payload}}","layout":"row-spread","x":530,"y":280,"wires":[]},{"id":"e4fc1fd3.c1044","type":"ui_group","z":"","name":"Text","tab":"9caadd41.281e9","order":2,"disp":false,"width":7,"collapse":false},{"id":"9caadd41.281e9","type":"ui_tab","z":"","name":"Remote View","icon":"dashboard","disabled":false,"hidden":false}]

I would use a pulldown menu instead, On/Off/Auto

Hello,
have a look at this, rewrote your flow, explanation below:

[{"id":"246bd678.de324a","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"17","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":1160,"wires":[["4d98c56f.a6b00c"]]},{"id":"4d98c56f.a6b00c","type":"function","z":"f954564f.03e718","name":"Fan control","func":"const Temp = msg.payload;\nconst override = global.get(\"AutoManual_switch\") || 0;\nconst fanstate = global.get(\"FanState\");\nif (override != 1) {\n    if (Temp <= 24 && fanstate !== 0) {\n        msg.payload = 0;\n        global.set(\"FanState\", msg.payload);\n        return msg;\n    } else if (Temp >= 27 && fanstate != 1) {\n        msg.payload = 1;\n        global.set(\"FanState\", msg.payload);\n        return msg;\n    } else {\n        return null;\n    }\n}","outputs":1,"noerr":0,"x":810,"y":1200,"wires":[["6e265f71.fb3a6","65be4476.5dc02c","5bee4a8f.3e9474"]]},{"id":"322a543.5d29b2c","type":"ui_switch","z":"f954564f.03e718","name":"","label":"Fan Auto/Manual switch","tooltip":"","group":"ad3e78a9.60d328","order":1,"width":"0","height":"0","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":1730,"y":1160,"wires":[["2ad33de.326ce42","5a900562.3c1bcc"]]},{"id":"65be4476.5dc02c","type":"ui_switch","z":"f954564f.03e718","name":"","label":"Fan On/Off switch","tooltip":"","group":"ad3e78a9.60d328","order":3,"width":"0","height":"0","passthru":false,"decouple":"true","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":1110,"y":1160,"wires":[["6879a6fc.3ca07","5bee4a8f.3e9474","6e265f71.fb3a6"]]},{"id":"2ad33de.326ce42","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"Auto","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"Manual","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1970,"y":1120,"wires":[["6be889a8.402848"]]},{"id":"5a900562.3c1bcc","type":"change","z":"f954564f.03e718","name":"Auto/Manual var","rules":[{"t":"set","p":"AutoManual_switch","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1970,"y":1180,"wires":[[]]},{"id":"6879a6fc.3ca07","type":"change","z":"f954564f.03e718","name":"FanState var","rules":[{"t":"set","p":"FanState","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1310,"y":1160,"wires":[["65be4476.5dc02c","938160bc.5804e"]]},{"id":"5bee4a8f.3e9474","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"Off","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"On","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":1100,"wires":[["714c6456.6ce8dc"]]},{"id":"714c6456.6ce8dc","type":"ui_text","z":"f954564f.03e718","group":"ad3e78a9.60d328","order":4,"width":"0","height":"0","name":"OnOFF Text","label":"","format":"{{msg.payload}}","layout":"row-spread","x":1290,"y":1100,"wires":[]},{"id":"6be889a8.402848","type":"ui_text","z":"f954564f.03e718","group":"ad3e78a9.60d328","order":2,"width":"0","height":"0","name":"manualText","label":"","format":"{{msg.payload}}","layout":"row-spread","x":2170,"y":1160,"wires":[]},{"id":"6e265f71.fb3a6","type":"debug","z":"f954564f.03e718","name":"Output to GPIO","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1100,"y":1280,"wires":[]},{"id":"edeb03a5.5ab118","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"28","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":1240,"wires":[["4d98c56f.a6b00c"]]},{"id":"938160bc.5804e","type":"function","z":"f954564f.03e718","name":"turn on override","func":"const override = global.get(\"AutoManual_switch\") || 0;\nif(override === 0){\n    msg.payload = 1;\n    return msg;\n} else {\n    return null;\n}","outputs":1,"noerr":0,"x":1500,"y":1160,"wires":[["322a543.5d29b2c"]]},{"id":"d2c21975.fbb22","type":"comment","z":"f954564f.03e718","name":"Temp input","info":"","x":480,"y":1080,"wires":[]},{"id":"ad3e78a9.60d328","type":"ui_group","z":"","name":"Text","tab":"85b04dc9.699e8","order":2,"disp":false,"width":7,"collapse":false},{"id":"85b04dc9.699e8","type":"ui_tab","z":"","name":"Remote View","icon":"dashboard","disabled":false,"hidden":false}]

It should do what you want now. When a temperature arrives the fan control function checks for both the manual override and the current fan state. Only if the override is off and the new state would be different than the last state will it send a fan command and will set the fanstate variable to the new state.
This new state also gets passed to the on/off switch so it always represents the current fan state.
The on off switch itself is directly connected to the output and can switch the fan. It is connected to itself as it only changes state of the switch on input messages as to not create inetractions with the second part of the flow when automatically switching.
The second part is the ovveride.
The override is a switch that sets the override variable that gets checked by the fan control function.
I added a functionality that if you manually turn off or on the fan from the ui the override automatically gets turned on as to prevent the fan to change back when a different temp arrives.
If you dont want this you can just delete the turn on override function.
You will of course have to replace the debug with your gpio output and put the dht read back in instead of the two inputs i used to simulate. I also changed the dashboard layout a little bit as it wasnt displaying correctly on my phone. Feel free to change it back.
Hope this is what you are looking for.
Best regards Johannes

Cheers Johannes works a treat, thanks very much for taking the time to work it out for me.

I’m also intrigued by the dropdown node that Bakman suggested but I have no idea on how to use it, any help would be appreciated.

Cheers
Jez

Here is a dropdown implementation:

[{"id":"246bd678.de324a","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"17","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":470,"y":1420,"wires":[["4d98c56f.a6b00c"]]},{"id":"4d98c56f.a6b00c","type":"function","z":"f954564f.03e718","name":"Fan control","func":"const Temp = msg.payload;\nconst override = global.get(\"AutoManual_switch\") || 0;\nconst fanstate = global.get(\"FanState\");\nif (override != 1) {\n    if (Temp <= 24 && fanstate !== 0) {\n        msg.payload = 0;\n        global.set(\"FanState\", msg.payload);\n        return msg;\n    } else if (Temp >= 27 && fanstate != 1) {\n        msg.payload = 1;\n        global.set(\"FanState\", msg.payload);\n        return msg;\n    } else {\n        return null;\n    }\n}","outputs":1,"noerr":0,"x":790,"y":1460,"wires":[["6e265f71.fb3a6","5bee4a8f.3e9474"]]},{"id":"2ad33de.326ce42","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"Auto","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"Manual","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":1180,"wires":[["6be889a8.402848"]]},{"id":"5a900562.3c1bcc","type":"change","z":"f954564f.03e718","name":"Auto/Manual var","rules":[{"t":"set","p":"AutoManual_switch","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1280,"y":1220,"wires":[[]]},{"id":"6879a6fc.3ca07","type":"change","z":"f954564f.03e718","name":"FanState var","rules":[{"t":"set","p":"FanState","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":1420,"wires":[[]]},{"id":"5bee4a8f.3e9474","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"Off","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"On","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":1360,"wires":[["714c6456.6ce8dc"]]},{"id":"714c6456.6ce8dc","type":"ui_text","z":"f954564f.03e718","group":"ad3e78a9.60d328","order":3,"width":"0","height":"0","name":"OnOFF Text","label":"","format":"{{msg.payload}}","layout":"row-spread","x":1270,"y":1360,"wires":[]},{"id":"6be889a8.402848","type":"ui_text","z":"f954564f.03e718","group":"ad3e78a9.60d328","order":2,"width":"0","height":"0","name":"manualText","label":"","format":"{{msg.payload}}","layout":"row-spread","x":1270,"y":1180,"wires":[]},{"id":"6e265f71.fb3a6","type":"debug","z":"f954564f.03e718","name":"Output to GPIO","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1080,"y":1540,"wires":[]},{"id":"edeb03a5.5ab118","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"28","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":470,"y":1500,"wires":[["4d98c56f.a6b00c"]]},{"id":"938160bc.5804e","type":"function","z":"f954564f.03e718","name":"turn on override","func":"const override = global.get(\"AutoManual_switch\") || 0;\nif(override === 0){\n    msg.payload = 1;\n    return msg;\n} else {\n    return null;\n}","outputs":1,"noerr":0,"x":1080,"y":1280,"wires":[["2ad33de.326ce42","5a900562.3c1bcc"]]},{"id":"72310995.d00fe8","type":"ui_dropdown","z":"f954564f.03e718","name":"","label":"","tooltip":"","place":"Select option","group":"ad3e78a9.60d328","order":1,"width":0,"height":0,"passthru":true,"options":[{"label":"ON","value":1,"type":"num"},{"label":"OFF","value":0,"type":"num"},{"label":"AUTO","value":"auto","type":"str"}],"payload":"","topic":"","x":650,"y":1300,"wires":[["6b3f9be2.e49dec"]]},{"id":"6b3f9be2.e49dec","type":"switch","z":"f954564f.03e718","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"auto","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":810,"y":1300,"wires":[["2ad33de.326ce42","9f506b80.990a6"],["938160bc.5804e","5bee4a8f.3e9474","6e265f71.fb3a6","6879a6fc.3ca07"]]},{"id":"d2c21975.fbb22","type":"comment","z":"f954564f.03e718","name":"Temp input","info":"","x":460,"y":1560,"wires":[]},{"id":"9f506b80.990a6","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":1220,"wires":[["5a900562.3c1bcc"]]},{"id":"7f1fee39.4bfe8","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"auto","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":470,"y":1300,"wires":[["72310995.d00fe8"]]},{"id":"ad3e78a9.60d328","type":"ui_group","z":"","name":"Text","tab":"85b04dc9.699e8","order":2,"disp":false,"width":"7","collapse":false},{"id":"85b04dc9.699e8","type":"ui_tab","z":"","name":"Remote View","icon":"dashboard","disabled":false,"hidden":false}]

As long as Auto is choosen from the dropdown the fan will change according to the temp.
as soon as you choose on or off the fan will stay that way and ignore temp changes until you choose auto again.
Auto mode gets set automatically on first deploy or restart.
Best regards Johannes

Thanks again mate for your help, I’ll give it a go in the morning.

Cheers,
Jez

1 Like

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