DateTime Conversion (openHAB 3.x)

#4 Get Java Time from DateTimeType
Tested in OH3, works fine:
val MyJavaTimeFromDateTimeItem = (MyDateTimeItem.state as DateTimeType).getZonedDateTime()



I had in OH 2.5 the following rule - that calculates my power consuption for one day:

rule “Strom Verbrauch Tag”
tem homematic_stecker_Energiezaehler_kwh received update
if(homematic_stecker_Energiezaehler_kwh.state instanceof Number){
Stromzaehler_Verbrauch_Tag.postUpdate(homematic_stecker_Energiezaehler_kwh.deltaSince(now.withTimeAtStartOfDay, “influxdb”) as Number)

Unfortunately i am not a programmer. Could someone please give me a hint on how to recreate the rule for OH3?




thanks - now it works

I replaced




I didn’t understand Java at all.
I need to convert


in oh3 format because now one rule dont work properly.

Many thanks


When rewriting my rules for OH3, I encountered a problem with the conversion between DateTimeType and Java Time: for some reason, when I convert Java Time to String, the name of my timezone is added to the string, e.g. 2021-01-01T07:00+01:00[Europe/Amsterdam]. This probably has to do with the time zone configuration of the Java environment. Obviously, this leads to problems when the string is parsed.

I also had a problem the other way around. When a DateTimeType was converted to a String, the string was missing a : in the time zone identifier:

DateTimeType to String: "2021-01-01T07:00:00.000+0100"
Java Time expects:      "2021-01-01T07:00:00.000+01:00"

I was able to solve both issues by using a formatter:

  val formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

  val morning_start = // 7:00

  // Java time to DateTimeType:

  // DateTimeType to Java time:
  val day_start = ZonedDateTime.parse(vSunrise_Time.state.toString, formatter) 

I hope this helps someone. I’d think this method is more robust in all cases, so it might be recommended to do this, even if you don’t encounter problems yet.


You can use the .toLocaleDateTime method to get the time with the time zone. Then you don’t the formatter.



now.toEpochSecond * 1000
1 Like


val zdt = Instant.ofEpochMilli(XXX).atZone(ZoneId.systemDefault())
val zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(XXX), ZoneId.systemDefault())
1 Like


val zdt = Instant.ofEpochMilli(XXX).atZone(ZoneId.systemDefault())
val MyDateTimeType = new DateTimeType(zdt)
1 Like

hey there

I’m having trouble to figure out the right syntax in OH3 for this:

if (now.isAfter((Sunset_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli) || now.isBefore((Sunrise_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)

Any hint? thank you very much

Sorry for asking, but I don’t get any further allthough reading this over and ober again…
Can somebody give me a code example how to store the time (now) in a variable, and then later calculate the difference in seconds to that stored value??


Something like this (?):
Post current time to item
My_Item.postUpdate(new DateTimeType())

Get Epoch from Item:
val Number item_millis = (My_Item.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

Get Now Epoch:
val Number now_millis = now.toInstant.toEpochMilli

Difference in Minutes:
var Number diff = (now_millis - item_millis)/60000


@dakoeli: This works for my setup very well! This is exactly what I needed in order to sum up my “uptime firing unit” for calculation of my energy consumption!

Thank you very much!

Thanks a lot!!!

I still don’t get it to work. I have made a TestRule which I trigger by Hand:

//val DateTimeType Epoche = now.toInstant()
val DateTimeType Epoche = now.toInstant()
logInfo("TestTrigger", "Epoche 2: " + Epoche)

createTimer(now.plusSeconds(3 )) [|
    val DateTimeType Spaeter = now.toInstant()
logInfo("TestTrigger", "Spaeter 4: " + Spaeter)
    val DateTimeType Differenz = Duration.between (Spaeter, Epoche)
logInfo("TestTrigger", "Differenz 5: " + Differenz)

// Here I need to convert from "DateTimeType" to a kind of Number. I want the time Difference in Millisecond or seconds......

This is working so far. I can calculate timedifferences between 2 given times. I get as result:

Differenz 5: PT-3.003164S

which is fine. Now I need this as Number, e.g. “3000” .
So how can the DateTimeType be converted to a Number???

Thanks again,

I would try the following

val Number millis_spaeter = Spaeter.getZonedDateTime.toInstant.toEpochMilli
val Number millis_epoche = Epoche.getZonedDateTime.toInstant.toEpochMilli

For rule internal internal calculations I would recommend to stay with the Java 11 classes like “ZonedDateTime” or “LocalDateTime”. The DateTimeType I would only use if I want to populate an Openhab Item with it.

(from the javadoc DateTimeType (openHAB Core 3.1.0-SNAPSHOT API))

@ljsquare You are right, thanks. I looked up the code of DateTimeType , and it turns out it’s only a thin wrapper around ZonedDateTime. So there is an easy conversion between the two, without the need for any String parsing or formatting.

I’ve rewritten my rule as follows:

  val morning_start = // 7:00

  // Java time to DateTimeType:
  vMorning_Time.postUpdate(new DateTimeType(morning_start)) 

  // DateTimeType to Java time:
  val day_start = (vSunrise_Time.state as DateTimeType).getZonedDateTime()

Much cleaner and less error prone.

@anfaenger May I suggest to rewrite section #3 of your start post, as follows?

#3 Get DateTimeType from Java Time

DateTimeType has a conventient constructor method that accepts Java java.time.LocalDateTime objects. So getting a DateTimeType if you have a java.time.LocalDateTime is as easy as:

val LocalDateTime myLocalDateTime = new LocalDateTime();
val DateTimeType myDateTimeType = new DateTimeType(myLocalDateTime);

Sorry for “hijacking” that chat, but I do not come any further.
My Rules for measuring “working times per day” don’t work under OH3 any more. (And I spent a Day now not getting them to work)
I have A switch which can be On or Off. I need to store the time when the switch goes high, and when it went back to low again. Then subtrated it and transfered the result to an Integer (Seconds).
Seconds = MakeSeconds(TimeNow - TimeBefore)

Maybe you can tell me how to do that OH3 conform?
Thanks in advance,