# How to control fan with 2 different set points

**URL:** <https://discourse.nodered.org/t/how-to-control-fan-with-2-different-set-points/12206>\
**Category:** General\
**Created:** [14 June 2019 02:29 UTC](https://discourse.nodered.org/t/how-to-control-fan-with-2-different-set-points/12206 "2019-06-14T02:29:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jez](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Jez](https://discourse.nodered.org/u/Jez)\
**Post date:** [14 June 2019 02:29 UTC](https://discourse.nodered.org/t/how-to-control-fan-with-2-different-set-points/12206/1 "2019-06-14T02:29:00Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [14 June 2019 06:46 UTC](https://discourse.nodered.org/t/how-to-control-fan-with-2-different-set-points/12206/2 "2019-06-14T06:46:23Z")

</div>

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;  
}  
}

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 07:56 UTC](https://discourse.nodered.org/t/how-to-control-fan-with-2-different-set-points/12206/3 "2019-06-14T07:56:33Z")

</div>

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

```auto
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).

---

<div class="post-metadata">

**Author:** ![Jez](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Jez](https://discourse.nodered.org/u/Jez)\
**Post date:** [15 June 2019 01:06 UTC](https://discourse.nodered.org/t/how-to-control-fan-with-2-different-set-points/12206/4 "2019-06-15T01:06:02Z")

</div>

Thanks for your help guys, its up and running.
