DateTime Conversion (openHAB 3.x)

Thanks to @rlkoshak I was able to figure this out for my use case. Here are two examples that work for me.

val day_start = (AstroSunData_Rise_StartTime.state as DateTimeType).getZonedDateTime()
val evening_start = (AstroSunData_Set_StartTime.state as DateTimeType).getZonedDateTime()
4 Likes

Thank you and @rlkoshak! It works.

1 Like

@ljsquare
maybe you can share the solution here ?

This is the solution:

A post was split to a new topic: Time conversion

#5:

val MyZoneDateTime = ZoneDateTime.parse("2020-12-25T00:00:00.000Z").withZoneSameInstant(ZoneId.systemDefault())
1 Like

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

3 Likes

Hello,

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

rule “Strom Verbrauch Tag”
when
tem homematic_stecker_Energiezaehler_kwh received update
then
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

KR
Michael

2 Likes

thanks - now it works

I replaced

now.withTimeAtStartOfDay

with

ZonedDateTime.now().with(LocalTime.MIDNIGHT)

2 Likes

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

now.getWeekOfWeekyear

in oh3 format because now one rule dont work properly.

Many thanks

Hi,

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 = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(7) // 7:00

  // Java time to DateTimeType:
  vMorning_Time.postUpdate(DateTimeType.valueOf(morning_start.format(formatter))) 

  // 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.

5 Likes

You can use the .toLocaleDateTime method to get the time with the time zone. Then you don’t the formatter.
See:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html#toLocalDateTime()
And
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDateTime.html

#7

now.toInstant.toEpochMilli
now.toEpochSecond * 1000
1 Like

#6

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

#2

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??
Thanks,
Ingo

2 Likes

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

3 Likes

@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!