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.
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.