Doorbell Using Aoetec Dry Contact Sensor

import org.eclipse.smarthome.core.library.types.DateTimeType

var String BellRung = "off"

rule "FrontDoorBell"
when
	Item FrontDoorBell_BinarySensor changed to ON
	then
		if (BellRung == "off")	 
    	
    	{
    	BellRung = "on"	
    	playSound("Westminster.mp3")
    	postUpdate(DoorBellTime, new DateTimeType())
    	sendMail("YourPhoneNumber@vtext.com", DoorBellTime.state.format("%tR") +" "+ DoorBellTime.state.format("%tD"),"Front Door Bell!!!")
	  	Thread::sleep(5000)
    	BellRung = "off"
    	}
 end 

FrontDoorBell_BinarySensor is the Aoetec Dry contact.
BellRung is set to “on” when the door bell is pressed, playing my doorbell sound, and then sending me a text that the doorbell has been rung, then I sleep for 5 seconds, making my test up top false for 5 seconds, which keeps someone from pressing the doorbell multiple times, and playing your doorbell sound overlayed one on top of the other. the sleep should be just a little longer than your door bell sound. After that BellRung is set back to “off”, making the doorbell ready to be rung again.

Experts, could I have used a timer instead of a sleep??? I ask this, as I understand a sleep ties up a thread for the time of the sleep, and 10 seconds in the life of openHab is a long time.

To answer my own, question, it looks like I can use a timer here, I will work on the new rule, and publish it when I have it completed.

Not really. In a typical home automation scenario, you are very unlikely to run out of running threads.

A Timer or the Expire Binding (my preferred choice) would also work. But I wouldn’t choose either one of those only because you are worried about performance or tying up a thread for ten seconds. I’d only worry about using Thread::sleep if

  • you have a particularly large installation with four to five rules triggering every couple of seconds
  • the sleep is for minutes or longer

To use the Expire binding the Item and Rule will look like:

Switch BellRung { expire="10s,command=OFF" }
rule "FrontDoorBell"
when
    Item FrontDoorBell_BinarySensor changed to ON
then
    if(BellRung.state == OFF){
        BellRung.sendCommand(ON)
        playSound("Westminster.mp3")
        DoorBellTime.postUpdate(new DatetimeType)
        sendMail("YourPhoneNumber@vtext.com", DoorBellTime.state.format("%tR") +" "+ DoorBellTime.state.format("%tD"),"Front Door Bell!!!")
    }
end

The Expire Binding will set BellRung to OFF after ten seconds. No need for the bookkeeping required with Timers.

I don’t have any of the issues mentioned, but, this looks like a much cleaner way of doing things.

Looks like I need an Items file? (which isn’t a problem)

…or can I define an item like that through PaperUI/Habmin somehow???

I don’t think you can define Items that use Expire binding through PaperUI yet.

1 Like

Didn’t think so either, but, you knew about the Expire binding…:joy: