[SOLVED] Timer for an item switch in rule error

Hi,
I have a Xiaomi Aqara Wall Switch with two channels for opening and closing a skylight window. The problem is that it doesn’t turn off by itself and I would like to know if it’s possible to setup a timer in a rule for each time that it’s on. Something like turn on and after 7 seconds turn off. I tried the rule that I post below but I have an error. Thank you very much!:slight_smile:

Platform information:

Hardware: Raspberry PI 3B
OS: openHABian v1.4.1
Java Runtime Environment: openjdk version “1.8.0_152”
OpenJDK Runtime Environment (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8 .0_152-b76)
OpenJDK Client VM (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 25.152-b76, m ixed mode, Evaluation)
openHAB version: openHAB 2

Issue of the topic: Timer in rule error

  • Items configuration related to the issue
    // Llave apertura claraboya baño principal

Switch LlaveAperturaClaraboyaBaOPrincipal_Button1 “Está[MAP(luces.map):%s]” <switch> (Home, FF_PrimeraPlanta, FF_BanoPrincipal, FF_MotorClaraboyaBanoPrincipal) [ “Switchable” ] { channel=“mihome:ctrl_neutral2:158dXXXXXX:ch1” }

Switch LlaveAperturaClaraboyaBaOPrincipal_Button2 “Está[MAP(luces.map):%s]” <switch> (Home, FF_PrimeraPlanta, FF_BanoPrincipal, FF_MotorClaraboyaBanoPrincipal) [ “Switchable” ] { channel=“mihome:ctrl_neutral2:158dXXXXXX:ch2” }

  • Sitemap configuration related to the issue
    Group item=FF_MotorClaraboyaBanoPrincipal
    {
    Frame label=“Subir”
    Switch item=LlaveAperturaClaraboyaBaOPrincipal_Button1
    Frame label=“Bajar”
    Switch item=LlaveAperturaClaraboyaBaOPrincipal_Button2
  • Rules code related to the issue
    var Timer timer = null

rule “Tiempo apertura claraboya - Abrir”
when
Item LlaveAperturaClaraboyaBaOPrincipal_Button1 changed to ON
then
timer = createTimer(now.plusSeconds(5) [|
sendCommand(LlaveAperturaClaraboyaBaOPrincipal_Button1, OFF)
timer = null // reset the timer
])
end

  • If logs where generated please post these here using code fences:
    [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Tiempo apertura claraboya - Abrir’: An error occurred during the script execution: index=1, size=1

You could use the EXPIRE binding and then add it to the item, then you could eliminate using a rule.

See my garage door item below which automatically sends the OFF command after 1 second.

Switch RtGarageDr "Right Garage Door" [ "Switchable" ] { channel="zwave:device: xxxxxx:node6:switch_binary",expire="1s,command=OFF",autoupdate="false" }

Squid

I tried changing my items:
Switch LlaveAperturaClaraboyaBaOPrincipal_Button1 “Está[MAP(luces.map):%s]” <switch> (Home, FF_PrimeraPlanta, FF_BanoPrincipal, FF_MotorClaraboyaBanoPrincipal) [ “Switchable” ] { channel=“mihome:ctrl_neutral2:158dXXXXX:ch1”,expire=“7s,command=OFF”,autoupdate=“false” }
Switch LlaveAperturaClaraboyaBaOPrincipal_Button2 “Está[MAP(luces.map):%s]” <switch> (Home, FF_PrimeraPlanta, FF_BanoPrincipal, FF_MotorClaraboyaBanoPrincipal) [ “Switchable” ] { channel=“mihome:ctrl_neutral2:158dXXXXX:ch2”,expire=“7s,command=OFF”,autoupdate=“false” }
…but nothing happens! Is it OK? Thank you very much!:slight_smile:

Did you install the binding? EXPIRE is not installed by default…use PAPERUi to install it.

Also, please use code fences around your code so that others can easily read and review.

Switch LlaveAperturaClaraboyaBaOPrincipal_Button1 “Está[MAP(luces.map):%s]” <switch> (Home, FF_PrimeraPlanta, FF_BanoPrincipal, FF_MotorClaraboyaBanoPrincipal) [ “Switchable” ] { channel=“mihome:ctrl_neutral2:158dXXXXX:ch1”,expire=“7s,command=OFF”,autoupdate=“false” }
Switch LlaveAperturaClaraboyaBaOPrincipal_Button2 “Está[MAP(luces.map):%s]” <switch> (Home, FF_PrimeraPlanta, FF_BanoPrincipal, FF_MotorClaraboyaBanoPrincipal) [ “Switchable” ] { channel=“mihome:ctrl_neutral2:158dXXXXX:ch2”,expire=“7s,command=OFF”,autoupdate=“false” }

Squid

1 Like

After install the binding EXPIRE works. I didn’t know this binding. It works perfectly. By the way, thank you very much for everything and I will follow your advice and next time I will use them (code fences - csv.......) :slight_smile:

You original rule was almost right

Also, use the .sendCommand method instead of the sendCommand() action
You were missing a comma in your timer syntax

rule “Tiempo apertura claraboya - Abrir”
when
    Item LlaveAperturaClaraboyaBaOPrincipal_Button1 changed to ON
then
    createTimer(now.plusSeconds(5), [ |
        LlaveAperturaClaraboyaBaOPrincipal_Button1.sendCommand(OFF)
        timer = null // reset the timer
    ])
end