Timer Rule Problem

import org.openhab.model.script.actions.Timer //this import is only needed in openHAB1

var Timer delayTimer = null
val int timeoutMinutes = 30  //timeoutMinutes wasn't defined yet!

rule "Nilan Humidity OFF Forseret"
when
    Item Nilan_Input_RH changed
then
    if(delayTimer == null) {
        delayTimer = createTimer(now.plusSeconds(30), [|
            Nilan_Control_VentSet.sendCommand(2)
            Smarthouse_Motorventil.sendCommand(OFF)
            delayTimer = null 
        ])
    }
    else {
        delayTimer.reschedule(now.plusSeconds(timeoutMinutes))
    }
end

should also work. In fact, it’s way better to use the methods [Item.sendCommand(value)] instead of actions [sendCommand(item,value)] (see sendCommand() Documentation and http://docs.openhab.org/configuration/rules-dsl.html#sendcommand-method-vs-action)
It’s always a good Idea to check if the timer is initialized.
I don’t understand the difference between createTimer(int) [| ] and createTimer(int,[| ]) but in the end both are ok.

The error in first place was the Timer. before createTimer, this is not allowed in the rules DSL.

1 Like