Working with two window contact sensors - best practice?

Hey guys,

I have added two window/door contacts (aqara window/ door contact) to all of my windows. One of them at the top and one at the bottom of the window. The reason is, I want to know if a window is open/ closed or tilted.
The item type contact only supports the states OPEN and CLOSED, so I created the following items for every window:

// Fenster Büro rechts
String FensterBueroRechts "Fenster Büro (rechts)"

Contact FensterBueroRechts_oben {channel="deconz:openclosesensor:00212E054B9D:00158d0004a02799010006:open"}
Number FensterBueroRechts_oben_battery_level {channel="deconz:openclosesensor:00212E054B9D:00158d0004a02799010006:battery_level"}

Contact FensterBueroRechts_unten {channel="deconz:openclosesensor:00212E054B9D:00158d0002d442b2010006:open"}
Number FensterBueroRechts_unten_battery_level {channel="deconz:openclosesensor:00212E054B9D:00158d0002d442b2010006:battery_level"}

My rule looks like this:

var rulename = "fensterkontakte.rules"

rule "Fensterkontakt Büro (rechts)"
when
    Item FensterBueroRechts_unten changed or
    Item FensterBueroRechts_oben changed 
then
    logInfo(rulename, "Fensterkontakt Büro (rechts) ausgelöst")
    logInfo(rulename, "Fensterkonakt oben: "+ FensterBueroRechts_oben.state)
    logInfo(rulename, "Fensterkontakt unten: "+ FensterBueroRechts_unten.state)

    if (FensterBueroRechts_oben.state == CLOSED && FensterBueroRechts_unten.state == CLOSED) {
        logInfo(rulename, "Fensterkontakte oben und unten geschlossen.")
        logInfo(rulename, "Aktueller Stand: "+ FensterBueroRechts.state)
        FensterBueroRechts.sendCommand("Geschlossen")
        return ;
    }

    if (FensterBueroRechts_oben.state == OPEN && FensterBueroRechts_unten.state == CLOSED) {
        logInfo(rulename, "Fensterkontakte oben geöffnet.")
        logInfo(rulename, "Aktueller Stand: "+ FensterBueroRechts.state)
        FensterBueroRechts.sendCommand("Gekippt")
        return ;
    }

    if (FensterBueroRechts_oben.state == OPEN && FensterBueroRechts_unten.state == OPEN) {
        logInfo(rulename, "Fensterkontakte oben und unten geöffnet.")
        logInfo(rulename, "Aktueller Stand: "+ FensterBueroRechts.state)
        FensterBueroRechts.sendCommand("Geöffnet")
        return ; 
    }

end

This is working fine, but maybe there is a better opportunity to realize this?

Thank you :slight_smile:

That is the important thing! There is more than one way to do most things, but results are more important than methods.

I would probably structure the rule using
if ()
else if ()
else
to make a little more efficient and to ensure a known failsafe state (probably “open”).

1 Like

Thank you :slight_smile: