OH3 - zonedDateTime.now() - Remove Time Zone from Var Result

Spent about an hour looking around how to remove the Time Zone from zonedDateTime.now() without any luck.

Here’s my variables in .rules:

var 				Cal_Date0 = new DateTimeType().zonedDateTime.now().plusDays(0)
var 				Cal_Date1 = new DateTimeType().zonedDateTime.now().plusDays(1)
var 				Cal_Date2 = new DateTimeType().zonedDateTime.now().plusDays(2)
var 				Cal_Date3 = new DateTimeType().zonedDateTime.now().plusDays(3)
var 				Cal_Date4 = new DateTimeType().zonedDateTime.now().plusDays(4)

Here’s the results from it:

2021-11-13 11:10:09.472 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date0 is 2021-11-13T11:10:06.031673-06:00[America/Chicago]
2021-11-13 11:10:09.770 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date1 is 2021-11-14T11:10:06.033016-06:00[America/Chicago]
2021-11-13 11:10:09.775 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date2 is 2021-11-15T11:10:06.034169-06:00[America/Chicago]
2021-11-13 11:10:09.778 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date3 is 2021-11-16T11:10:06.035307-06:00[America/Chicago]
2021-11-13 11:10:09.781 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date4 is 2021-11-17T11:10:06.036415-06:00[America/Chicago]

Here’s the issue at hand with Time Zone appended to Date Time variables to use else where:

2021-11-13 11:10:09.866 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '2021-11-13T11:10:06.031673-06:00[America/Chicago]' to a state type which item 'FIOWeather_ObservationTime0' accepts: [DateTimeType, UnDefType].
2021-11-13 11:10:09.875 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '2021-11-14T11:10:06.033016-06:00[America/Chicago]' to a state type which item 'FIOWeather_ObservationTime1' accepts: [DateTimeType, UnDefType].
2021-11-13 11:10:09.880 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '2021-11-15T11:10:06.034169-06:00[America/Chicago]' to a state type which item 'FIOWeather_ObservationTime2' accepts: [DateTimeType, UnDefType].
2021-11-13 11:10:09.885 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '2021-11-16T11:10:06.035307-06:00[America/Chicago]' to a state type which item 'FIOWeather_ObservationTime3' accepts: [DateTimeType, UnDefType].
2021-11-13 11:10:09.890 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '2021-11-17T11:10:06.036415-06:00[America/Chicago]' to a state type which item 'FIOWeather_ObservationTime4' accepts: [DateTimeType, UnDefType].

FIOWeather_ObservationTime fields are all defined as DateTime.

Here’s the syntax for update FIOWeather_ObservationTime fields:

		if (gInternet.state == ON && Cal_Date0 !== null && Cal_Date0 != NULL && Cal_Date0 != UNDEF) { FIOWeather_ObservationTime0.postUpdate(Cal_Date0.toString()) }
		if (gInternet.state == ON && Cal_Date1 !== null && Cal_Date1 != NULL && Cal_Date1 != UNDEF) { FIOWeather_ObservationTime1.postUpdate(Cal_Date1.toString()) }
		if (gInternet.state == ON && Cal_Date2 !== null && Cal_Date2 != NULL && Cal_Date2 != UNDEF) { FIOWeather_ObservationTime2.postUpdate(Cal_Date2.toString()) }
		if (gInternet.state == ON && Cal_Date3 !== null && Cal_Date3 != NULL && Cal_Date3 != UNDEF) { FIOWeather_ObservationTime3.postUpdate(Cal_Date3.toString()) }
		if (gInternet.state == ON && Cal_Date4 !== null && Cal_Date4 != NULL && Cal_Date4 != UNDEF) { FIOWeather_ObservationTime4.postUpdate(Cal_Date4.toString()) }

Best, Jay

Example

This OH 3.x date conversion stuff is so frustrating and time consuming to get OH 2.x rules to work with OH 3.x. I spent another hour on it and found this post from @rlkoshak which solved my issue.

Here’s the result:

Items:

var 				Cal_Date0 = new DateTimeType(now.plusDays(0))
var 				Cal_Date1 = new DateTimeType(now.plusDays(1))
var 				Cal_Date2 = new DateTimeType(now.plusDays(2))
var 				Cal_Date3 = new DateTimeType(now.plusDays(3))
var 				Cal_Date4 = new DateTimeType(now.plusDays(4))

Results (NO time zone attached to it):

2021-11-13 20:13:47.123 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date0 is 2021-11-13T20:13:29.311957-0600
2021-11-13 20:13:47.127 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date1 is 2021-11-14T20:13:29.313856-0600
2021-11-13 20:13:47.131 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date2 is 2021-11-15T20:13:29.315491-0600
2021-11-13 20:13:47.133 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date3 is 2021-11-16T20:13:29.317179-0600
2021-11-13 20:13:47.136 [INFO ] [rg.openhab.core.model.script.STARTUP] - Cal_Date4 is 2021-11-17T20:13:29.318945-0600

Best, Jay

Here is the full and complete docs for everything that ZonedDateTime can do.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html

There are also a million examples on Google for working with ZonedDateTime.

This line is over done with lots of redundancies. Assuming Rules DSL,

var Cal_Date0 = now
var Cal_Date1 = now.plusDays(1)

It’s really no different from 2.5.

Indeed, because the toString() for a ZonedDateTime is different from a the toString from a Joda DateTime. But it’s easy to get to an ISO 8601 string version:

log.info("STARTUP", "Cal_Date0 is " + Cal_Date0.toLocalDateTime())

This is a case where OH 3 is better that OH 2. Just pass it the ZonedDateTime without the toString()

FIOWeather_ObservationTime1.postUpdate(Cal_Date1)

Even that is way more than you need with redundancies. See above. You just need now.plusDays() and pass it to postUpdate without the toString. Easy peasy.

1 Like