[SOLVED] How do I use a user-set timer in a rule to send a command?

Hello all,

I have a question as to how to work my idea out. I am using my KlikAanKlikUit poweroutlet switches with OpenHab and I love it. When my phone battery is reaching the charged capacity of 95%, Tasker sends an OFF command to OpenHab and the switch (and thus the phone charger) switches off. Good for the battery, great for preventing fires due to overcharging.

Now, most devices I charge don´t have the luxury of telling OpenHab what to do, e.g. a Nintendo 3DS. In order to prevent overcharging, I was expirimenting with a user-set timer. The timer would be set in the sitemap. This timer would run while the device is charging and switch off the charger when the timer runs out, preventing the device from charging any further.

Unfortunately, I did not have any luck getting my idea to work. I used a setpoint in my sitemap to set the time, the problem that rose was that I did not know how to ´transform´ this setpoint to a useable variabele.

used information
Searching the forum I came across the following information:


Does anyone have any idea or leads for me how to work this idea out? Thanks in advance!

Kind regards,

Ok let’s say that you have two items:

Switch MySwitch
Number MySwitchTimer

Then the rule would be as follow:

var Timer timer = null //Global variable

rule "MySwitch turned ON"
    Item MySwitch changed to ON
    val timerDuration = (MySwitchTimer.state as Number).intValue // transforms the Timer item value into an integer as timers only accept integers as arguments
    if (timer === null) { // If the timer is not running already
        timer = createTimer(now.plusMinutes(timerDuration), [ | // Creates a routine that will run a the time of now plus timerDuration minutes
            // Timer routine starts
            mySwitch.sendCommand(OFF)  // Turns the switch off
            timer = null  // cancel the timer
            // Timer routine ends
1 Like

I use the expire binding to set a timer in some of my rules. Here is an example that turns off a light switch 3 minutes after last motion is detected. New motion resets the timer.

Switch BasementTimer { expire="3m,command=OFF" }


rule "Basement Light On"
    Item basement_motion_1 changed from OFF to ON or Item basement_motion_2 chan
ged from OFF to ON
    // start or reset Timer

rule "Basement Light Off"
    Item BasementTimer changed from ON to OFF

The problem with the expire binding tis that you can’t change the timer duration at run time

You’re the idea man… :smiley:

Thank you for the code. I implemented it but unfortunatley I get two errors.
The ChargerSwitchTimer.state gives the following error: ¨The method or field State is undefined for the type NumberItem¨

The timer inside the if statement gives the same error ¨the method or field timer is undefinded¨.

Do you happen to know what could resolve these errors?

My (euhrm…your…hehe:relaxed:) rule:

rule "ChargerSwitchTimer"
    Item MySwitch changed to ON
    val timerDuration = (ChargerSwitchTimer.State as Number).intValue
    if (timer === null) {
        timer = createTimer(now.plusMinutes(timerDuration), [ |
            timer = null

The items:

Switch ChargerSwitch
Number ChargerSwitchTimer
1 Like

Sorry state not State apologies
Typo corrected above


It is working perfectly, thanks for all the replys and thanks @vzorglub for the solution! :smile:

You are welcome.
Just make sure you read the code that you copied and pasted and understand it
This use of timers is very, very useful and you will need it again.
Anything not clear just ask.
I have added comments to the code

Have a look at:

for more uses of timers