Published on

Automatic Bathroom Fan

Authors

The fan in my bathroom (certified 1989) once had a time delay function, but it was not correctly connected. Not sure if this was done because it didn't work anymore. Additionally, the wire that was supposed to carry constant voltage wasn't even connected behind the mirror cabinet...

After providing constant voltage to the fan, with a Shelly 1 Plus, Addon Board and a DHT22 humidity/temperature sensor, not just a simple time delay, but a complex humidity-dependent control system could be set up.

  • Switch inputs are always responded to

  • If humidity exceeds a value x, the fan turns on

  • If it falls below a value Y, the fan turns off

  • Switch inputs and automatic activation pause the automation for a certain period

let blocktime = 15 * 60 * 1000;  //15Min
let humidity_on = 65;          //Upper Limit, start Fan
let humidity_off = 60          //Lower Limit, stop Fan

let suspended = false          //indicate suspension. When Fan is switched, for "blocktime" no further automatic switching
let timer = null;              

//reset automatic switching
function reset(){
  suspended =  false;
  Timer.clear(timer);
  print("Autmation reenabled");
}

//suspend automatic switching for "blocktime"
function suspend(){
  print("Suspend automation");
  suspended = true;
  if(timer)Timer.clear(timer);
  timer=Timer.set(blocktime,false,reset);
}

Shelly.addEventHandler(function(e){
    //Manual switching disables automation for "blocktime"
    if(e.component==="switch:0"){
        suspend();
    }
    if(!suspended){
      if(e.info.event == "humidity_measurement"){
        if(e.info.rh>humidity_on){
            Shelly.call("Switch.set", {'id': 0, 'on': true});
            print("automatic turn on");
            suspend();
        }
        if(e.info.rh<humidity_off){
            Shelly.call("Switch.set", {'id': 0, 'on': false});
            print("automatic turn off");
            //not suspending
        }
      }
    }
  }
);