Runs at midnight and moves the date for any tagged DateTime Items that are in the past to today’s date. The rule handles DST. The tag it looks for is a configuration parameter. Any Item with the tag that is not a DateTime Item, those DateTime Items with a NULL or UNDEF state, and any DateTime Item whose state is in the future is ignored.
Note: Most of the reasons for this rule template to be no longer apply in OH 4 so I’ve no plans to rewrite this to take advantage of OH 4 new features. If for some reason you need to move a date time to today’s date, the JS Scripting openhab-js library added a toToday() function to ZonedDateTime Objects which I recommend using.
Made compatible with both Nashorn and JSScripting
NOTE: Will drop Nashorn support sometime after OH 3.2 release.
JS doesn’t use the Java ZonedDateTime Classes and uses the js-joda libraries instead which is mostly the same but slightly different in some ways. One of the ways is given the nature of JS we’ve been able to monkey patch a few functions onto ZonedDateTime. Most of these are for compatibility purposes, but toToday() and isBetween() are two useful user facing additions.
But yes indeed, that approach would work too, only with slightly different syntax.
var timePart = time.toZDT(items.DateTime1.state).toLocalTime();
var datePart. = time.toZDT().toLocalDate();
var tz = time.toZDT().zone();
items.DateTimeItem1.postUpdate(time.ZonedDateTime.of3(timePart, datePart, tz));
But that’s awkward so that’s why I proposed and it was accepted the toToday() addition.
Almost. The state of the Item is the String representation of a DateTime so you’d need to parse that back into a ZonedDateTime first. But time.toZDT() handles that for you if you just pass it the Item.
I don’t use DateTimes too much in my rules beyond setting timers, and a duration string is usually easier and clearer in that case anyway (e.g.time.toZDT( 'PT1H2M`) for a time one hour and two minutes from now). There might be something built into the Item class to make it so we don’t have to parse the String. But I’ve no looked at that code in a long time.