Set last tripped/alarm date, or last action date to an item - best practice?

Your “coolest way” I’m pretty sure is not possible. I just have a DateTime Item associated with the Item I want to know when the last time it was triggered (in my case a Door contact that was opened) and a rule to update the DateTime with now when the Item changes.

#Items:

// Entry Sensors
Group:Contact:OR(OPEN, CLOSED) gDoorSensors "Entry Status [MAP(en.map):%s]" <frontdoor>
Group gRemindDoorSensors
Group gDoorSensorsTime

Contact N_D_Front                        "Front Door [MAP(en.map):%s]"             <frontdoor>  (gDoorSensors, gRemindDoorSensors, gAlarmSensors, gHydraSens
ors)    { mqtt="<[mosquitto:entry_sensors/main/front_door:state:default]" }
DateTime N_D_Front_Last_Opened       "Front Door [%1$tm/%1$td %1$tH:%1$tM]"    <frontdoor>  (gDoorSensorsTime)

Contact N_D_Back                         "Back Door [MAP(en.map):%s]"              <frontdoor>  (gDoorSensors, gRemindDoorSensors, gAlarmSensors, gHydraSensors)    { mqtt="<[mosquitto:entry_sensors/main/back_door:state:default]" }
DateTime N_D_Back_Last_Opened        "Back Door [%1$tm/%1$td %1$tH:%1$tM]"     <frontdoor>  (gDoorSensorsTime)

Contact N_D_Garage                       "Garage Door [MAP(en.map):%s]"            <door>               (gDoorSensors, gRemindDoorSensors, gAlarmSensors, gHydraSensors)    { mqtt="<[mosquitto:entry_sensors/main/garage_door:state:default]" }
DateTime N_D_Garage_Last_Opened      "Garage Door [%1$tm/%1$td %1$tH:%1$tM]"   <door>       (gDoorSensorsTime)

Contact N_D_GarageDoor1              "Garage Door 1 [MAP(en.map):%s]"          <garagedoor> (gDoorSensors, gRemindDoorSensors, gGarageSensors) { mqtt="<[mosquitto:entry_sensors/main/garage/door1:state:default]" }
DateTime N_D_GarageDoor1_Last_Opened "Garage Door 1 [%1$tm/%1$td %1$tH:%1$tM]" <garagedoor> (gDoorSensorsTime)

Contact N_D_GarageDoor2              "Garage Door 2 [MAP(en.map):%s]"          <garagedoor> (gDoorSensors, gRemindDoorSensors, gGarageSensors) { mqtt="<[mosquitto:entry_sensors/main/garage/door2:state:default]" }
DateTime N_D_GarageDoor2_Last_Opened "Garage Door 2 [%1$tm/%1$td %1$tH:%1$tM]" <garagedoor> (gDoorSensorsTime)

Things to note:

  • Each Contact has a DateTime with the same name but with “_Last_Opened” appended
  • All of the DateTimes belong to the same group
  • All of the contacts belong to the gDoorSensors group which lets me create one rule to handle all the Contacts and figure out which door was opened to trigger the rule

#Rule:

rule "A Door's State Changed"
when
        Item N_D_Front changed or
        Item N_D_Back changed or
        Item N_D_Garage changed or
        Item N_D_GarageDoor1 changed or
        Item N_D_GarageDoor2 changed
then
    // Get the door that opened
    gDoorSensors?.members.filter(door|door.changedSince(now.minusSeconds(1))).forEach[ door |
        // Do stuff

        // Save the date/time for openings
        if(door.state == OPEN) {
            gDoorSensorsTime?.members.filter(dt|dt.name == door.name+"_Last_Opened").head.postUpdate(new DateTimeType)
        }
    ]
end

Things to note:

  • Because of the way Groups are processed, we have to list each door Contact separately as a rule trigger or else the rule gets triggered more than once per event
  • The line to get the most recently opened door requires persistence for changedSince to work
  • The line that gets the DateTime associated with the Contact is found by name so naming consistency is key

#Sitemap:

                        Text item=N_D_Front_Last_Opened       icon="frontdoor-open"    visibility=[N_D_Front=="OPEN"]
                        Text item=N_D_Front_Last_Opened       icon="frontdoor-closed"  visibility=[N_D_Front!="OPEN"]
                        Text item=N_D_Back_Last_Opened        icon="frontdoor-open"    visibility=[N_D_Back=="OPEN"]
                        Text item=N_D_Back_Last_Opened        icon="frontdoor-closed"  visibility=[N_D_Back!="OPEN"]
                        Text item=N_D_Garage_Last_Opened      icon="door-open"         visibility=[N_D_Garage=="OPEN"]
                        Text item=N_D_Garage_Last_Opened      icon="door-closed"       visibility=[N_D_Garage!="OPEN"]
                        Text item=N_D_GarageDoor1_Last_Opened icon="garagedoor-open"   visibility=[N_D_GarageDoor1=="OPEN"]
                        Text item=N_D_GarageDoor1_Last_Opened icon="garagedoor-closed" visibility=[N_D_GarageDoor1!="OPEN"]
                        Text item=N_D_GarageDoor2_Last_Opened icon="garagedoor-open"   visibility=[N_D_GarageDoor2=="OPEN"]
                        Text item=N_D_GarageDoor2_Last_Opened icon="garagedoor-closed" visibility=[N_D_GarageDoor2!="OPEN"]

Each DateTime is listed twice because I want to use the correct opened or closed icon to reflect the door’s current state which I achieve through the visibility attribute.

5 Likes