Automating shower exhaust fan by comparing humidity sensors

I’m setting up a rule that turns on the bathroom exhaust fan if the humidity in the bathroom is 10 percentage points higher than the humidity in the livingroom.

Additionally, I want the fan to stay on no more than one hour at a time, and come back on no sooner than 15 minutes after it last shut off.

I think the following rule will do that, but it’s hard to test without running the shower a bunch :smile:
And there’s a warning in designer on the line where I subtract the value of the livingroom humidity from the value of the bathroom humidity. What did I do wrong?

import org.joda.time.*

var DateTime FanTimeOn = null
var DateTime FanTimeOff = null

rule "Bathroom exhaust fan"
when
		Item BRHumiditySensorCurrentLevel received update
then
		var BRHumid = BRHumiditySensorCurrentLevel.state
		var LRHumid = LRHumiditySensorCurrentLevel.state
	if(FanTimeOn == null && FanTimeOff.plusMinutes(20).isBeforeNow()) {
			if(BRHumid - LRHumid => 10){
				BathroomFan.sendCommand(ON)	
				FanTimeOn = now.toDateTime
			}
	}
	else if(FanTimeOn != null && FanTimeOn.plusMinutes(60).isBeforeNow()){
				BathroomFan.sendCommand(OFF)
				FanTimeOff = now.toDateTime
	}
end

I’m not sure, but I think they will be better like this:

var DecimalType BRHumid = BRHumiditySensorCurrentLevel.state as DecimalType
var DecimalType LRHumid = LRHumiditySensorCurrentLevel.state as DecimalType

but if one of the items is still Undefined, you would be better off doing:

...then
	if (BRHumid.state instanceof DecimalType && 
		LRHumid.state instanceof DecimalType &&
		FanTimeOn == null && FanTimeOff.plusMinutes(20).isBeforeNow()) {
		var DecimalType BRHumid = BRHumiditySensorCurrentLevel.state as DecimalType
		var DecimalType LRHumid = LRHumiditySensorCurrentLevel.state as DecimalType
		if(BRHumid - LRHumid => 10){
				BathroomFan.sendCommand(ON)	
				FanTimeOn = now.toDateTime
			}
	}
	else if(FanTimeOn != null && FanTimeOn.plusMinutes(60).isBeforeNow()){
				BathroomFan.sendCommand(OFF)
				FanTimeOff = now.toDateTime
	}
end

Thanks. I’ll try that out and see how it goes!