-
-
Notifications
You must be signed in to change notification settings - Fork 91
Rule Designer: Example 2
This rule is used to control a bathroom fan based on the humidity of two rooms (the fan is actually a heat recovery system, so impacts both rooms). If the humidity of either room is above a setpoint, it turns the fan on. If the humidity of both rooms is below a different setpoint, it turns the fan off. A different ON/OFF setpoint provides some hystereses which ensures that the fan doesn't switch too often. We also use some the timers to turn the fan on periodically even if the humidity is below the setpoint. Two timers are used to turn on the fan if it's been off for 3 hours, and to turn the it off after 10 minutes if it was turned on by the first timer.
At the very top of the rule is the rule name. This is used within the rule, and must be unique.
Under this is a Definitions section in the rule. Here we define two constants - these are used within the rule, but by defining them as constants here we make it easy to update later.
Comments can be added to the rule by right clicking on a block and clicking "Add Comment". This will add a ? icon to the block, and clicking on this will open a box for you to add the comment. Comments added is some blocks will also be added to the rule output file.
When saving this rule, HABmin will automatically create the following openhab .rules file -:
// This rule file is autogenerated by HABmin.
// Any changes made manually to this file will be overwritten next time HABmin rules are saved.
// Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
// Global Variables
var boolean Override = false
var Timer _timerA = null
var Timer _timerB = null
// Constants used to generate this rule
// HumidityHIGH == 70
// HumidityLOW == 68
rule "Bathroom Fan Controller"
when
Item Bath_TempHum_Humidity changed
or
Item Bed1_TempHum_Humidity changed
or
Item Bath_Fan_State changed
then
// This section controls the fan between a high and low set-point.
if((Bath_TempHum_Humidity.state > 70) || (Bed1_TempHum_Humidity.state > 70)) {
Override = false
sendCommand(Bath_Fan_State, ON)
}
else if((Override == false) && (Bath_TempHum_Humidity.state < 68) && (Bed1_TempHum_Humidity.state < 68)) {
sendCommand(Bath_Fan_State, OFF)
}
// This timer turns the fan on if it has been off for 3 hours.
if(Bath_Fan_State.state == OFF) {
if(_timerA == null) {
_timerA = createTimer(now.plusHours(3)) [|
Override = true
sendCommand(Bath_Fan_State, ON)
]
}
}
else if(_timerA != null) {
_timerA.cancel()
_timerA = null
}
// This timer turns the fan off after 10 minutes if it was turned on by the first timer.
if((Bath_Fan_State.state == ON) && (Override == true)) {
if(_timerB == null) {
_timerB = createTimer(now.plusMinutes(10)) [|
Override = false
sendCommand(Bath_Fan_State, OFF)
]
}
}
else if(_timerB != null) {
_timerB.cancel()
_timerB = null
}
end