Rule optimization: Window OPEN reminder

Hi,

whit the help of this Thread from @rlkoshak i build this:

Contact     FHT_Bad_Fenster 			            "Bad Fenster[MAP(fht_fenster.map):%s]"  				                <iftswindow1w>          (FHT, FHTfenster)
DateTime    FHT_Bad_Fenster_LastUpdate              "Bad Fenster letztes Update [%1$tm.%1$td.%1$tY %1$tH:%1$tM]"            <itimeclock>            (FHT, FHTfensterUpdate)
Switch      FHT_Bad_Fenster_Timer                   "Bad Fenster Timer"                                                     <itimetimer>            (FHT, FHTfensterTimer)   {expire="15m,command=OFF" }
Number      FHT_Bad_Fenster_offenZeit               "Bad Fenster offen Zeit[%s]"                                            <itimetimer>            (FHT, FHToffenZeit)
Number      FHT_Bad_Fenster_maxoffenZeit            "Bad Fenster max öffnungs Zeit[%s]"                                     <itimetimer>            (FHT, FHTmaxoffenZeit)

rule "Fensterüberwachung"
when
    Item FHT_Bad_Fenster changed or
    Item FHT_Buero_Fenster_Hof changed or
    Item FHT_Buero_Fenster_Strasse changed or
    Item FHT_Wohnzimmer_Fenster_Tur changed or
	Item FHT_Wohnzimmer_Fenster_Bal changed or
	Item FHT_Wohnzimmer_Fenster_Hof changed or
	Item FHT_Schlafzimmer_Fenster changed or
    Item FHT_Kueche_Fenster changed
then

    val logname = 'FENSTERÜBERWACHUNG'

    Thread::sleep(100)

    val fenster = FHTfenster.members.filter[s|s.lastUpdate("mapdb") !== null].sortBy[lastUpdate("mapdb")].last as ContactItem 
    val timer = FHTfensterTimer.members.filter[t | t.name == fenster.name+"_Timer"].head as SwitchItem

    val lastUpdate = FHTfensterUpdate.members.filter[dt | dt.name == fenster.name + "_LastUpdate"].head as DateTimeItem
    lastUpdate.postUpdate(new DateTimeType)

    val StringBuilder msg = new StringBuilder
    msg.append = transform("MAP", "fenster.map", fenster.name)

    if(fenster.state == OPEN) {
        timer.sendCommand(ON)
        msg.append(" wurde geöffnet")
    } 
    else {
        timer.postUpdate(OFF)
        sendCommand(fenster.name+"_offenZeit", "0")
         msg.append(" wurde geschlossen")
     }
    var alert = false
    if(Tageszeit.state.toString == "Nacht" && fenster.state == OPEN){
        alert = true
        msg.append(" und es ist Nacht")
    }
    if(Anwesend.state == OFF){
        alert = true
        msg.append(" und niemand Zuhause")
    }
    if(alert){
        msg.append("!")
        FensterAlarm.sendCommand(msg.toString)
    }
    logInfo(logname, msg.toString)
    
    if(Anwesend.state == OFF){
        sendNotification("xxx", msg.toString)
    }
end

rule "Fenster Timer ausgelaufen"
when
    Item FHT_Bad_Fenster_Timer received command OFF or
    Item FHT_Buero_Fenster_Hof_Timer received command OFF or
    Item FHT_Buero_Fenster_Strasse_Timer received command OFF or
    Item FHT_Wohnzimmer_Fenster_Tur_Timer received command OFF or
	Item FHT_Wohnzimmer_Fenster_Bal_Timer received command OFF or
	Item FHT_Kueche_Fenster_Timer received command OFF or
	Item FHT_Schlafzimmer_Fenster_Timer received command OFF or
    Item FHT_Wohnzimmer_Fenster_Hof_Timer received command OFF
then

    val logname = 'FENSTERALARM'

    Thread::sleep(100)

    val timer = FHTfensterTimer.members.filter[t|t.lastUpdate("mapdb") !== null].sortBy[lastUpdate("mapdb")].last as SwitchItem
    var fensterName = timer.name.split("_Timer").get(0)
    val fenster = FHTfenster.members.filter[z | z.name == fensterName].head as ContactItem
    var offenZeit = FHToffenZeit.members.filter[o | o.name == fenster.name+"_offenZeit"].head as NumberItem
    val maxoffenZeit = FHTmaxoffenZeit.members.filter[m | m.name == fenster.name+"_maxoffenZeit"].head as NumberItem
    var erlaubt = maxoffenZeit.state as Number
    var dauer = offenZeit.state as Number

    val StringBuilder msg = new StringBuilder
    msg.append = transform("MAP", "fenster.map", fenster.name)

    if(fenster.state == OPEN) {
        dauer = dauer + erlaubt
        sendCommand(offenZeit, dauer)
        if( dauer == erlaubt ) {
            msg.append(" schon ")
            msg.append(dauer)
            msg.append(" Minuten offen!")
        } else {
            msg.append(" immer noch offen! (")
            msg.append(dauer)
            msg.append("min)")
        }
        logInfo(logname, msg.toString)
        if( Anwesend.state == OFF && dauer == erlaubt ) {
           sendNotification("xxx", msg.toString)
           sendNotification("xxx", msg.toString)
        }
        if( Virtuell_x1_Zuhause.state == ON ) {
            sendNotification("xxx", msg.toString)
        }
        if( Virtuell_x2_Zuhause.state == ON ) {
            sendNotification("xxx", msg.toString)
        }
        sendCommand(timer, ON)
    }
end

After a couple of days it seems to work :slight_smile:

i hope it helps somebody

Cheers
Chris

2 Likes