Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Timers in OH rules sometimes not being executed #4208

Closed
2 tasks
DanielMalmgren opened this issue Sep 7, 2017 · 1 comment
Closed
2 tasks

Timers in OH rules sometimes not being executed #4208

DanielMalmgren opened this issue Sep 7, 2017 · 1 comment

Comments

@DanielMalmgren
Copy link

I have the following code in a openHAB rule:

when
    Item Utetemperatur changed
then
    sendHttpGetRequest("http://www.temperatur.nu/rapportera.php?hash=supersecret&t=" + Utetemperatur.state)
    logDebug("temprule", "Sending " + Utetemperatur.state + " to temperatur.nu")

    if(timer_u == null) {
        logDebug("temprule", "Setting timer")
        timer_u = createTimer(now.plusMinutes(19)) [|
            sendHttpGetRequest("http://www.temperatur.nu/rapportera.php?hash=supersecret&t=" + Utetemperatur.state)
            logDebug("temprule", "Resending " + Utetemperatur.state + " to temperatur.nu and rescheduling timer")
            timer_u.reschedule(now.plusMinutes(19))
        ]
    } else {
        logDebug("temprule", "Rescheduling timer")
        timer_u.reschedule(now.plusMinutes(19))
    }
end

(timer_u is declared as a global Timer)
What this is supposed to achieve is the following:

  • Every time the sensor reading changes, send the reading to temperatur.nu
  • If the reading has not changed for 19 minutes, send the reading anyway and reschedule the Timer

This means that during normal operation (ie the sensor is changed more often than every 19:th minute) the Timer is never executed, it's just rescheduled forward forever. If the temperature doesn't change in 19 minutes the Timer executed but is also rescheduled. So no matter what, it's supposed to be rescheduled indefinitely.

This almost always works the way it's supposed to. But then suddenly it happens that the Timer is supposed to be executed and simply doesn't. The last thing I see from this rule in the log is "Rescheduling timer" and then after 19 minutes when it's supposed to execute I get nothing. Everything seems to start working like it should again the next time I actually get a new reading.

The theory from the forum thread where this has been discussed (https://community.openhab.org/t/eliminating-lingering-timers/33755/19?u=danielmalmgren) is that maybe Timers are being garbage collected? If so, that's no good. When writing a rule you should be able to trust a Timer always being executed, no matter what.

@DanielMalmgren
Copy link
Author

I'll simply close this one. Turned out the problem was me rescheduling a timer from inside the timer's body, which I thought I could do. Apparently a timer is only executed once so that's never gonna work...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant