DateTime Conversion (openHAB 3.x)

thanks!

Hi,

im trying to get the time conversion/formatting done. This is my code for getting the DtaeTime as String

 val String dts = now.plusHours(72).toLocalDateTime().toString()
       plansprinklerNextStartMiR.postUpdate(dts)

Now i am frustrated in not getting this output "2021-05-07T16:49:37.451418 " into something like this “2021-05-07 16:49”
The working OH 2.5 version is this

        var planbewtimenext = now.plusHours(72)
        plansprinklerNextStartMiR.postUpdate(planbewtimenext.toString("yyyy-MM-dd' 'HH:mm")) 

It must be just a small step to get this, but i cant get it working with googling and searching this forum.
Any Help is appreciated.

Cheers M.

See post about DateTimeFormatter, just two posts above yours.

Hi rossko, i did try but didnt succeed

Show us your attempt.

Hi,

here you go :slight_smile:

val  dts = now.plusHours(72).toLocalDateTime().toString()
       
       plansprinklerNextStartMiR.postUpdate(dts)
       plansprinklerNextStartMiRFormat.postUpdate(plansprinklerNextStartMiR.format(DateTimeFormatter.ofPattern("HH:mm,dd.MM"))

Hi, got it working :slight_smile: Sometimes talking about something helps :slight_smile:

Note that if these are Items, postUpdate is asynchronous. It taks a finite time to happen and the rule does not stop and wait.
If you read the Item state in the next line you will almost certainly get the “old” state.

Hi rossko,

thnx a lot. How would you recommend to do this? Is there a way to always get the “new” state?
One way to do this would be with a timer of a few seconds. But it looks a bit “overpowered” for a simple timestamp.

Cheers
M.

Well, take a look


plansprinklerNextStartMiR.postUpdate(dts)
plansprinklerNextStartMiRFormat.postUpdate(plansprinklerNextStartMiR...

You know exactly what the new state of plansprinklerNextStartMiR will be because you’ve just posted it - it is in the variable dts
Don’t over complicate things.

Hi all,

I seem to be struggling as well in the DateTime-zone
 Have the Locale set correct, Timezone is set correct but when my TimeOfDay rule fires, it fire on UTC times and not local times.

An extract from the rule:


        System started or // run at system start in case the time changed when OH was offline
        Channel 'astro:sun:home:rise#event'    triggered START or // For the day
        Channel 'astro:sun:home:set#event'     triggered START or // For the evening
        Channel 'astro:sun:minus90:set#event'  triggered START or // For afternoom
        Time cron "0 1 3 * * ? *" or // one minute after 3 o'clock so we don't get interferred with clockchanges
        Time cron "0 0 6 * * ? *" or // For the morning
        Time cron "0 0 23 * * ? *" // For the night

then

  logInfo(logName, "Calculating time of day...")

  // Calculate the times for the static tods and populate the associated Items
  // Update when changing static times
  val morning_start = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(6)
  morningTime.postUpdate(new DateTimeType(morning_start))

  val night_start = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(23)
  nightTime.postUpdate(new DateTimeType(night_start))

  // Convert the Astro Items to Joda DateTime
  val day_start = (sunriseTime.state as DateTimeType).getZonedDateTime()
  val evening_start = (sunsetTime.state as DateTimeType).getZonedDateTime()
  val afternoon_start = (afternoonTime.state as DateTimeType).getZonedDateTime()

And from the logfile:

2021-05-20 03:42:00.003 [INFO ] [penhab.core.model.script.Time Of Day] - Calculating time of day...

2021-05-20 03:42:00.008 [INFO ] [penhab.core.model.script.Time Of Day] - Morning start: 2021-05-20T06:00Z[Etc/UTC]

2021-05-20 03:42:00.009 [INFO ] [penhab.core.model.script.Time Of Day] - Day start: 2021-05-20T05:42+02:00

2021-05-20 03:42:00.011 [INFO ] [penhab.core.model.script.Time Of Day] - Afternoon start: 2021-05-20T20:04+02:00

2021-05-20 03:42:00.012 [INFO ] [penhab.core.model.script.Time Of Day] - Evening start: 2021-05-20T21:34+02:00

2021-05-20 03:42:00.014 [INFO ] [penhab.core.model.script.Time Of Day] - Night start: 2021-05-20T23:00Z[Etc/UTC]

2021-05-20 03:42:00.015 [INFO ] [penhab.core.model.script.Time Of Day] - Calculated time of day is NIGHT

2021-05-20 03:42:00.019 [INFO ] [hab.core.model.script.StaircaseLight] - Time of day = NIGHT

2021-05-20 06:00:00.192 [INFO ] [penhab.core.model.script.Time Of Day] - Calculating time of day...

2021-05-20 06:00:00.198 [INFO ] [penhab.core.model.script.Time Of Day] - Morning start: 2021-05-20T06:00Z[Etc/UTC]

2021-05-20 06:00:00.199 [INFO ] [penhab.core.model.script.Time Of Day] - Day start: 2021-05-20T05:42+02:00

2021-05-20 06:00:00.201 [INFO ] [penhab.core.model.script.Time Of Day] - Afternoon start: 2021-05-20T20:04+02:00

2021-05-20 06:00:00.203 [INFO ] [penhab.core.model.script.Time Of Day] - Evening start: 2021-05-20T21:34+02:00

2021-05-20 06:00:00.204 [INFO ] [penhab.core.model.script.Time Of Day] - Night start: 2021-05-20T23:00Z[Etc/UTC]

2021-05-20 06:00:00.208 [INFO ] [penhab.core.model.script.Time Of Day] - Calculated time of day is DAY

2021-05-20 06:00:00.212 [INFO ] [hab.core.model.script.StaircaseLight] - Time of day = DAY

We can see that the Day-start is set as Day start: 2021-05-20T05:42+02:00 but it fires at 03:42. Exactly my timezone difference earlier.
Any ideas please, I’m stuck :grimacing:

Did you try example #3 variant A from the top of the page? You can probably remain in LocalDateTime all the time


I would try rewriting this:

  val morning_start = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(6)
  morningTime.postUpdate(new DateTimeType(morning_start))

into:

  val morning_start = LocalDateTime.now().with(LocalTime.MIN).plusHours(6)
  morningTime.postUpdate(new DateTimeType(morning_start))

Note that I also replaced LocalTime.MIDNIGHT with LocalTime.MIN.

There are three different places to set timezones and/or locales.
Your host operating system, your openHAB system, 
 and the Java system in between.
Logs are timestamped according to Java clock, and may disagree with openHABs clock.

This didn’t work unfortunately.

In the mean time I have found out my log files are in UTC.
Further I found when looking in my items-section that my
day_start, evening_start and afternoon_start are shown as UTC-times but do trigger on the correct moment. How do I get them to show local time (the items)? If I now implement them in my page, they are offset by two hours :expressionless:

I have just upgraded from OpenHab 2 and I want to simply format the current time as a string (such as 22-May 22:49)

I had the following code in a rule and this now outputs a long unformatted timestring

    val timestamp = now.toString("dd-MMM HH:mm")
    logInfo("log.rules", timestamp)

I have tried many things, such as #11 and variations on SimpleDateFormat, but to no avail

I must be missing something trivial as this cannot be too difficult


EDIT:
The following works, but is there a simpler solution (without an import)?

import java.time.format.DateTimeFormatter

    ...
    val timestamp = now.format(DateTimeFormatter.ofPattern("dd-MMM HH:mm"))
    logInfo("log.rules", timestamp)
    ...

Sure.

val timestamp = now.format(java.time.format.DateTimeFormatter.ofPattern("dd-MMM HH:mm"))

You’d have to do that in a UI entered DSL rule anyway.

1 Like

Thanks, I did not know that. That will kill some more imports :slight_smile:

Got it more or less fixed due to this post. Should anyone else struggle


Hello experts,

can you please help me out with my rule? IÂŽm struggeling with openhab3 and the following code in a rule:

// set the default irrigation hour to X hours before the sunrise
		val localSunRise = new DateTimeType(Sunrise_Time.state.toString).minusHours((IrrigationHoursBeforeSunrise.state as Number).intValue)
		var Number wateringHour = localSunRise.getHourOfDay()
		var Number wateringMinute = localSunRise.getMinuteOfHour()

In Visual Studio Code the “.minusHours” is having a red bottom line with the following message:

The method minusHours(int) is undefined for the type DateTimeType(org.eclipse.xtext.diagnostics.Diagnostic.Linking)

I already changed the DateTime item to DateTimeType accordingly to OH 3 documentation but how to substract the hours in my case?

Many thanks in advance!

I assume Sunrise_Time is a DateTime Item and IrrigationHoursBeforeSunrise is a Number Item.

val localSunrise = Sunrise_Time.getZonedDateTime().minusHours((IrrigationHoursBeforeSunrise as Number).longValue)
val wateringHour = localSunrise.getHour()
val wateringMinute = localSunrise.getMinute()

Of course, one could just configure the Astro binding with an offset and not even need to do this calculation.