# Using Timer Class in Rule Making

**URL:** <https://community.openhab.org/t/using-timer-class-in-rule-making/35080>\
**Category:** Scripts & Rules\
**Created:** [October 13, 2017, 4:54am UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080 "2017-10-13T04:54:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaaaron](https://community.openhab.org/user_avatar/community.openhab.org/aaaaron/32/68672_2.png) [@Aaaaron](https://community.openhab.org/u/Aaaaron)\
**Post date:** [October 13, 2017, 4:54am UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/1 "2017-10-13T04:54:40Z")

</div>

So I’m working on a pretty simple rule file which simply operates a relay attached to the garage door switch as a momentary switch, and then turns on the overhead light.

My problem is with timers, and specifically, how to get them working. The second rule I have is the one that doesn’t work, and what it is doing is attempting to call a timer that will turn off the overhead light after a set time (currently 3 seconds for getting it working, I’ll change it to a few minutes after I get it working).

```auto
import org.joda.DateTime
import org.openhab.model.script.actions.Timer

var Timer LightTimer = null

rule "Open Garage Door"
when
  Item CH4 changed from OFF to ON
then
   Thread::sleep(500)	
   CH4.sendCommand(OFF)
end

rule "Turn on light with garage door"
when
  Item CH4 changed from ON to OFF
then
  CH5.sendCommand(ON)
  LightTimer = Timer.createTimer(now.plusSeconds(3)) [|
        CH5.sendCommand(OFF)]
        LightTimer = null // reset the timer
end

```

I’m pretty sure my issue lies with importing the Timer Class, as the compiler flags the first line:

_“org.joda.DateTime cannot be resolved as a type”_,

and the second line:

_“org.openhab.model.script.actions.Timer cannot be resolved as a type”_

consequently, the compiler also doesn’t understand the createTimer method. So I’m pretty sure the createTime class isn’t being imported.

So can anyone help me? I’m not exactly a programming expert, although I’ve been able to muddle my way through most of this. I’m thinking I’m missing a library maybe? Or maybe it’s something else entirely. All help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Olymp](https://community.openhab.org/user_avatar/community.openhab.org/olymp/32/5553_2.png) [@Olymp](https://community.openhab.org/u/Olymp)\
**Post date:** [October 13, 2017, 7:30am UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/2 "2017-10-13T07:30:11Z")

</div>

What version is OpenHAB?  
for me (openHAB 2.2.0 Build #1060) works like this:

```
var Timer LightTimer
rule "Turn on light with garage door"
when
  Item CH4 changed from ON to OFF
then
  CH5.sendCommand(ON)
  LightTimer = Timer.createTimer(now.plusSeconds(3)) [|
        CH5.sendCommand(OFF)
        LightTimer = null // reset the timer
  ] 
end

```

---

<div class="post-metadata">

**Author:** ![HomeAutomation](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@HomeAutomation](https://community.openhab.org/u/HomeAutomation)\
**Post date:** [October 13, 2017, 7:51am UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/3 "2017-10-13T07:51:22Z")

</div>

Just a little question here.

Is there a difference in the two rules in the functionallity. The position of the “]” is different.

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [October 13, 2017, 8:04am UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/4 "2017-10-13T08:04:26Z")

</div>

as for the timer Syntax, mine _were_ like Olymp wrote (make sure to place the ] after resetting the timer).

**alternative approach: using expire binding instead of timer**  
a small hint, if you’re thinking of using more timers, use expire functionality…  
I replaced my timers with an easier to read and implement design pattern using the expire binding:

> [@Design Pattern: Expire Binding Based Timers](https://community.openhab.org/t/design-pattern-expire-binding-based-timers/32634):
>
> Problem Statement One often needs to schedule some code to execute at some time in the future. The “usual” way to achieve this is by using a Timer which lets you create and schedule a lambda to execute at a given date and time. However, Timers have their drawbacks including: the need to keep track of open Timers so they can be canceled at a later date, often requiring the use of HashMaps or some other data structure rescheduling or creating a cascade of Timers is challenging (i.e. when a tim…

This way, it’s much easier to comprehend the purpose of the timer - even after returning to your Code after some days/week/months/… 😉

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 13, 2017, 3:13pm UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/5 "2017-10-13T15:13:01Z")

</div>

> [@Aaaaron](#):
>
> I’m pretty sure my issue lies with importing the Timer Class, as the compiler flags the first line:

If you are running on OH 2.x, you do not import anything from Joda nor do you import anything from org.openhab nor anything from org.eclipse.smarthome. All of those are imported for you now. Furthermore, in OH 2, Timer would be in something like org.eclipse.smarthome.model.script.actions.Timer.

The import of a Timer class that doesn’t exist could be causing you problems.

And as already mentioned, your closing `]` is misplaced.

---

<div class="post-metadata">

**Author:** ![Aaaaron](https://community.openhab.org/user_avatar/community.openhab.org/aaaaron/32/68672_2.png) [@Aaaaron](https://community.openhab.org/u/Aaaaron)\
**Post date:** [October 13, 2017, 3:15pm UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/6 "2017-10-13T15:15:25Z")

</div>

Thanks for the quick responses everyone!

---

<div class="post-metadata">

**Author:** ![Udo\_Hartmann](https://community.openhab.org/user_avatar/community.openhab.org/udo_hartmann/32/70871_2.png) [@Udo\_Hartmann](https://community.openhab.org/u/Udo_Hartmann)\
**Post date:** [October 13, 2017, 3:15pm UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/7 "2017-10-13T15:15:51Z")

</div>

…and neither the key word is needed.

```auto
LightTimer = createTimer(now.plusSeconds(3)) [|

```

in OH2 you don’t need neither

```auto
import org.joda.DateTime

```

nor

```auto
import org.openhab.model.script.actions.Timer

```

---

<div class="post-metadata">

**Author:** ![Aaaaron](https://community.openhab.org/user_avatar/community.openhab.org/aaaaron/32/68672_2.png) [@Aaaaron](https://community.openhab.org/u/Aaaaron)\
**Post date:** [October 13, 2017, 3:32pm UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/8 "2017-10-13T15:32:29Z")

</div>

Thank you sir; you identified all my mistakes, and now it works perfectly. Most thanks.

I tried pretty hard before posting my first plea for help, and I knew when it happened it would be a piece of cake for someone!

---

<div class="post-metadata">

**Author:** ![Udo\_Hartmann](https://community.openhab.org/user_avatar/community.openhab.org/udo_hartmann/32/70871_2.png) [@Udo\_Hartmann](https://community.openhab.org/u/Udo_Hartmann)\
**Post date:** [October 13, 2017, 3:33pm UTC](https://community.openhab.org/t/using-timer-class-in-rule-making/35080/9 "2017-10-13T15:33:00Z")

</div>

You’re welcome 😉
