[SOLVED] Persistence query in habpanel


(Romain ALCARAZ) #1

Hi guys !
I want to display last opened date of my door/window module in habpanel.
this item is in my persistence model and i want to use item.lastupdate query to do that.

SO, it’s possible or not ? Do i need to create virtual item which store lastupdate date ?

thanks a lot


(Rich Koshak) #2

Yes. Create an unbound item and a rule that triggers when your door/window sensor changes to populate the unbound item.

See:

And here is the code I use to achieve this (note, this rule does a lot of other stuff, the part you care about is the first four lines of actual code (i.e. skip the comments)):

rule "Keep track of the last time a door was opened or closed"
when
  Item vGarageOpener1 changed from CLOSED to OPEN or
  Item vGarageOpener1 changed from OPEN to CLOSED or
  Item vGarageOpener2 changed from CLOSED to OPEN or
  Item vGarageOpener2 changed from OPEN to CLOSED or
  Item vFrontDoor changed from CLOSED to OPEN or
  Item vFrontDoor changed from OPEN to CLOSED or
  Item vBackDoor changed from CLOSED to OPEN or
  Item vBackDoor changed from OPEN to CLOSED or
  Item vGarageDoor changed from CLOSED to OPEN or
  Item vGarageDoor changed from OPEN to CLOSED
then
  // Get the most recently changed sensor which we will assume is the sensor that triggered the rule
  Thread::sleep(100) // give persistence a change to catch up
  val door = gDoorSensors.members.filter[s|s.lastUpdate("mapdb") != null].sortBy[lastUpdate("mapdb")].last as ContactItem
  
  // Update LastUpdate Item
  val last = gDoorsLast.members.filter[dt | dt.name == door.name+"_LastUpdate"].head as DateTimeItem
  last.postUpdate(new DateTimeType)

  // Set/Cancel the Timer for the warning when the door remains open for more than an hour
  if(door.state == OPEN) sendCommand(door.name+"_Timer", "ON")
  else postUpdate(door.name+"_Timer", "OFF")
  
  // Log and alert immediately if no-one is home
  val StringBuilder msg = new StringBuilder
  val doorName = transform("MAP", "en.map", door.name)
  
  msg.append(doorName)
  msg.append(" was")
  msg.append(if(door.state == OPEN) " opened" else " closed")
  
  var alert = false
  if(vTimeOfDay.state.toString == "NIGHT" || vTimeOfDay.state.toString == "BED"){
  	alert = true
  	msg.append(" and it is night")
  }
  
  if(vPresent.state == OFF) {
  	alert = true
  	msg.append(" and no one is home")
  }
  
  if(alert){
  	msg.append("!")
  	aAlert.sendCommand(msg.toString)
  }
  
  logInfo(logName, msg.toString)
	  
end