Create a datetime item

Is it possible to create a datetime item then use that in the rules to compare it to a ‘now’ datetime time


If I wanted something like

StartTime = yyyy-MM-dd HH:mm:ss
EndTime = yyyy-MM-dd HH:mm:ss

Then in the rules

if (now.isbefore(EndTime) && now.isafter(StartTime))

I have seen lots of example of creating datetimes from bindings where the item/object is already formatted. But what about creating your own?

I want to do this to easily make a schedule that can quickly be adjusted.

Im using version 2.2


The item-state is unformated, the format-option is only for the displayed label.

What about a variable in the rules file?

That’s exactly the expression you can use in a rule. But be careful, you mast cast the right itemtypes on it. See Rich’s genious pattern on that one:

Sould the start- and end-time be dates on fix days or times on every days?

The design pattern is one of the sources I was looking at but it only uses the available times from the binding, but otherwise the comparisons between items is all relevant.

As for the days, most would be daily schedules but a few that arent I would compare to the current day to decide if the schedule need to be turned on.

oh, that’s not an issue - if you just create an empty DateTime item, you can use it just as a binding item.

Im not following, How do I input the times I want and convert that into something I can compare to a ‘now’ datetime object?

I notice this from Richs post.


DateTime vSunrise_Time "Sunrise [%1$tH:%1$tM]"
    { channel="astro:sun:home:rise#start" }


val long day_start = (vSunrise_Time.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli

Can I create that item maybe as a string but in the right format, then cast it to a datetime object?

you could do that. But first: What do you want to achieve?

To create schedules, where I can set and adjust the start and stop times. Cron isnt working. No idea why.

ok - Then don’t play around with creating timestamps and rules and comparisons… :wink:

please post your rule with your cron-triggers.

but this question isnt about why the cron doesnt work, I wanted to know if you can create a datetime object with my own values.

sure! either you can create a timestamp at the time, your rule triggers: MyTimestamp.sendCommand(new DateTimeType) or you can create on depending on the now(): var DateTime HoursAgo = now.minusHours(3) or you can calculate one out of thin air:

var MyTimestamp_String = "2018-01-18 13:25:15" // not sure on that one, as I only update DateTime via REST API or MQTT, but with '%Y-%m-%dT%H:%M:%S', so I guess, it'll work

In the DP you will notice I have three cron triggers and therefore three self defined times I need to compare against. I use `now.withTimeAtStartOfDay.plusHours(6).plusMinutes(15) which would give me 06:15 for today.

val morningStart = now.withTimeAtStartOfDay.plusHours(6).millis 

It gets harder if you want to be able to define the DateTime from one of your GUIs to populate an Item as there currently is no good mechanism to support that. You can find several approaches people have take to implement that though by searching the forum for “Alarm Clock”.

The hard part is the UI part. If you are OK with defining your times from a Rule though, you can apply Design Pattern: Encoding and Accessing Values in Rules (Approach 2). You would have a rule triggered at System started or midnight (or incorporate them into the TimeOfDay rule).

This has been reported by a few others. But even if you did create DateTime Items with your own values as described above, there is no way for you to trigger the Rule based solely on that DateTime Item. You would have to implement one of the Alarm Clock examples which requires the use of Timers and such.

Scheduling is one of the areas that OH is weak but there are efforts underway to improve that.

I did mean to reply yesterday regarding the difference between time and datetime objects, realising that datetime is outside of this scope.

Thanks Rich for the description of that line. It answers the question I was really heading towards. The lack of gui is fine as long as something works.

I have one last question though in more general terms. I looked through various examples and see the use of ‘now.xxxx’ but where do I find the info on those classes and methods?

unfortuantely XTEND (the Scripting language used by the rules-engine) isn’t that well documented:

for all those “”-stuff, that’s Joda Time (e.g. and for use cases in openHAB you have to either comb the forum or get lucky with examples in

Or, if you use VSCode with the OH Extension you can just type now. and wait a second or so and it will list all the properties and methods on the object. I can’t remember if it links to the JavaDoc or not.

I cannot recommend VSCode for OH Rules editing strongly enough.

1 Like

I have fought this for the better part of an afternoon. I too wanted to use a datetime constant in before / after logic. I followed links to design principles, I tried DateTime, Stings, Longs, DateTimeType, epoch, millis and Joda, tripping over every can’t cast error in the book.

I finally got a working example. In this rule snipit, I am testing if today is after Aug 5th, or before May 20th, trying to determine if my children have school. This rule is still under development, but in my version I also test for day of the week. It is my goal to run via cron setting the SchoolDayState for the following day. Ultimately, I would like to incorporate testing for Holidays. I want to set the state early the day before so I can manually override for unexpected no school days the evening before so wake up alarms and other schedules don’t fire when there is no school. I think the vacation logic will be the most difficult, not sure how to get that phase started, but I did find an example in the docs. I’ll see if I can tweak it to work.

Anyway, I didn’t want others to suffer and become frustrated at the lack of a basic example.

rule "Setting School Day State"
//    Time cron "0 0 15 * * ?"
Item  TestSwitch changed
   logInfo("SchoolDayState", "Setting SchoolDay Switch")

   val DateTime FirstDay = new DateTime("2018-08-05T00:00:00")
   val DateTime LastDay  = new DateTime("2018-05-20T00:00:00")

   logInfo("SchoolDayState", "First Day value: " + FirstDay)
   logInfo("SchoolDayState", "Last Day value : " + LastDay)

   if((now.isBefore(LastDay)) || (now.isAfter(FirstDay))) {

   // more logic

1 Like