Help with timer / rules needed

Hi all.

Hopefully anyone can help me with this because I cannot find the error.

First of all I’m using OH 1.8.2

Default.rules:

import org.openhab.model.script.actions.*
import org.openhab.core.library.types.*
import java.util.*

var Timer nachf = null

rule "Ueberwachung der Laufzeit bei Nachfuellung des Schwimmbades"
when
Item G_B_EffektWeiss received command
then
if(G_B_EffektWeiss.state == ON) {
nachf = createTimer(now.plusMinutes(1)) [|
NachfuellenWarnung.postUpdate(1)
sendMail …
sendMail …
]
} else {
NachfuellenWarnung.postUpdate(0)
nachf.cancel
nachf = null
}
end

When the Switch gets on everything is fine and the timer is starting. But when the Switch gets state off the timer does not end and in the openhab.log I receive this message:

Error during the execution of rule 'Ueberwachung der Laufzeit bei Nachfuellung des Schwimmbades': The name '<XFeatureCallImplCustom>.cancel' cannot be resolved to an item or type.

What’s wrong with this.

Sorry for the quick and dirty entry. :wink:

try cancel() instead of cancel

Thomas

You should also check that nachf != null before attempting to cancel the timer.

1 Like

Thanks for your quick Response.

After trying cancel() I’m sorry to say that it did not solve the problem.

Error message now looks like:

Error during the execution of rule 'Ueberwachung der Laufzeit bei Nachfuellung des Schwimmbades': The name '<XFeatureCallImplCustom>.cancel()' cannot be resolved to an item or type.

Thanks for your quick Response.

The else-path now Looks like

    } else {
            if(nachf != null) {
                    NachfuellenWarnung.postUpdate(0)
                    nachf.cancel()
                    nachf = null
            }
    }

But I’m sorry to say that this also did not work. The error message now:

Error during the execution of rule 'Ueberwachung der Laufzeit bei Nachfuellung des Schwimmbades': The name '<XFeatureCallImplCustom> != <XNullLiteralImpl>' cannot be resolved to an item or type.

Found cancel in one of my ruels

timerCellarEntryLight?.cancel()

The ? covers the if clause. So it is canceled only if there is a timer availabe

Thomas

Did you

import org.joda.time.DateTime

?
I guess this has to be imported to use now and Timer

Yes, I tried to import org.joda.time.* before but this did not Change anything.
I also don’t think that this is the Problem because the timer itself is working without any Problem. It is the cancellation of a running timer which runs into this errors.

Not true. You do have to import org.openhab.model.script.actions.* though. That will give you Timer. now is available without importing joda, though if you want to create or save now to some variable or convert DateTimes you do need to import it.

You can try to check for hasTerminated as well.

    if(nachf != null && !nachf.hasTerminated) {
        NachfuellenWarnung.postUpdate(0)
        nachf.cancel()
    }
    nachf = null

Good evening guys.

What I found out now is that it seems not to be a problem with the .cancel command itself. If I do a variable declaration like var Timer nachf within the rule no error occurs.

That means it seems that the global variable declaration does not take affect but I don’t know why and what I can do against this.
So the reason why this errors occure is only because he does not find the Timer.

Regards,
bertl1982

If you make the declaration local to the rule but make no other changes, the else part with the cancel will never run. Every time the rule triggers nachf will be then either you create a new timer (i.e G_B_EffektWeiss.state == ON) or nachf will be null so the call to nachf.cancel never occurs. In short, unless you made other changes you are not testing the broken call at all.

Did you copy and paste your code exactly or did you retype it? Is it possible that you have a typo causing a mispelling of nachf?

The latest error that pops up once you added the if(nachf != null) clearly indicates that nachf doesn’t exist (not that it is null, it doesn’t exist at all).

Here is the actual default.rules with copy and paste:

import org.openhab.model.script.actions.*
import org.openhab.core.library.types.*
import java.util.*

var Timer NachfuellenTimer = null

rule "Ueberwachung ein"
when
        Item G_B_EffektWeiss changed to ON
then
        if(NachfuellenTimer == null) {
                logInfo("Schwimmbad.Nachfüll.Überwachung","Überwachung EIN")
                NachfuellenTimer = createTimer(now.plusMinutes(2)) [|
                        NachfuellenWarnung.postUpdate(1)
                        sendMail...
                ]
        }
end

rule "Ueberwachung aus"
when
        Item G_B_EffektWeiss changed to OFF
then
        if(NachfuellenTimer != null) {
                logInfo("Schwimmbad.Nachfüll.Überwachung","Überwachung AUS")
                NachfuellenWarnung.postUpdate(0)
                NachfuellenTimer.cancel
                NachfuellenTimer = null
        }
end

I see nothing wrong with this code. Have you loaded it into Designer? Maybe there is a syntax error that we are all not seeing.

Done that for @bertl1982 and found a unvisible typo regarding to

import org.openhab.model.script.actions.*

Maybe there is a special space or so… after I had typed the line manually, the errors disappeared :confused:

You won’t believe it but I got it. :smiley:

I found a hint for the solution at https://groups.google.com/forum/#!topic/openhab/eDxDdfXyDBE => the problem is the following line:

import java.util.*

@Kai postet there that both packages, the java.util.* and org.joda.time (which is loaded by default) hold a class called “Timer”. If both packages are loaded there is a conflict loading this class.

I changed the import to my needs like this:

import java.util.ArrayList

Now everything is working like a charm.

1 Like

Thanks to all for helping me. :wink: