Compairing times

I’m trying to compare times.
I have Opening (DateTime Item type) and want to know if that is before now
All three versions raise errors!

now.isAfter(Opening)

now.isAfter(Opening.calendar.timeInMillis)

now.isAfter((Opening.calendar.timeInMillis).millis)

Not sure whether this applies here too, but from Design Patterns Time of Day (Design Pattern: Time Of Day), I took:

val long sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis).millis

which works with

now.isAfter(sunrise)

(Sunrise_Time it self is an item that is linked to the astro binding)
date conversion confuse me easily but I believe you need to import
import java.util.Date
You may want to read through the whole post

Edit: this rather old post clarified a few things, at least for me

1 Like

Depending on how your opening time is set, you could use this if you are trying to define a particular time:

val Opening = now.withTimeAtStartOfDay.plusHours(X).millis 

I believe if I remember right you can also do along the lines of:

now.withTimeAtStartOfDay.plusHours(X).plusMinute(Y).millis

For both X and Y can be either variables if you want to control it that way or just numbers. Then you can use the now.isAfter(Opening) for a true/false check.

My OpeningTime is read from somewhere else.

I tried it the way it was posteed in the “old” thread (including the imports):

val DateTime CheckTimeStart = new DateTime((Opening.state as DateTimeType).calendar.currentTimeMills)
if (now.isAfter(CheckTimeStart)&& now.isBefore(CheckTimeEnd) {

and I get:

22:37:57.903 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Benzinpreise: An error occured during the script execution: The name ‘.currentTimeMills’ cannot be resolved to an item or type.

Not sure what you want to accomplish, but if you want the read the current time and compare it to a setpoint or similar, you may want to look this excellent post: Time of Day Events

the first term give you the time rounded to full minutes and compares it to another time also rounded to full minutes.
Works like a charm for me.

1 Like

Thanks for the second help in here. My failure was to copy from the “rather old” thread instead of directly from your post.
So I used: “calendar.currentTimeMills” instead of "calendar.timeInMillis"
Works like a charm now.

Dankeschön