Debouncing a Contact Sensor in OpenHAB: A Detailed Guide

Hello OpenHAB enthusiasts! I recently embarked on a project to debounce a contact sensor in OpenHAB, and I thought I’d share my journey with you. The goal was to receive an email notification only if the contact’s state changed and remained changed for a specific duration. This is crucial for avoiding false alarms, especially with sensors that might be prone to temporary fluctuations.

Initially, I wrote a rule that involved setting a timer and comparing the state after the timer elapsed. Here’s a simplified version of what I tried:

java
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

var Timer timerNeato
var OpenClosed stateNeato

rule “Neato Basisstation”
when
Item hmK1State changed from OPEN to CLOSED or Item hmK1State changed from CLOSED to OPEN
then
if(timerNeato==null || stateNeato==null) {
stateNeato = (hmK1State.state as OpenClosedType)
}
if(timerNeato!=null) {
timerNeato.cancel
timerNeato=null
}
timerNeato = createTimer(now.plusSeconds(30)) [|
if (hmK1State.state==stateNeato) {
var String Titel = "Neato "
var String Meldung = "Neato is "
if (hmK1State.state == OPEN) {
Titel = Titel + “started”
Meldung = Meldung + “started”
} else {
Titel = Titel + “back home”
Meldung = Meldung + “back home”
}
sendMail(“myemail”, Titel, Meldung)
timerNeato=null
}
]
end

The idea was to save the current state when the contact changes and restart the timer. If the state remains the same after the timer elapses, an email is sent. However, I encountered an exception during the state comparison in the timer:

java.lang.IllegalStateException: Could not invoke method: org.openhab.model.script.lib.NumberExtensions.operator_equals(org.openhab.core.types.Type,java.lang.Number) on instance: null

After some research and debugging, I realized the issue stemmed from type mismatch during the comparison. The state of the contact is of type OpenClosedType, while the comparison was trying to use Number operations. This was causing the interpreter to throw an exception.

To resolve this, I adjusted the comparison to use the appropriate type checks. Here’s the corrected version of the rule:

java
rule “Neato Basisstation”
when
Item hmK1State changed from OPEN to CLOSED or Item hmK1State changed from CLOSED to OPEN
then
if(timerNeato==null || stateNeato==null) {
stateNeato = (hmK1State.state as OpenClosedType)
}
if(timerNeato!=null) {
timerNeato.cancel
timerNeato=null
}
timerNeato = createTimer(now.plusSeconds(30)) [|
if (stateNeato == hmK1State.state) {
var String Titel = "Neato "
var String Meldung = "Neato is "
if (hmK1State.state == OPEN) {
Titel = Titel + “started”
Meldung = Meldung + “started”
} else {
Titel = Titel + “back home”
Meldung = Meldung + “back home”
}
sendMail(“myemail”, Titel, Meldung)
timerNeato=null
}
]
end

The key change was ensuring that both sides of the comparison are of type OpenClosedType. This resolved the exception and allowed the rule to function as intended.

This experience taught me the importance of careful type checking in OpenHAB scripts. It’s easy to overlook such details, especially when dealing with different data types. I hope this guide helps others avoid similar pitfalls when working with contact sensors and debouncing logic in OpenHAB.

If you have any questions or suggestions for improving this setup, feel free to reach out! Happy scripting! :blush: