Help with a simple Rule to wait 5 seconds

Hi All

hoping for some help

I currently have the below rule running that turns a group of lights off when the house alarms is set only when its dark, however every so often the alarm seems to send send a false signal for 1 second then returns back to off/unset however this then stars the rule and turns the group of lights off.

My question is can it be changed so the rule will wait for maybe 5 seconds of the state change to stay set/on before running and if its changes back in less then 5 seconds/ it would cancel or not do anything

rule "Alarmset"
when
    Item F2_AlarmSet changed
then
    if(F2_AlarmSet.state == ON && SunElevation.state < 0 ) {
  gHome.sendCommand(ON)
        }
end

thank you for anyone who can help what i have tried has failed

You’re looking for timers, per the documentation.

“plusMinutes” can be “plusSeconds” (or other units of time).

See [Deprecated] Design Pattern: Motion Sensor Timer

so i have this

rule "Alarmset"
when
    Item F2_AlarmSet changed
then
    if(F2_AlarmSet.state == ON && SunElevation.state < 0 ) {

if(timer === null) {
        timer = createTimer(now.plusSeconds(30)) [|
            gHome.sendCommand(ON)
          timer = null]}
}

end

all i have achieved is a 30sec delay regardless if the state changes back within the 30sec, could you point me to what i have missed.

am sorry i am still learning

You have to cancel the timer if it exists and the AlarmSet changes to OFF.

if(F2_AlarmSet.state == OFF) {
    timer?.cancel
}
else if(F2_AlarmSet.state == ON && ...

Thank you this makes sense