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).
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.
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
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:
This way, it’s much easier to comprehend the purpose of the timer - even after returning to your Code after some days/week/months/…
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.