Simple timer rule help

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