OH3 DateTime-Conversion again (with Astro items)

I know, that there is a long thread regarding such topics, but there I did not found a solution or - in other words - the solution that I found there did not work in my case. So I raise up this new topic.

In a rule I have to compare the current time with the timestamp of an Astro item:

   ( sunset_end_time.state as DateTimeType ).getZonedDateTime())

Regarding the advice in the above mentioned thread, in my opinion this should work (see case #4 there).

But I get:

Could not invoke method: java.time.LocalDateTime.isAfter(java.time.chrono.ChronoLocalDateTime) on instance: 2021-09-28T20:33:57.606679 in astro

Does anyone have an idea how to solve that? Although an explicit cast did not change the behaviour.

I believe that thread had you using ZonedDateTime.now(), not LocalDateTime.now(). The error is saying Local datetime.now().isAfter() wants a CronoLocalDateTime but you are passing it a ZonedDateTime.

Thank you very much, @rlkoshak, for your quick help!

Based on your note, the solution is:

val now = LocalDateTime.now().atZone(ZoneId.of("Europe/Berlin"))
if (
    now.isAfter( (sunrise_start_time.state as DateTimeType).getZonedDateTime() )
    && now.isBefore( (sunset_end_time.state as DateTimeType).getZonedDateTime() )
) {

That seems overly complicated compared to

val now = ZonedDateTime.now()

That was my first assumption, too, but ZonedDateTime.now() is NULL in my case …

It can’t be NULL. That’s a special state Items can carry.

I’ve never encountered a case where ZonedDateTime.now() returns null. There is no reason that it should. It’s a core part of the Java language (ZonedDateTime (Java SE 11 & JDK 11 )) and there is no indication that it will return null under any circumstances. Show the line of code that you tried to run that returned null.

Okay, sorry. Failure on my side. I tried it with

val now = ZonedDateTime.now()

which resulted in the following output in the log:

Script execution of rule with UID 'astro-2' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in astro

which let me assume, that now is NULL … (I just forgot toString in this log output).

Thank you for your endurance :slight_smile:

1 Like

OK, glad you figured it out. But be careful with terminology. NULL and null are two completely different things and mean completely different things. It gets confusing if they are not used correctly.

Sorry for any confusion … and again thanks for your help!