createTimer error in rules script

  • Platform information:
    • Hardware: x86_64 Intel Dual Core CPU
    • OS: Debian 9
    • Java Runtime Environment: Zulu 8.38.0.13
    • openHAB version: 2.4.0.1
  • Issue of the topic:

Hi, I am trying to test a rules script but seem to have a problem with createTimer. This is the rules script:

import org.joda.time
import org.openhab.model.script.actions.Timer

var Timer timer = null    

rule "Pump Timer1"
when
    Item Sonoff_4CH_Power4 changed to ON      
then
    sendCommand(Sonoff_4CH_Power1, ON)
    logInfo("Switch on","My switch on rule triggered on")
    timer = Timer.createTimer(now.plusSeconds(1) [|
        sendCommand(Sonoff_4CH_Power1, OFF)
        timer = null   // reset the timer
    ])
    logInfo("Switch off","My switch on rule triggered off")
end

Sonoff_4CH_Power1 does switch on but seem to stop there
I cannot locate the packages to import anywhere on the system.
I have cleared the cache as some posts have suggested. Before I reinstall OH again, maybe someone can help me with this issue. The error in the log file:

2019-06-04 12:31:47.370 [INFO ] [pse.smarthome.model.script.Switch on] - My switch on rule triggered on
2019-06-04 12:31:47.529 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Pump Timer1': 'createTimer' is not a member of 'java.lang.Class<org.eclipse.smarthome.model.script.actions.Timer>'; line 13, column 13, length 135

Many thanks in advance.

This gives away that you have started from an old tutorial or example for OH1.
Assuming you use OH2, these are not needed and indeed mess things up.

Better example

Restructuring like this will give you a better idea of how timer works

   ...
    timer = Timer.createTimer(now.plusSeconds(1) [|
        logInfo("Switch off","My timer executing")
        sendCommand(Sonoff_4CH_Power1, OFF)
        timer = null   // reset the timer
    ])
    logInfo("Switch off","My switch on rule exiting")
end

Thank you very much for your reply. I tried running the rule without the import declarations but I am still getting the same error in the logs. Seems like the Timer class is missing on my system? If I can locate or know where it should now be living then maybe I can take it from there. I should also mention that I am doing these tests offline. Will this have a negative effect?

var Timer PumpTimer = null

rule "Pump Timer On"

when
    Item Sonoff_4CH_Power4 changed to ON
then
    sendCommand(Sonoff_4CH_Power1, ON)
    logInfo("Switch on","My Pump Timer On rule triggered on")
    PumpTimer = Timer.createTimer(now.plusSeconds(5) [|
        logInfo("Switch off","My Pump Timer On rule triggered off")
        sendCommand(Sonoff_4CH_Power1, OFF)
        sendCommand(Sonoff_4CH_Power4, OFF)
        PumpTimer = null   // reset the timer
    ])

end

2019-06-05 08:54:21.151 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘default.rules’
2019-06-05 08:54:52.600 [INFO ] [pse.smarthome.model.script.Switch on] - My Pump Timer On rule triggered on
2019-06-05 08:54:52.602 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Pump Timer On’: ‘createTimer’ is not a member of ‘java.lang.Class<org.eclipse.smarthome.model.script.actions.Timer>’; line 10, column 17, length 254

Oh, I missed this. As per all the examples you’ve seen, get rid of Timer here.

PumpTimer = createTimer(now.plusSeconds(5) [|

1 Like