Here is an example of a rule I use to detect a rapid rate of change in humidity in my bathroom, which then turns the bathroom fan on/off;
rule "Bathroom fan"
when
Item Sensor_Humidity2 changed
then
// rule parameters
var int turnOnThreshold = 5 // rate of change per min
var int turnOffThreshold = 50 // static value
// get the current value
var Number humidity = Sensor_Humidity2.state as DecimalType
// check if the humidity is rising or falling...
if (humidity2_LastValue > 0) {
if (humidity > humidity2_LastValue) {
// get the difference since our last update
var Number humidityDelta = humidity - humidity2_LastValue
var Number timeDeltaMs = now.millis - humidity2_LastUpdated
// calculate the rate of change - % per minute
var Number rateOfChange = humidityDelta / timeDeltaMs
var Number rateOfChangePerMin = rateOfChange * 60000
// check if rising rapidly, faster than our threshold
if (rateOfChangePerMin >= turnOnThreshold && Heating_BathroomFan.state == OFF) {
Notify_Info.postUpdate("Bathroom humidity increasing at " + rateOfChangePerMin + "%/min - turning on fan")
Heating_BathroomFan.sendCommand(ON)
}
} else {
// check if humidity is falling and drops below our threshold
if (humidity <= turnOffThreshold && Heating_BathroomFan.state == ON) {
Notify_Info.postUpdate("Bathroom humidity dropped to " + humidity + "% - turning off fan")
Heating_BathroomFan.sendCommand(OFF)
}
}
}
// update our 'last' value/update dttm so we can track the rate of increase
humidity2_LastValue = humidity
humidity2_LastUpdated = now.millis
end