How to compare times in OH3

What is the native/recommended method to store and compare two times inside rules in OH3. I’ve been through other posts but they seem to require imports and/or conversions etc.

var timeON = [?????]

rule "TestSwitch"
when 
	Item SwitchA changed 
then
	if (SwitchA.state == ON) {
		timeON = [??????]
	} else {
		if (timeON < [NOW MINUS 2 MINUTES]) {
			logInfo('test', "2 min expired")
		}
	}
end

The examples with the imports are native. Just because something is imported doesn’t make it not part of the language.

In OH 3 all time interactions and comparisons are done using ZonedDateTime. Any one of the ZonedDateTime examples you’ve seen is how to do it.

now is a ZonedDateTime initialized to the time it’s called.

var ZonedDateTime timeON = null

rule "TestSwitch"
when 
	Item SwitchA changed 
then
	if (SwitchA.state == ON) {
		timeON = now
	} else {
		if (timeON.isBefore(now.minusMinutes(2)) {
			logInfo('test', "2 min expired")
		}
	}
end

Thanks Rich! I was missing the ZonedDateTime.

BTW if (timeON.isBefore(now.minusMinutes(2)) is missing a “)”

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.