# OH3: update a datetime item with a ZonedDateTime

**URL:** <https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861>\
**Category:** Migration\
**Created:** [November 10, 2022, 12:05pm UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861 "2022-11-10T12:05:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_G](https://community.openhab.org/user_avatar/community.openhab.org/max_g/32/64598_2.png) [@Max\_G](https://community.openhab.org/u/Max_G)\
**Post date:** [November 10, 2022, 12:05pm UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861/1 "2022-11-10T12:05:54Z")

</div>

- openhabian 3.3.0 on rPi4

I have a decimal time of say 6.75  
Then turn this into hours and minutes.  
Then update a DateTime item, which fails

```auto
        val time_to_charge = TeslaM3_time_to_full_charge.state as DecimalType
        logInfo(logPrefix + "02.01", "time_to_charge {}", time_to_charge)

        val hours = (Math::floor(time_to_charge.floatValue)).intValue
        logInfo(logPrefix + "02.02", "hours {}", hours)

        val minutes = Math::round((time_to_charge.floatValue - hours) * 60)
        logInfo(logPrefix + "02.03", "minutes {}", minutes)

        val charged_at_time = ZonedDateTime.now().plusHours(hours).plusMinutes(minutes)
        logInfo(logPrefix + "02.04", "charged_at_time {}", charged_at_time)

        TeslaM3_time_charged.postUpdate(charged_at_time)
        logInfo(logPrefix + "02.05", "TeslaM3_time_charged {}", TeslaM3_time_charged.state)

```

This line fails:

```auto
        TeslaM3_time_charged.postUpdate(charged_at_time)

```

Here the log:

```auto
2022-11-10 22:01:00.484 [INFO] [ab.core.model.script.testRules.02.01] - time_to_charge 6.75
2022-11-10 22:01:00.487 [INFO] [ab.core.model.script.testRules.02.02] - hours 6
2022-11-10 22:01:00.490 [INFO] [ab.core.model.script.testRules.02.03] - minutes 45
2022-11-10 22:01:00.493 [INFO] [ab.core.model.script.testRules.02.04] - charged_at_time 2022-11-11T04:46:00.491425+10:00[Australia/Brisbane]
2022-11-10 22:01:00.496 [INFO] [ab.core.model.script.testRules.02.05] - TeslaM3_time_charged 2022-11-11T04:45:30.491524+1000

```

Any hints appreciated.

I had a look at this:

> [@DateTime Conversion (openHAB 3.x)](https://community.openhab.org/t/datetime-conversion-openhab-3-x/107197):
>
> Because openHAB 2.x rules use Jodatime instead of Java Time API, there is still the old [DateTime Conversion thread for openHAB 2.x](https://community.openhab.org/t/datetime-conversion-openhab-2-x/54266). Overview In openHab 3.x there are different ways to handle Date/Time values. DateTimeType A [DateTime Item](https://www.openhab.org/docs/concepts/items.html#items) carries a [DateTimeType](https://www.openhab.org/docs/concepts/items.html#state-and-command-type-formatting). Java Time By default openHAB 3.x Rules DSL use [java.time API](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) to represent date and time. Principle java date-time concepts are [LocalDateTime](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDateTime.html) (date and time without any offset or time-zone) and [ZonedDateTime](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html) (“full” date-time wit…

But it does not sink in what type I have in `charged_at_time`.  
I thought it was DateTime?!

---

<div class="post-metadata">

**Author:** ![Max\_G](https://community.openhab.org/user_avatar/community.openhab.org/max_g/32/64598_2.png) [@Max\_G](https://community.openhab.org/u/Max_G)\
**Post date:** [November 10, 2022, 12:14pm UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861/2 "2022-11-10T12:14:46Z")

</div>

Well, further reading and trial and error, this works:

```auto
TeslaM3_time_charged.postUpdate(DateTimeType.valueOf(charged_at_time.toLocalDateTime().toString()))

```

```auto
2022-11-10 22:15:30.879 [INFO] [ab.core.model.script.testRules.02.02] - hours 6
2022-11-10 22:15:30.882 [INFO] [ab.core.model.script.testRules.02.03] - minutes 30
2022-11-10 22:15:30.885 [INFO] [ab.core.model.script.testRules.02.04] - charged_at_time 2022-11-11T04:45:30.884094+10:00[Australia/Brisbane]
2022-11-10 22:15:30.889 [INFO] [ab.core.model.script.testRules.02.05] - TeslaM3_time_charged 2022-11-11T04:45:00.880009+1000

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [November 10, 2022, 12:31pm UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861/3 "2022-11-10T12:31:09Z")

</div>

> [@Max\_G](#):
>
> I have a decimal time of say 6.75

Wouldn’t that be a duration, a period of time, not a datetime, a specific instant in the life of the world?

EDIT okay ignore that, you are trying to predict an instant I think.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 10, 2022, 2:48pm UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861/4 "2022-11-10T14:48:36Z")

</div>

> [@Max\_G](#):
>
> This line fails:

I would have expected that to work on Rules DSL. What’s not clear here is how that line fails. There is no error in the log.

> [@Max\_G](#):
>
> But it does not sink in what type I have in `charged_at_time`.

Right now you have a `java.time.ZonedDateTime` in `changed_at_time`. You started with a `ZonedDateTime` and manipulated it’s state but did change it’s type.

> [@Max\_G](#):
>
> Well, further reading and trial and error, this works:

That’s a little overworked. This should work.

```
TeslaM3_time_charged.postUpdate(new DateTimeType(charged_at_time))

```

This should work too:

```
TeslaM3_time_charged.postUpdate(charged_at_time.toLocalDateTime().toString()))

```

But over all I would expect both of these to work too.

```
TeslaM3_time_charged.postUpdate(charged_at_time)
TeslaM3_time_charged.postUpdate(charged_at_time.toString())

```

> [@rossko57](#):
>
> Wouldn’t that be a duration, a period of time, not a datetime, a specific instant in the life of the world?

Running with this, here is how it would be done with a Duration.

```
val myDur = Duration.ofHours(hours).plusMinutes(minutes)
val charged_at_time = now.plus(myDur)

```

There could be times where working with a duration might be easier, I’m not sure this is one of them.

But overall, I think it would make sense to just calculate the minutes and forget the hours. I think the following would work:

```auto
val time_to_charge = TeslaM3_time_to_full_charge.state as Number // It's better to cast to Number than DecimalType
val minutes = Math:round(time_to_charge * 60)
TeslaM3_time_charged.postUpdate(now.plusMinutes(minutes).toString())

```

Using a Duration gives you some convenience functions that might prove useful.

```auto
val time_to_charge = TeslaM3_time_to_full_charge.state as Number
val dur = Duration.ofMinutes(time_to_charge * 60)
// dur.toString() will print the duration in ISO8601 format, e.g. 6 hours 45 minutes would be PT6h45m
// dur.toHours() gives you just the hours
// dur.toMinutesPart() gives you the minutes after subtracting the hours
TeslaM3_time_charged.postUpdate(now.plus(dur).toString())

```

---

<div class="post-metadata">

**Author:** ![Max\_G](https://community.openhab.org/user_avatar/community.openhab.org/max_g/32/64598_2.png) [@Max\_G](https://community.openhab.org/u/Max_G)\
**Post date:** [November 11, 2022, 5:09am UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861/5 "2022-11-11T05:09:49Z")

</div>

> [@rlkoshak](#):
>
> ```auto
> TeslaM3_time_charged.postUpdate(charged_at_time)
> 
> ```

What I had…

In OH2 I had:

```auto
        spm_LastUpdate.postUpdate(new DateTimeType())

```

So I thought I stick the time into the brackets and should be OK; but it threw an error I can no longer trace, as I tried a few iterations.

Anyway, I put your suggested:

```auto
        TeslaM3_time_charged.postUpdate(new DateTimeType(charged_at_time))

```

… into the rule, and indeed it works.

Again, thank you for your detailed explanation… always appreciate your’s too rossko57 🙂

* * *

The Tesla API returns “time\_charged\_to\_set\_level” as decimal number. I am too lazy calculating the end time in my head, as it will change based on solar PV power levels.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 11, 2022, 2:39pm UTC](https://community.openhab.org/t/oh3-update-a-datetime-item-with-a-zoneddatetime/140861/6 "2022-11-11T14:39:08Z")

</div>

> [@Max\_G](#):
>
> The Tesla API returns “time\_charged\_to\_set\_level” as decimal number. I am too lazy calculating the end time in my head, as it will change based on solar PV power levels.

Right, but you can convert that number to just minutes and save a step calculating the hours. That’s the point of my reductions. now.plusHours(5).plusMinutes(10) is the same as now.plusMinutes(310).
