openhab-distro icon indicating copy to clipboard operation
openhab-distro copied to clipboard

Example timer rule

Open mharvan opened this issue 5 years ago • 1 comments

In the example rule with timer I suggest to remove the "nulling" of the timer. That way, just a single timer would be created and reused (rescheduled) throughout the lifetime of the rule. With the "nulling", a new timer is created whenever the light is turned on.

In more detail, the suggestion is to remove the line

            timer = null

in

rule "Timer Demo"
when
    Item Light_GF_Corridor_Ceiling received command
then
    if (receivedCommand == ON) {
        if (timer === null) {
            // first ON command, so create a timer to turn the light off again
            timer = createTimer(now.plusSeconds(10)) [|
                sendCommand(Light_GF_Corridor_Ceiling, OFF)
            ]
        } else {
            // subsequent ON command, so reschedule the existing timer
            timer.reschedule(now.plusSeconds(10))
        }
    } else if (receivedCommand == OFF) {
        // remove any previously scheduled timer
        if (timer !== null) {
            timer.cancel
            timer = null
        }
    }
end

Link to the line: https://github.com/openhab/openhab-distro/blob/6805d0b8840dc10f15a30a9d544c683e395b5a31/features/distro-resources/src/main/resources/rules/demo.rules#L48

mharvan avatar Jul 30 '20 07:07 mharvan

What is the benefit of not nulling the timer?

Because if you will use the switch from time to time and not regularly than there is always some variables left in the space. And with null the variable will be removed from the java working space

miloit avatar Aug 18 '20 06:08 miloit