Simple timer rule help

One thing to watch is that the timer spawns a new thread so if your using locks that count on a rule finishing before the next one starts (Like I did with my generator start rule) the lock will unlock before your timer has finished.

Good question.
As long as you always assign it to the same variable I would expect it to be written to the same place in the memory (at least logically). So your memory consumption should not increase over time.
If you don’t plan to cancel or re-schedule it, you don’t even need to assign it to a variable. Then it will be garbage-collected as soon as it has run (or the rules file is refreshed, or the service is restarted, of course).

But I have to admit I don’t really know all those things. I just recall some useful facts I have learned about the JVM years ago and fill the gaps with clever i-would-have-developed-it-like-this-assumptions… :slight_smile:

Just found this post via google.
Seems as if this import statement is not enough.

I had the same problem. The timer did not work.
Changing this to

import org.openhab.*

removed the red lines and everything worked fine.

1 Like

Hello All,
I tried to adapt the example to my sonoff S20. But somehow it doesn’t work.
I can’t find the error in my code. Can someone please advise:

import org.openhab.*

var Timer timer = null

rule "Steckdose Küche Timer"
    when
    	Item ki_plug1_SWITCH received command
    then
    	if(receivedCommand==ON) {
    		if(timer==null) {
    			timer = createTimer(now.plusSeconds(5)) [|
                publish("mosquitto:cmnd/sonoff_ki_plug1/POWER:OFF")
               timer = null
    			]
    		} 
    	}
    end

Thanks in Advance
Florian

If using openHAB2, imports with * are not allowed anymore. Fortunately you don’t need any imports for this rule.

1 Like

Hello Udo,
I changed the rule to the following:

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

var Timer timer = null

rule "Steckdose Küche Timer"
    when
    	Item ki_plug1_SWITCH received command
    then
    	if(receivedCommand==ON) {
    		if(timer==null) {
    			timer = createTimer(now.plusSeconds(5)) [|
                sendCommand(ki_plug1_SWITCH, OFF)
               timer = null
    			]
    		} 
    	}
    end

Still no reaction.
@kisseler used a string in a different scenario. MQTT Switching with String
Is that the solution?

Thank you
Florian

If using openHAB2, you don’t need this import to use timers:

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

What’s the item definition? Does the item work at all in any way?

Yes, I use openHAB2.

The item is configured as follows:

Switch ki_plug1_SWITCH "Küche Steckdose 1" <light> (LR,gLight)
    { mqtt=">[mosquitto:cmnd/sonoff_ki_plug1/POWER:command:*:default],<[mosquitto:stat/sonoff_ki_plug1/POWER:state:default]" }

I can switch it on and off manually on the sitemap.

Try this for your item definition. Your rule seems fine.

Switch ki_plug1_SWITCH "Küche Steckdose 1" <light> (LR,gLight) {mqtt=">[mosquitto:cmnd/sonoff_ki_plug1/power:command:ON:1],>[mosquitto:cmnd/sonoff_ki_plug1/power:command:OFF:0]"}

Still no change. Switchable throug command and UI but it doesn’t switch off automatically.

Can you try this?

var Timer timer = null

rule "Steckdose Küche Timer"
when
	Item ki_plug1_SWITCH received command ON
then
	if (timer == null) {
		timer = createTimer(now.plusSeconds(5)) [|
  		 sendCommand(ki_plug1_SWITCH, OFF)
		 timer = null
		]
	} 
end

I would suggest to add some logging:

var Timer timer = null

rule "Steckdose Küche Timer"
when
    Item ki_plug1_SWITCH received command
then
    logInfo("myrule","ki_plug1_SWITCH received command {}",receivedCommand)
    if(receivedCommand==ON) {
        logInfo("myrule","timer is {}",timer)
        if(timer==null) {
            logInfo("myrule","set timer to {}",now.plusSeconds(5).toString)
            timer = createTimer(now.plusSeconds(5)) [|
                logInfo("myrule","Switching off ki_plug1_SWITCH")
                ki_plug1_SWITCH.sendCommand(OFF)
                timer = null
            ]
        }
    }
end

Please be aware that I used the Method ki_plug1_SWITCH.sendCommand(OFF) instead of the action sendCommand(ki_plug1_SWITCH,OFF), this is due to the fact that sometimes the action will fail when in most situations the method will work.
See http://docs.openhab.org/configuration/rules-dsl.html#sendcommand-method-vs-action for details.

1 Like

Neither rule works.
@Udo_Hartmann Where is the logInfo going to?. There is nothing either in opehnab.log, events.log or mqtt.log

I tried the rule from the demo.rules and modified it, but even this doesn’t work:

rule "Steckdose Küche Timer"
    when
        Item ki_plug1_SWITCH 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(ki_plug1_SWITCH, 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 

Item configuration is still as @Saracen suggested.

If there is no log at all (in openhab.log) in question of the rule, I guess, the rule file is not recognized or there are errors in the rules file which prevent openHAB to load the rules.
What’s the name of your rules file? Did you put the file in {openhab-conf-folder}/rules/ ?

Someone else on this forum had the rules file extension set to .rule rather than .rules

Worth checking too.

When you restart openhab, do you get an log entry like this:

[INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'aussenlicht.rules'

Hello @HolgerL,
I will check this on saturday. I’m on a business trip until then. Thanks for the hint

This line seems to miss something.
As SWITCH supports two states, ON or OFF, openhab needs to know what state it needs to compare to.

No, this line is correct. You could either trigger to a distinct command (ON, OFF, STOP, UP, DOWN…) or trigger, regardless which command was received.
When doing the latter, you probably want to decide what to do by using an if clause:

if(receivedCommand==ON)

When triggered by received command, the var receivedCommand will contain the command itself.
When triggered by changed, the var previousState will contain the old state (not to mixed up with persistence! this is independent from persistence)

1 Like

Thanks Udo,
I was not aware of this possibility. This gives a whole new view.
Thnks for the info.
Rik