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.
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
MyTimestamp.sendCommand(MyTimestamp_String)
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?
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.
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"
when
// Time cron "0 0 15 * * ?"
Item TestSwitch changed
then
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
}
end