To Today [3.2.0;3.4.9)

logo

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.

Language: Nashorn JavaScript ECMAScript 5.1 or JSScripting.

Changelog

Version 0.2

  • Made compatible with both Nashorn and JSScripting
    NOTE: Will drop Nashorn support sometime after OH 3.2 release.

Version 0.1

  • initial release

Resources

https://raw.githubusercontent.com/rkoshak/openhab-rules-tools/main/rule-templates/toToday/to_today.yaml

4 Likes

I appreciate what you did.

Hi Rich,
i might have a stupid question on this one.
How can i move the date but not the time of an item to today in oH4?

Cheers Michael

It depends on your use case.

There is a `Time is time only" trigger. If you use that you don’t need to move the date to today’s date. It’s ignored.

In JS Scripting there is a toToday() method on the ZonedDateTime class. I think jRuby has something similar.

1 Like

In Java, you can replace the date part of a ZonedDateTime instance to today’s date, keeping the time the same.

zdt_same_time_today = zdt_in_the_past_or_future.with(LocalDate.now())

This can of course be done in a JS or JRuby code that holds a ZonedDateTime object.

For example in jruby, to move the date of the item to today whilst keeping the same time

DateTimeItem1.update DateTimeItem1.state.with(LocalDate.now)

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.

How would this be done using toToday()?

Like this?

items.DateTimeItem1.postUpdate(time.toZDT(items.DateTimeItem1.state).toToday())
items.DateTimeItem1.postUpdate(time.toZDT(items.DateTimeItem1).toToday());

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.