Delay after switching

Is there any way to control the switcing “timing”?
What I want to do is when I turn on a light (actually a high voltage reflector) I want to give it for example 2 minutes to keep it ON, and after that anyone can swith it OFF. So that way I can prevent the light being swithed multiple times per seconds, which would kill it.
I want to set a minimum time intervall when you can use the switch.
(They are controlled trough MQTT -> wifi -> aruino, by the way)

I would set up one of @bob_dickenson 's patented proxy Items and then use a rule to control actually sending the command to the real switch.

This code assumes you have persistence setup so the call to lastUpdate works.

Items:

Switch LightProxy "nice label to use on sitemap" <icon> (groups) // no binding on purpose
Switch Light { mqtt ... // this is never to be put on the sitemap

Rules:

// Included here in case you want to control how long it has to be off before you turn it back on
rule "Turn on light"
when
    Item LightProxy received command ON
then
    if(Light.state != ON) light.sendCommand(ON) // no need to turn it on if it is already on
end

rule "Turn off light"
when
    Item LightProxy received command OFF
then
    if(Light.state != OFF && Light.lastUpdate.before(new java.util.Date(now.minusMinutes(2).millis))){
        Light.sendCommand(OFF)
    }
end

O, I will try it this afternoon! :))

It does not work. :confused:
I can still switch any time, even 5 times per second.

Add some logging to the rule. See if the lastUpdate part of the if statement is working correctly.

Make double sure that you only put the Proxy item on your sitemap, not the item bound to the MQTT topic.

Note: Additional logic is required to keep the Proxy Item in an off state in the case that it did not send the command to the MQTT Item. So if all you are doing it looking at the sitemap UI, and you have double checked that the Item on the sitemap is the Proxy and not the MQTT Item, then when you switch it ON and OFF it will switch no matter how much time has passed and appear on the sitemap as being ON or OFF no matter how fast you switch it. But it will only send OFF command to the MQTT Item if it has been ON for two minutes or more.

I got it! Do I have to import something “timer” jar script/addon on the first line in rules?

There is no “org.openhab.model.script.actions.Timer”
as shown here: demo scripts

I try to get some log.

Yes, You need to import org.openhab.model.script.actions.*

I think, you suggested me a very good solution, even I can’t solve it on your way :smile:
I started to play with “visibility” options to hide the swithOff button for 2 minutes.

So I did it this way. Items:

Switch reflektor1 "Reflektor #1" (all) {mqtt=">[broker:/home/p3/com:command:on:ON],>[broker:/home/p3/com:command:off:OFF],<[broker:/home/p3/com/state:state:default"}
String reflektor1state "Reflektor #1 [%s]" <energy>  {mqtt="<[broker:/home/1/ard1/refl1/com/state:state:default]"}

and sitemap

Switch item=reflektor1 label="Reflektor #1" visibility=[reflektor1state==allowed]
Text item=reflektor1state label="Reflektor #1 [%s]" visibility=[reflektor1state!=allowed]

rules:

import org.openhab.model.script.actions.*
var Timer timer
rule "deny to change state for 10 secs"
when
    Item reflektor1 changed
then
    sendCommand(reflektor1state, "denied")
    timer = createTimer(now.plusSeconds(10))[|
    sendCommand(reflektor1state, "allowed")
    timer.cancel
    timer = null
end

It might be a little “cheap and poor” solution, but what I need is all are in that segment.
You probably can write better lines, but this is OK with me (for a while, until I got something else in my mind, something new to explore)
Thank you rlkoshak!!

Don’t know if it is a typo or actually exists in your real rule, but you need to close out that timer lambda.

timer = createTimer(now.plusSeconds(10))[|
    sendCommand(reflektor1state, "allowed")
    timer.cancel
    timer = null
] // you are missing this line

That was just a typo. Thanks!
It is working flawlessly!