Compare two TimeDate types

ok, because learning by trying causes problems in openHAB (repeated file loads et.) let us walk thru this

orig rule

rule "doorTimerExpired"
when
	Item doorTimer changed from ON to OFF
then
	var Number epoc1 = (lastDoorOpen.state as DateTimeType).calendar.timeInMillis
	var Number epoc2 = (lastMotion.state as DateTimeType).calendar.timeInMillis
	if (epoc1 < epoc2){
		andyIsHome.sendCommand(ON)
	}
	if (epoc1 > epoc2){
		andyIsHome.sendCommand(OFF)
	}
end

new version from Rich’s doc
longer but doesn’t throw deprecation warning (untested)

	var Number epoc1 = (lastDoorOpen.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
	var Number epoc2 = (lastMotion.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

The cool thing here is we can use simple math operations to compare times. Time is expressed in milliseconds since epoch (1st Jan 1970). So if an event occurred earlier in time, the number will be lower. If the number is greater, the event occurred later (after) in time. hence:

	if (epoc1 < epoc2){
		andyIsHome.sendCommand(ON)
	}
	if (epoc1 > epoc2){
		andyIsHome.sendCommand(OFF)
	}

on to the next method.

I’m guessing (sorry openHAB dumb) this is only going to work with joda time so I must covert TimeDate type to joda time type. Link to Rich’s older (8/17) but still awesome type conversion thread points us to joda time conversion methods.

// Convert DateTimeType to Joda DateTime
val joda = new DateTime((MyDateTimeItem.state as DateTimeType).calendar.timeInMillis)

hmm… smells like millis with depreciated calender warning (untested)
newer and even more awesome time conversion thread by @anfaenger found here suggest 2 methods

val MyJodaFromDateTimeType_VariantA = new DateTime((MyDateTimeItem.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)

hmmm… uses zonedDateTime (avoids warning) but still smells like millis
second method

so then I’m hoping this will work

	var Number epoc1 = new DateTime(lastDoorOpen.state.toString)
	var Number epoc2 = new DateTime(lastMotion.state.toString)

then

	if (epoc1.isBefore(epoc2)){
		andyIsHome.sendCommand(ON)
	}
	if (epoc1.isAfter(epoc2)){
		andyIsHome.sendCommand(OFF)
	}

will work?

edit: additional question… is the

	var Number

needed… or even correct, is it a joda time not a number?
how about

	var epoc1 = new DateTime(lastDoorOpen.state.toString)
	var epoc2 = new DateTime(lastMotion.state.toString)