Hi guys,
I used this beautiful design pattern from @rlkoshak Design Pattern: Motion Sensor Timer to implement a logic for my bathrooms.
I used the pattern to implement the rule for handling timed turn off of the fan, i.e. if you want to go to toilet you click the button and it turns the fan ON, after period of time it turns it OFF.
Then I use another ruled logic which is very simple but operates the same item: when humidity level goes above certain treshold, turn ON the fan, when it goes bellow another treshold turn it Off.
The problem is that I cannot distinguish the source of the event in order to disable the timer, i.e. when the humidity treshold goes up the event bus fires item changed event and when this event is ON I create timer.
Is there a solution to this? I’m thinking of reading the event source if there is such support
Here is the code:
rule "Item that needs to be switched off after 30 minutes"
when Member of TriggerOffAfterThirdyMinutes changed
then
val timeOutMinutes = 30;
if(triggeringItem.state == ON) {
logDebug("Bathrooms", "Received command ON for item " + triggeringItem.name )
if(fansTimers.get(triggeringItem.name) === null || fansTimers.get(triggeringItem.name).hasTerminated()) {
logDebug("Bathrooms", "Timer of " + triggeringItem.name + " is null or finished. Will create new one." )
fansTimers.put(triggeringItem.name, createTimer(now.plusMinutes(timeOutMinutes), [|
logDebug("Bathrooms", "Timer of " + triggeringItem.name + " finished. Executing command OFF." )
triggeringItem.sendCommand(OFF)
fansTimers.put(triggeringItem.name, null)
]))
} else {
logDebug("Bathrooms", "Rescheduling timer for " + triggeringItem.name + " ...")
fansTimers.get(triggeringItem.name).reschedule(now.plusMinutes(timeOutMinutes))
}
} else {
val timer = fansTimers.get(triggeringItem.name);
if (timer !== null && !timer.hasTerminated()) {
logDebug("Bathrooms", "Canceling timer + " +timer.toString())
timer.cancel();
}
}
end
rule "Bedroom Bath Humidity events"
when Item floor2BedBathroomHumidity received update or Item floor2BedBathroomDesiredHumidity changed
then
val measuredHumidity = floor2BedBathroomHumidity.state as DecimalType
val desiredHumidity = floor2BedBathroomDesiredHumidity.state as DecimalType
val timer = fansTimers.get(floor2BedroomBathFanPower.name);
val tolerance = 5;
if (measuredHumidity <= desiredHumidity - tolerance && floor2BedroomBathFanPower.state == ON ) {
logDebug("Bathrooms", floor2BedBathroomHumidity.label + " value=" + measuredHumidity + "%.")
if (timer === null || timer.hasTerminated()) {
logDebug("Bathrooms", "Will stop Fan.")
floor2BedroomBathFanPower.sendCommand(OFF)
} else {
logDebug("Bathrooms", "Timer is not null or finished. Cannot stop it until timer finishes...")
}
} else if (measuredHumidity > desiredHumidity + tolerance && floor2BedroomBathFanPower.state == OFF) {
logDebug("Bathrooms", "Will start Fan.")
floor2BedroomBathFanPower.sendCommand(ON)
}
end