Monitor garage door with boolean state

I would like to monitor my garage door with an Aqara Zigbee Window sensor.
Main issue is that I can’t find the perfect position where the sensor triggers only once when opened or closed. E.g. when closing the door it goes from OPEN to CLOSED, then back and forth a few times, and may end up on whichever state.

To navigate around this issue, I introduced a 10 second delay after the first sensor signal to ignore the noise, which works as expected:

var lastTriggerGarageVorne = now
val String filename = "Kontakte.rules"

rule "Garagentor vorne Steuerung"
    Item C_Garage_vorne changed
    if (lastTriggerGarageVorne.isBefore(now.minusSeconds(10))) {  // to prevent double-triggering
        if (C_Garage_vorne.state == OPEN) {
            logInfo(filename,"Garage vorne geöffnet.")
            lastTriggerGarageVorne = now
        } else if (C_Garage_vorne.state == CLOSED) {
            logInfo(filename,"Garage vorne geschlossen.")
            lastTriggerGarageVorne = now

And this is the Log output:

2021-01-15 15:56:40.709 [ome.event.ItemCommandEvent] - Item 'SA_Garage_vorne' received command ON
2021-01-15 15:56:40.731 [nt.ItemStatePredictedEvent] - SA_Garage_vorne predicted to become ON
2021-01-15 15:56:40.775 [vent.ItemStateChangedEvent] - SA_Garage_vorne changed from OFF to ON
2021-01-15 15:56:41.803 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from CLOSED to OPEN
2021-01-15 15:56:41.824 [INFO ] [marthome.model.script.Kontakte.rules] - Garage vorne geöffnet.
2021-01-15 15:56:41.934 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from OPEN to CLOSED
2021-01-15 15:56:42.155 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from CLOSED to OPEN
2021-01-15 15:56:42.196 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from OPEN to CLOSED
2021-01-15 15:56:42.276 [vent.ItemStateChangedEvent] - SA_Garage_vorne changed from ON to OFF
2021-01-15 15:56:42.427 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from CLOSED to OPEN
2021-01-15 15:56:42.581 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from OPEN to CLOSED
2021-01-15 15:56:42.770 [vent.ItemStateChangedEvent] - C_Garage_vorne changed from CLOSED to OPEN

Two questions:

  1. after saving the rules file, the first movement does not trigger the rule. I assume this is due to some issue with how I introduce lastTriggerGarageVorne? After the first movement, when lastTriggerGarageVorne has been set once by the rule, all works fine.

  2. Sometimes, the sensor overshoots. E.g. when closing, it goes OPEN -> CLOSE -> OPEN. In my rule, the next movement (OPEN -> CLOSE -> OPEN) would trigger “Garage door closed” again. Also, the state of the door in my sitemap is wrong in this case.
    I tried to introduce a boolean state GarageVorneOffen that would be set along with the first sensor signal and also ignore the noise for 10 seconds.
    I added
    var GarageVorneOffen = new Boolean("true")
    GarageVorneOffen = true
    GarageVorneOffen = false
    in tht respective parts of the if/else clause. How do I best monitor the state of this variable?

openHAB 2.5.11

  1. set lastTriggerGarageVorne to something a long time into the past, like now.minusMinutes(1). That will ensure that it will be before now even if the rule is triggered immediately after the rule triggers.

Alternately, initialize the variable to null and then add to the if a check to see if it’s is null or ten seconds ago. You’ll have to change the definition of lastTriggerGarageVorne to var DateTime lastTriggerGarageVorne = null.

  1. It really sounds like this is not a good sensor to use for this use. If you can’t trust that the state reported by the sensor is correct (i.e. when the sensor says CLOSED you know the door is closed) there will always be some edge cases caused by timing that will cause your rule to become out of sync with the actual door. I would strongly suggest looking for an alternative sensor. But in answer to your specific question, you’ll need to put that into an Item, not a variable.
1 Like

Chiming in on Richs advice to check/replace the sensor. How did. you install it? I have eight of them and they are very reliable!

1 Like