Determining the triggering item in the body of a rule

I made similar solution:
items:

Group gEntranceContact
Group PersistentCurrent
Contact Input1_violation "Entrance 1"  <frontdoor> (gEntranceContact,PersistentCurrent)  { // binding config }
Contact Input2_violation "Entrance 2"  <frontdoor> (gEntranceContact,PersistentCurrent)  { // binding config }
Contact Input3_violation "Entrance 3"  <frontdoor> (gEntranceContact,PersistentCurrent)  { // binding config }

I use mapdb persistance persistence/mapdb.persist

Items {
  PersistentCurrent* : strategy = everyChange, restoreOnStartup
}

rules

import java.util.HashMap
var HashMap<String, Boolean> entranceChanged = newHashMap

rule initHashMap
when
  System started
then
  gEntranceContact.members.forEach[ currItem |
    val itemName = currItem.name
    if ((entranceChanged.get(itemName) == null))
    {
      logInfo("hashInit", "item: " + itemName + ", value: true" )
      entranceChanged.put(itemName, true)
    }
  ]
end

rule entraceOpen
when
    Item gEntranceContact received update
then
    Thread::sleep(250)
    try {
      gEntranceContact.members.forEach[ currItem |
        val itemName = currItem.name
        if (entranceChanged.get(itemName) == null)
        {
          entranceChanged.put(itemName, true)
        }
        else if ( entranceChanged.get(itemName) && currItem.state == OPEN )
        {
          entranceChanged.put(itemName, false)
          logInfo("entranceOpen", "          ---> ALERT for "+ itemName)
          // Here add your way of processing changed item.
        } 
        else if ( (! entranceChanged.get(itemName))  && currItem.state == CLOSED )
        {
        }
      ]
    } catch (Exception e) {
      logInfo("entranceOpen", "exception: " + e)
    }
end

I had to use such approach bacause my security system (Satel) updates all inputs at one time (even input didn’t change its state) so all have the same (or even random) update time and I can’t trust sortBy methodology. I also didn’t want to make huge persistance (so I’m using mapdb only to keep las val) and I’m detecting change of state between each update with HashMap.
Only problem is lack of item label. I can’t find method to read item label in rule, so naming schema of items should be more understandable for the people than for machines :slight_smile:

1 Like