I made recently a bathroom fan control based on light on / off ( lux detection was not possible because our bathroom has a window!).
The control is based on the idea to mesure the humidity when the light is switched on, to switch the fan if the humidity value is above a certain level for me : 40%.
Experrience shows me that the humidity change quickly after after fan runs because the humidity sensor near the fan mesures the real ambiance air humidity after some time. So i make a new mesure 60 sec after light on.
When light is switched off : new mesure and compare with value at beginning. If no change turn fan off. If humidity has increased, run the fan until hum level reachs the start level.
After 15 min stop fan anyway. If light is switched on when fan is running no new value is mesured because the deshumidification process is still ongoing : this could be the case to turn light on quickly after having turn thr light off.
and rules ... please delete comments to save space !!
Code: Select all
On system#boot do
let,1,50 // Hum Ecl On
let,2,18 //Temp Ecl On
let,3,51 //Hum Ecl Off
let,4,19 // Temp Ecl Off
let,5,0 // Delta Hum
let,6,0 // Delta Temp
let,7,900 // Tempo max Fan on in sec
let,8,40 // low limit Hum %
let,9,0 // fan was not on
gpio,0,0 // Fan Off
// timer 1 : check interval hum level if Ecl = 0ff
// timer 2 : max run time of fan
// timer 3 : recheck Hum after Ecl On
endon
On Ecl#State=1 do // Ecl On
if [Fan#State] = 0
Let,1,[Temp_Hum#°C]
Let,2,[Temp_Hum#Hum%]
Let,3,[Temp_Hum#°C]
Let,4,[Temp_Hum#Hum%]
Let,9,0
TaskValueSet,12,1,[var#2] // Store actual hum in non volatile variable
else
Let,9,1 // fan was on
endif
if [Temp_Hum#Hum%] > [var#8] // higher than low limit hum %
Gpio,0,1 // Fan On
Publish, %sysname%/Fan,"On"
endif
Timerset,3,60 // wait 60 sec and take a new hum mesure
Publish, %sysname%/Ecl,"On"
Endon
On Rules#timer=3 do // new mesure 60 sec after Ecl = On (time needed for ambiance air to reach the fan hum sensor)
if [Temp_Hum#Hum%]> [var#8] and [Ecl#State]=1
Gpio,0,1 // Fan On
Publish, %sysname%/Fan,"On"
else
Timerset,3,60
endif
if [Var#9] = 0 // only if fan was not already on
Let,1,[Temp_Hum#°C]
Let,2,[Temp_Hum#Hum%]
TaskValueSet,12,1,[var#2]
endif
endon
On Ecl#State=0 do // Ecl Off -> store actual values in spare variables
Let,3,[Temp_Hum#°C]
Let,4,[Temp_Hum#Hum%]
Let,5,[var#3]-[var#1]
Let,6,[var#4]-[var#2]
TaskValueSet,12,2,[var#4]
TaskValueSet,12,3,[var#6]
Publish, %sysname%/Ecl,"Off"
Timerset,1,10 // wait 10 sec
Timerset,2,[var#7]
endon
// Rules set 2
On Rules#timer=1 do
if [var#5] <= 1 // If hum did not increase ...
Gpio,0,0 // Fan off
Publish, %sysname%/Fan,"Off"
else
Let,3,[Temp_Hum#°C] // if not : reload the variables
Let,4,[Temp_Hum#Hum%]
Let,5,[var#3]-[var#1]
Let,6,[var#4]-[var#2]
TaskValueSet,12,2,[var#4]
TaskValueSet,12,3,[var#6]
Publish, %sysname%/Hum1, [var#2]
Publish, %sysname%/Hum2, [var#4]
Publish, %sysname%/Delta_Hum, [var#6]
Timerset,1,10
endif
endon
On Rules#timer=2 do
Gpio,0,0 // after 15 min Fan off
Publish, %sysname%/Fan,"Off"
Timerset,1,0
endon
On Temp_Hum#°C do
Publish, %sysname%/Temp, [Temp_Hum#°C]
Publish, %sysname%/Hum, [Temp_Hum#Hum%]
Publish, %sysname%/Atm, [Temp_Hum#hPa]
Publish, %sysname%/Delta_Hum, [var#6]
endon
Have fun ...