I’ d like to reschedule a time every 60 seconds until a switch changes its state to OFF.
However, what I have got doe snot reschedule it; it only runs the initial timer.
The current rule:
val String LOG_PREFIX = "Water."
var Timer g_bore_pump_timer = null
rule "Watersystem: Update Bore Pump run time"
when
Item Bore_Pump_Status changed from OFF to ON or
Item test_Switch changed from OFF to ON
then
// increment number of pump starts by 1
var int PUMP_STARTS = (Bore_Pump_Starts.state as Number).intValue + 1
//Bore_Pump_Starts.postUpdate(PUMP_STARTS)
logInfo(LOG_PREFIX + "07.01", "Bore Pump: total starts.............: {}", PUMP_STARTS)
// timer interval in seconds
val TIMER_INTERVAL_IN_SECONDS = 60
val TIMER_INTERVAL_IN_HOURS = 1.0 / TIMER_INTERVAL_IN_SECONDS
logInfo(LOG_PREFIX + "07.02", "TIMER_INTERVAL_IN_SECONDS...........: {}", TIMER_INTERVAL_IN_SECONDS)
logInfo(LOG_PREFIX + "07.03", "TIMER_INTERVAL_IN_HOURS.............: {}", TIMER_INTERVAL_IN_HOURS)
if ((g_bore_pump_timer === null) || g_bore_pump_timer.hasTerminated())
{
// create timer
g_bore_pump_timer = createTimer(now.plusSeconds(TIMER_INTERVAL_IN_SECONDS))
[|
// read previously saved run time and add interval
val PUMP_RUN_TIME = (Bore_Pump_RunTime_Hours.state as Number) + TIMER_INTERVAL_IN_HOURS
logInfo(LOG_PREFIX + "07.04", "Bore_Pump_RunTime_Hours.............: {}", Bore_Pump_RunTime_Hours.state)
Bore_Pump_RunTime_Hours.postUpdate(PUMP_RUN_TIME)
// read previously saved run time for today and add interval
val RUN_TIME_TODAY = (Bore_Pump_RunTime_Hours_Today.state as Number) + TIMER_INTERVAL_IN_HOURS
logInfo(LOG_PREFIX + "07.05", "Bore_Pump_RunTime_Hours_Today.......: {}", Bore_Pump_RunTime_Hours_Today.state)
Bore_Pump_RunTime_Hours_Today.postUpdate(RUN_TIME_TODAY)
]
}
else
{
// if pump is still on reschedule the timer
if (Bore_Pump_Status.state == ON || test_Switch.state == ON)
{
val PUMP_RUN_TIME = (Bore_Pump_RunTime_Hours.state as Number) + TIMER_INTERVAL_IN_HOURS
logInfo(LOG_PREFIX + "07.06", "Bore_Pump_RunTime_Hours.............: {}", Bore_Pump_RunTime_Hours.state)
Bore_Pump_RunTime_Hours.postUpdate(PUMP_RUN_TIME)
// read previously saved run time for today and add interval
val RUN_TIME_TODAY = (Bore_Pump_RunTime_Hours_Today.state as Number) + TIMER_INTERVAL_IN_HOURS
logInfo(LOG_PREFIX + "07.07", "Bore_Pump_RunTime_Hours_Today.......: {}", Bore_Pump_RunTime_Hours_Today.state)
Bore_Pump_RunTime_Hours_Today.postUpdate(RUN_TIME_TODAY)
g_bore_pump_timer.reschedule(now.plusSeconds(TIMER_INTERVAL_IN_SECONDS))
logInfo(LOG_PREFIX + "07.08", "g_bore_pump_timer...................: ", "rescheduled")
}
else
{
g_bore_pump_timer?.cancel
g_bore_pump_timer = null
}
}
end
I did not specify the type of the variables due to conversion errors.
If you want to wait till you switch will change to off, why not creating a rule that’s triggered by OFF state?
You rule is only triggered by a change from off to on, therefore only triggered once. If the switch will stay on, the rule is not triggered again, therefore it can’t reschedule the timer.
Your reschedule code would need to stay within the time …
Putting the reschedule in the original timer does the trick…
rule "Watersystem: Update Bore Pump run time"
when
Item Bore_Pump_Status changed from OFF to ON or
Item test_Switch changed from OFF to ON
then
// increment number of pump starts by 1
var int PUMP_STARTS = (Bore_Pump_Starts.state as Number).intValue + 1
//Bore_Pump_Starts.postUpdate(PUMP_STARTS)
logInfo(LOG_PREFIX + "07.01", "Bore Pump: total starts.............: {}", PUMP_STARTS)
// timer interval in seconds
val TIMER_INTERVAL_IN_SECONDS = 60
val TIMER_INTERVAL_IN_HOURS = 1.0 / TIMER_INTERVAL_IN_SECONDS
logInfo(LOG_PREFIX + "07.02", "TIMER_INTERVAL_IN_SECONDS...........: {}", TIMER_INTERVAL_IN_SECONDS)
logInfo(LOG_PREFIX + "07.03", "TIMER_INTERVAL_IN_HOURS.............: {}", TIMER_INTERVAL_IN_HOURS)
if ((g_bore_pump_timer === null) || g_bore_pump_timer.hasTerminated())
{
// create timer
g_bore_pump_timer = createTimer(now.plusSeconds(TIMER_INTERVAL_IN_SECONDS))
[|
// read previously saved run time and add interval
val PUMP_RUN_TIME = (Bore_Pump_RunTime_Hours.state as Number) + TIMER_INTERVAL_IN_HOURS
logInfo(LOG_PREFIX + "07.04", "Bore_Pump_RunTime_Hours.............: {}", Bore_Pump_RunTime_Hours.state)
Bore_Pump_RunTime_Hours.postUpdate(PUMP_RUN_TIME)
// read previously saved run time for today and add interval
val RUN_TIME_TODAY = (Bore_Pump_RunTime_Hours_Today.state as Number) + TIMER_INTERVAL_IN_HOURS
logInfo(LOG_PREFIX + "07.05", "Bore_Pump_RunTime_Hours_Today.......: {}", Bore_Pump_RunTime_Hours_Today.state)
Bore_Pump_RunTime_Hours_Today.postUpdate(RUN_TIME_TODAY)
g_bore_pump_timer.reschedule(now.plusSeconds(TIMER_INTERVAL_IN_SECONDS))
logInfo(LOG_PREFIX + "07.08", "g_bore_pump_timer...................: ", "rescheduled")
]
}
end
rule "Watersystem: Bore Pump stopped"
when
Item Bore_Pump_Status changed from ON to OFF or
Item test_Switch changed from ON to OFF
then
if (g_bore_pump_timer !== null)
{
logInfo(LOG_PREFIX + "08.01", "Bore Pump: stopped")
g_bore_pump_timer?.cancel
g_bore_pump_timer = null
}
if (Bore_Pump_RunTime_Hours_Today.state > 1)
{
if (Bore_Pump_RunTime_Exceeded.state == OFF)
{
logInfo(LOG_PREFIX + "08.02", "Bore Pump: todayHours exceeded.. 1h")
Bore_Pump_RunTime_Exceeded.postUpdate(ON)
}
}
end