Timer Cancel Problem

Hi,
here is my Rule:

var Timer myTimer_Roll_kueche = null

     if (r_kueche.state == 100) {
        if (myTimer_Roll_kueche !== null) {
           logInfo("rules", "Timer rescheduled")
           // myTimer_Roll_kueche.reschedule(now.plusSeconds(10))
           myTimer_Roll_kueche.cancel()
            myTimer_Roll_kueche = null
        } else {
            myTimer_Roll_kueche = createTimer(now.plusSeconds(10), [ |
                logInfo("rules", "Timer activated")
                //Do something...
                                                       
                SteckdoseKueche.sendCommand(OFF)
            ])
            logInfo("rules", "Timer created")
        }
    } else {
        logInfo("rules", "Timer canceled")
         if (myTimer_Roll_kueche !== null){ 
            myTimer_Roll_kueche.cancel()
            myTimer_Roll_kueche = null
           }
       SteckdoseKueche.sendCommand(ON)
    }​

The Problem is that the Timer Cancel didn´t work.
Any Ideas ?
Regards
Jörg

Don’t think we’re seeing the complete rule.
But -

Every time that runs, it destroys the “handle” to any existing Timer. The timer continues, but you can’t communicate with it because you burnt your only lifeline.

@rossko57 Thanks for your advice…
The Rule fires if r_kueche.state changed

My Rule now looks like this

var Timer myTimer_Roll_kueche

     if (r_kueche.state == 100) {
        if (myTimer_Roll_kueche !== null) {
           logInfo("rules", "Timer rescheduled")
            myTimer_Roll_kueche.reschedule(now.plusSeconds(10))
        } else {
            myTimer_Roll_kueche = createTimer(now.plusSeconds(10), [ |
                logInfo("rules", "Timer activated")
                //Do something...
                                                       
                SteckdoseKueche.sendCommand(OFF)
            ])
            logInfo("rules", "Timer created")
        }
    } else {
       SteckdoseKueche.sendCommand(ON)
       logInfo("rules", "Timer canceled")
         if (myTimer_Roll_kueche !== null){ 
            myTimer_Roll_kueche.cancel()
            myTimer_Roll_kueche = null
           }
       
    }


But still the same, when the shutter r_kueche.state is 100 the log says Timer created, then I open the shutter again after 5 seconds and the log says timer canceled …but after the 10 seconds the log says
Timer activated and the SteckdoseKueche goes OFF

I will guess this is a UI entered rule.
You can use the code tab to show the complete rule, with triggers and conditions, in order to show us. This stuff does matter.

There are no"global" variables in UI entered rules. Every time your rule triggers, it destroys your only pointer to any existing timer.

This is a technique used in files-based DSL rules that you cannot use in GUI entered DSL rules. There is no workaround. You may use files based rules or you may use a different scripting language in GUI, most of which offer a version of variables surviving between runs of the same rule.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.