How to get my first rule running

Look at events.log to see if Ga_Bewegung is changing. Add logging to your Rule to see if the Rule is running.

Note that the Rule will only trigger if the Item changes from OFF to ON. Not all motion sensors do that. Some just get an ON for every motion detection and never return to OFF.

Thanks for reply! Do i log with "“logDebug” or “logInfo”?

First, look on events.log for your Item changing in the way you expect.
No change, no rule trigger, no point fiddling with rule that never runs.

1 Like

Follow rossko57’s advice. Then use logInfo for now. When you are done, you can convert them to logDebug which will keep the statements but they won’t show up in your openhab.log unless you lower the logging level of your Rules to debug.

1 Like

Thanks! In openhab.log i get following error:

2019-11-14 19:13:24.389 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Licht an bei Bewegungsmelder’: ‘createTimer’ is not a member of ‘java.lang.Class<org.eclipse.smarthome.model.script.actions.Timer>’; line 6, column 9, length 92

**My rule:**

rule "Licht an bei Bewegungsmelder"

when

Item Motion_Garderobe changed to ON

then

sendCommand(Licht_Wohnzimmer, ON)

timer = Timer.createTimer(now.plusSeconds(180) [|

sendCommand(Licht_Wohnzimmer, OFF)

timer = null

])

end

OK, the error is pretty clear. I points to the line

timer = Timer.createTimer(now.plusSeconds(180) [|

The error says “createTimer is not a member of …”

So there is something wrong with the call to createTimer. Let’s look at the docs for createTimer…

For example:

var Timer myTimer = createTimer(now.plusMinutes(5), [ |
   logInfo("rules", "Timer activated")
])

Notice, there is no Timer. before the call to createTimer.

NOTE: I’m not trying to be pedantic. I want to demonstrate the sort of thought process that should go through your mind and the actions you should take when you encounter an error.

NOTE2: If you were using VSCode with the openHAB extension that line would have been highlighted as an error.

1 Like

I am sorry for beeing exhausting, but i am very new to this all. Now i have recreated the rule and it seems to work, but not 100%, any issues are still there.

    • After detecting movement the light turns on = OK
    • After a few seconds it seems the light turns again on = NOT OK
    • After 2 minutes the light turns off = OK

openhab.log

2019-11-15 09:51:39.176 [INFO ] [.eclipse.smarthome.model.script.FILE] - Setting to ON and creating timer

2019-11-15 09:52:09.801 [INFO ] [.eclipse.smarthome.model.script.FILE] - Setting to ON and creating timer

2019-11-15 09:53:39.187 [INFO ] [.eclipse.smarthome.model.script.FILE] - Timer expired and setting to OFF

2019-11-15 09:54:09.817 [INFO ] [.eclipse.smarthome.model.script.FILE] - Timer expired and setting to OFF

rule

var Timer timer = null
rule "Beleuchtung Wohnzimmer an, für 2 Minuten"
when
Item Motion_Garderobe changed
then
    logInfo("FILE", "Setting to ON and creating timer")
    Licht_Wohnzimmer.sendCommand(ON)
    timer = createTimer(now.plusMinutes(2), [|
        logInfo("FILE", "Timer expired and setting to OFF")
        Motion_Garderobe.postUpdate(OFF)
        Licht_Wohnzimmer.sendCommand(OFF)
        //timer = null // DO I NEED THIS??
    ])
end

Thanks for helping!

You changed that line. Look back at post 25 for what it used to be

Not unless you are testing to see if the timer is null anywhere i.e. looking to see if it is already running.
In the rule you show us, you don’t do that - but you probably should. What happens if you get a second motion trigger after one minute - what should your timer do?

1 Like

Thanks! That means i have to change it to: Item Motion_Garderobe changed to ON?

Well, when would like your timer to begin? When motion starts, or when it stops? What changes does your Item make? (see events.log)

Personally, I’m a fan of a two-rule solution that other people seem to avoid.
When motion begins, turn on the light - and cancel any off-timer.
When motion stops, begin the light off-timer.

1 Like

I´d like to start the timer if first movement is detected and on a second movement the timer should extend. What do you think about it? Thanks!

events.log

2019-11-15 13:10:53.793 [vent.ItemStateChangedEvent] - Motion_Garderobe changed from OFF to ON

2019-11-15 13:10:54.036 [ome.event.ItemCommandEvent] - Item 'Licht_Garderobe' received command ON

2019-11-15 13:10:54.047 [nt.ItemStatePredictedEvent] - Licht_Garderobe predicted to become ON

2019-11-15 13:10:54.049 [vent.ItemStateChangedEvent] - Licht_Garderobe changed from OFF to ON

2019-11-15 13:11:24.346 [vent.ItemStateChangedEvent] - Motion_Garderobe changed from ON to OFF

2019-11-15 13:11:24.351 [ome.event.ItemCommandEvent] - Item 'Licht_Garderobe' received command ON

2019-11-15 13:11:24.361 [nt.ItemStatePredictedEvent] - Licht_Garderobe predicted to become ON

2019-11-15 13:12:54.050 [ome.event.ItemCommandEvent] - Item 'Licht_Garderobe' received command OFF

2019-11-15 13:12:54.074 [nt.ItemStatePredictedEvent] - Licht_Garderobe predicted to become OFF

2019-11-15 13:12:54.076 [vent.ItemStateChangedEvent] - Licht_Garderobe changed from ON to OFF

Have a go at that, then.

1 Like

Is that the right way?

var Timer timer = null

rule "Beleuchtung Garderobe, für 5 Minuten"
when
Item Motion_Garderobe changed to ON

then
if(timer === null)  {
    logInfo("FILE", "Setting to ON and creating timer")
    Licht_Garderobe.sendCommand(ON)
    timer = createTimer(now.plusMinutes(5), [|
        logInfo("FILE", "Timer expired and setting to OFF")
        Motion_Garderobe.postUpdate(OFF)
        Licht_Garderobe.sendCommand(OFF)
        timer = null
    ])
} else {
    logInfo("FILE", "Timer rescheduled")
    timer.reschedule(now.plusMinutes(5))
    
}
end

Seems reasonable, how does it work out for you?