DateTime Conversion after Update to 4.3.0

After the Update tonight this rule doesn’t work anymore:

val sunsetend = ((Sunset_End.state as DateTimeType).getZonedDateTime())
val daylstart = ((Daylight_Start.state as DateTimeType).getZonedDateTime())
Sunset_End_Rule.postUpdate(sunsetend.minusDays(1).toString)
Daylight_Start_Rule.postUpdate(daylstart.toString)

Sunset_End_Rule and Daylight_Start_Rule are both items of DateTime type.

The error message is:
Cannot convert ‘2024-12-22T16:29+01:00[Europe/Berlin]’ to a state type which item ‘Sunset_End_Rule’ accepts: [DateTimeType, UnDefType].
Cannot convert ‘2024-12-23T08:27+01:00[Europe/Berlin]’ to a state type which item ‘Daylight_Start_Rule’ accepts: [DateTimeType, UnDefType].

You have missed the deprecation warning.
Please check here

It is still the same error using this statement as suggested:

val sunsetend = ((Sunset_End.state as DateTimeType).getZonedDateTime(ZoneId.systemDefault()))
val daylstart = ((Daylight_Start.state as DateTimeType).getZonedDateTime(ZoneId.systemDefault()))

This is not related to the deprecation of getZonedDateTime(). What is the source of the state for Sunset_End and Daylight_Start?

My theory is that the state might have been updated without explicit time-zone ID provided and as a result toString() would return “2024-12-22T16:29+01:00” prior to 4.3. Now, the time-zone is provided as ZoneId.systemDefault() and thus you get “2024-12-22T16:29+01:00[Europe/Berlin]”, which I believe DateTimeType is not able to parse. I would like to confirm this.

You could try this to parse it as Instant rather than ZonedDateTime with time-zone ID:

val sunsetend = ((Sunset_End.state as DateTimeType).getZonedDateTime(ZoneId.systemDefault))
val daylstart = ((Daylight_Start.state as DateTimeType).getZonedDateTime(ZoneId.systemDefault))
Sunset_End_Rule.postUpdate(sunsetend.minusDays(1).toInstant.toString)
Daylight_Start_Rule.postUpdate(daylstart.toInstant().toString)
1 Like

For reference:

That’s it!
toInstant() at the postUpdate command was the missing part.
Many thanks!!

I did not get this confirmation, but with 4.3.2 (which was just released), DateTimeType can now handle a string like “2024-12-22T16:29+01:00[Europe/Berlin]”, so the original rule posted here should now work.