Delayed dark alarm

I wrote a simple code, which should change the light intensity of less than 450 and open the door, wait 2 minutes and run the buzzer for 0.5s until the door is closed. Unfortunately, somewhere it is a mistake and now it works so that after nightfall immediately activates an alarm and takes 2 minutes. Where I made a mistake?

rule "Lights Alarm"
	when 	
		Item Lights changed
	then
		if(Lights.state < 450 && Door.state==0) {
           	timer_alarm = createTimer(now.plusMinutes(2)) [| 
		sendCommand(buzzer, ON) 
		Thread::sleep(500)
		sendCommand(buzzer, OFF) 
		Thread::sleep(500)
		]
		}
end

Let’s guess that item ‘Lights’ is a light level reading or something? Once it falls to 449 your rule acts and sets up the timer. It will most likely fall to 448 pretty quickly, and so trigger the rule again (because it changed), and do the same things over again. I’ll bet you have many timers running, many ON commands in your log.

You need to find a way to act only once. A common approach is to declare your timer globally, as null to begin with. In the rule, before creating a new timer see if it is already running (i.e. not null) - and of course if it is don’t do anything else.
You’ll also need a way to reset things for the next night, perhaps clearing the timer when light > 450

When the timer does execute, it just buzzes once and finishes. Did you mean to have a while-loop in there that kept buzzing until the door was closed?

You’re right - when light levels fall below 450 rule acts and sets up the timer. This is what I mean, so it worked once but at different values ​​of light from 450 to 0. How to do it?