Rule example - compare two times

Tags: #<Tag:0x00007f1749c09c68>

This is a simple rule example for folks like me, who are new to openHAB, and need simple examples of rules to get started. In this example, two times are compared, enabling us to make a decision based on if one event occurred before another.
In the example, a contact switch on an entry door triggers a time stamp. A motion detector also triggers a time stamp. The two times can be compared to see if the occupant (andy) is home (motion since door was opened) or left (no motion since door was opened)

This rule uses the expire binding

we need several ‘dummy’ items which hold values but are not linked to any real physical device. These are created in an items file. You will need two DateTime items to hold the two times you wish to compare, a ‘dummy’ switch item which allows you to use the expire binding to create a timed event similar to a timer which will trigger after a given amount of time, in this case 60 seconds. Another switch item (andyisHome) is just used to hold the result of the rule.

items file:

DateTime    lastDoorOpen
DateTime    lastMotion
Switch doorTimer    { expire="60s,command=OFF" }
Switch  andyIsHome

you will also need two items to trigger the rules, in this example, they are actual physical devices (zwave sensors) but anything will work (make test buttons in habpanel or something to test) these can go in the same items file

Contact contactDoor  "Door Sensor"    <door>  (ghome) {channel="zwave:device:ffe82412:node4:sensor_door"}
Switch  sensorMotion "Motion sensor"  <alarm> (ghome) {channel="zwave:device:ffe82412:node5:alarm_motion"}

rules files:
first a rule to trigger when the door opens. When the door contact goes from closed to open it writes a time to the item lastDoorOpen and starts the timer

rule "doorOpened"
	Item contactDoor changed from CLOSED to OPEN
	val timeX = new DateTimeType(now.toString)

next rule fires when there is movement and writes a time stamp

rule "motioninhouse"
	Item sensorMotion changed from OFF to ON
	val timeY = new DateTimeType(now.toString)

then this rule is where the time comparison is made. It is triggered when the expire binding item turns from on to off. if there has been motion since the door opened, the switch indicating occupancy is set to true (andyIsHome set to ON) if there hasn’t been any motion since the door opened, the switch indicating occupancy is set to OFF

rule "doorTimerExpired"
	Item doorTimer changed from ON to OFF
	val openTime = new DateTime(lastDoorOpen.state.toString)
	val motionTime = new DateTime(lastMotion.state.toString)
	if (openTime.isBefore(motionTime)){
	if (openTime.isAfter(motionTime)){

This is a very simple example of an occupancy rule based on if there is motion since the last time the door opened but the premise can be used to compare any two times. I use this rule and it runs so I posted it hoping others would learn from it but if you find a mistake post in this thread and I will try to keep this original post updated

Further reading:
Time is a little complex in rules because openHAB stores dates and times one way and the rules use a different format. A great resource for understanding this better is the awesome time conversion thread by rud @anfaenger found here
Here is a link where Rich, namraccr and rud helped me understand the time formtting issue and develop this rule. Lots to be learned there also and various type conversions are found in Rich’s type conversion thread

if some of this stuff needs more explaining
expire binding

rules docs

hope this helps someone, if it does, click the like button and I’ll post a few more


Very well written. Thanks for posting!