RULE FILE for a timer that is triggered/retriggered when a specific item receives an update.
This works for all the use cases you describe above:
In this case “var Integer pantry_timeout = 1” defines a timer with a duration of 1 minute.
Each time " MotionSensor_Pantry_Motion" is updated the timer is triggered/retriggered for the duration of “var Integer pantry_timeout = 1”.
For presence purposes like you described you would replace " MotionSensor_Pantry_Motion" for a switch item that represents your phone.
But please look around on the forum a bit, there are several better ways for presence detections using phone-openhab. (expire binding, Unifi binding, dd-wrt trick)
var Timer pantry_timer = null
var Integer pantry_timeout = 1
rule "Pantry light motion"
when
Item MotionSensor_Pantry_Motion received update
then
if (MotionSensor_Pantry_Motion.state == ON && Night.state == ON) {
if (pantry_timer != null)
{
pantry_timer.reschedule(now.plusMinutes(pantry_timeout))
logInfo("Lights","Pantry light timer rescheduled for " + pantry_timeout + " minutes")
}
else
{
sendCommand(Switch_Pantry_1, ON)
logInfo("Lights","Pantry light timer created for " + pantry_timeout + " minutes")
RulePantryLightMotionTS.postUpdate( new DateTimeType() )
pantry_timer = createTimer(now.plusMinutes(pantry_timeout))
[|
if (MotionSensor_Pantry_Motion.state == ON)
{
pantry_timer.reschedule(now.plusMinutes(pantry_timeout))
logInfo("Lights","Pantry timer triggered, but rescheduled again for " + pantry_timeout + " minutes")
}
else
{
logInfo("Lights","Pantry light timer expired")
sendCommand(Switch_Pantry_1,OFF)
pantry_timer = null
}
]
}
}
end