How to control fan with 2 different set points

Im having trouble trying to control a fan based on temp. I want it to start at 27 and stop at 24 but it starts and stops on 24. ive tried a couple of ways but just cant get it to work.

[{"id":"fd535ba.ed8a328","type":"rpi-dht22","z":"e4167a44.5763d8","name":"DHT22","topic":"rpi-dht22","dht":22,"pintype":"0","pin":"4","x":1300,"y":280,"wires":[["7e9597b4.46cda8"]]},{"id":"963b8cff.95b678","type":"inject","z":"e4167a44.5763d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":1150,"y":280,"wires":[["fd535ba.ed8a328"]]},{"id":"7e9597b4.46cda8","type":"function","z":"e4167a44.5763d8","name":"temp","func":"msg.payload = msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":1430,"y":280,"wires":[["43f6add8.9d14fc"]]},{"id":"2e3fcd4f.ff4c9a","type":"rpi-gpio out","z":"e4167a44.5763d8","name":"Fan","pin":"23","set":true,"level":"0","freq":"","out":"out","x":1730,"y":280,"wires":[]},{"id":"43f6add8.9d14fc","type":"function","z":"e4167a44.5763d8","name":"Fan control","func":"//var Temp=msg.payload;\n//var Output=\"\"\n//if (Temp>=27) {\n//    Output=true\n//} else\n//if (Temp<=24) {\n//    Output=false\n//}\n//msg.payload=Output\n//return msg;\n\n\nvar Temp=msg.payload;\nvar Output;\n\nif (Output=true){ \n    if (Temp<=24){\n       Output=false; \n    }\n}else\nif (Output=false){ \n   if (Temp>=27){\n        Output=true; \n    }\n}\nmsg.payload=Output\nreturn msg;\n\n\n\n//var Temp=msg.payload;\n//var Output;\n\n//if (Output){ \n//    if (Temp<=25){\n//        Output=false; \n//    }\n//}\n//else{\n//    if (Temp>=27) { \n//        Output=true; \n//    }\n//}\n//msg.payload=Output\n//return msg;","outputs":1,"noerr":0,"x":1570,"y":280,"wires":[["2e3fcd4f.ff4c9a"]]}]

You have written the equality incorrectly in your IF tests.
It should be double = signs to compare against a string or number and TRIPLE quotes to compare a boolean..
For example if(Output === true) {

if (Output=true){
if (Temp<=24){
Output=false;
}
}else
if (Output=false){
if (Temp>=27){
Output=true;
}
}

Also you are not remembering the previous output state, you need to save it in the node context. However there is a somewhat simpler alternative. Something like

if (msg.payload <= 24) {
  msg.payload = false
} else if (msg.payload >= 27) {
  msg.payload = true
} else {
  msg = null
}
return msg

This sets the fan state appropriately if outside the threshold values but leaves it as it is otherwise by not sending any message at all (as msg is null).

1 Like

Thanks for your help guys, its up and running.