OH3: reschedule timer based on state (need help)

  • openHABian 3.3.0 on rPi4 with 4GB

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.

The log file shows this:

2022-12-26 10:11:05.463 [INFO ] [penhab.core.model.script.Water.07.02] - TIMER_INTERVAL_IN_SECONDS...........: 60
2022-12-26 10:11:05.465 [INFO ] [penhab.core.model.script.Water.07.03] - TIMER_INTERVAL_IN_HOURS.............: 0.016666666666666666
2022-12-26 10:12:05.467 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.797
2022-12-26 10:12:05.469 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.033333333333333332

Any hints appreciated.

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

The log confirms it

2022-12-26 10:28:07.217 [INFO ] [penhab.core.model.script.Water.07.01] - Bore Pump: total starts.............: 1500
2022-12-26 10:28:07.220 [INFO ] [penhab.core.model.script.Water.07.02] - TIMER_INTERVAL_IN_SECONDS...........: 60
2022-12-26 10:28:07.222 [INFO ] [penhab.core.model.script.Water.07.03] - TIMER_INTERVAL_IN_HOURS.............: 0.016666666666666666
2022-12-26 10:29:07.225 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.830333333333333332
2022-12-26 10:29:07.228 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.066666666666666664
2022-12-26 10:29:07.232 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:30:07.232 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.846999999999999998
2022-12-26 10:30:07.237 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.083333333333333330
2022-12-26 10:30:07.249 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:31:07.241 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.863666666666666664
2022-12-26 10:31:07.245 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.099999999999999996
2022-12-26 10:31:07.251 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:32:07.250 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.880333333333333330
2022-12-26 10:32:07.252 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.116666666666666662
2022-12-26 10:32:07.258 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:33:07.256 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.896999999999999996
2022-12-26 10:33:07.258 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.133333333333333328
2022-12-26 10:33:07.261 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:34:07.262 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.913666666666666662
2022-12-26 10:34:07.266 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.149999999999999994
2022-12-26 10:34:07.271 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:35:07.270 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.930333333333333328
2022-12-26 10:35:07.272 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.166666666666666660
2022-12-26 10:35:07.275 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:36:07.275 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.946999999999999994
2022-12-26 10:36:07.277 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.183333333333333326
2022-12-26 10:36:07.280 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:37:07.279 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.963666666666666660
2022-12-26 10:37:07.281 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.199999999999999992
2022-12-26 10:37:07.284 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:38:07.285 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.980333333333333326
2022-12-26 10:38:07.289 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.216666666666666658
2022-12-26 10:38:07.295 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:39:07.292 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1148.996999999999999992
2022-12-26 10:39:07.294 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.233333333333333324
2022-12-26 10:39:07.298 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:40:07.299 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1149.013666666666666658
2022-12-26 10:40:07.303 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.249999999999999990
2022-12-26 10:40:07.307 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:41:07.307 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1149.030333333333333324
2022-12-26 10:41:07.311 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.266666666666666656
2022-12-26 10:41:07.315 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:42:07.315 [INFO ] [penhab.core.model.script.Water.07.04] - Bore_Pump_RunTime_Hours.............: 1149.046999999999999990
2022-12-26 10:42:07.320 [INFO ] [penhab.core.model.script.Water.07.05] - Bore_Pump_RunTime_Hours_Today.......: 0.283333333333333322
2022-12-26 10:42:07.327 [INFO ] [penhab.core.model.script.Water.07.08] - g_bore_pump_timer...................: 
2022-12-26 10:46:12.655 [INFO ] [penhab.core.model.script.Water.08.01] - Bore Pump: stopped