I’ve been working on optimizing my mailbox sensor setup to reduce unnecessary notifications caused by vibrations. The sensitivity of the sensor is quite high, and even minor vibrations from nearby activities trigger alerts. I wanted to implement a delay mechanism to ensure that the system only sends an email if the mailbox remains closed for a continuous period, say 30 seconds.
Here’s the script I’ve been using, but it doesn’t seem to function as expected:
javascript
var Timer mailboxTimer = null;
if(Mailbox_Contact.state == CLOSED){
mailboxTimer = createTimer(now.plusSeconds(30)) [|
val mailActions = getActions(“mail”,“mail:smtp:dd7055cab0”)
mailActions.sendMail(“XXXX@xxx.com”,“Openhab”,“There is something in the mailbox”)
]
}else{
mailboxTimer.cancel;
mailboxTimer = null;
}
My goal was for the script to reset or cancel the timer if the contact opens within the 30-second window. However, it sends an email every time the contact closes, regardless of how brief the closure is. I suspect the issue lies in how the timer is being managed within the script. If anyone has experience with similar setups or can spot what’s wrong with the code, I’d greatly appreciate the insight!
This has been a bit of a learning curve for me, especially with the programming language involved, but I’m determined to get it working smoothly. Thanks in advance for any help!