DateTime Conversion (openHAB 3.x)

try this:

rule "Check and Reload ComfoAirQ Homie"
when
    Time cron "0 0/5 * * * ?"
then

    var Ventilation = (vQ_Ventilation_Gateway_Connection_State_Last_Update.state as DateTimeType).getZonedDateTime()

    if (vQ_Ventilation_Gateway_Connection_State_Last_Update.state == NULL) {
        logInfo("Check and Reload ComfoAirQ Homie", "vQ_Ventilation_Gateway_Connection_State_Last_Update = NULL");
    } 
    
        if (now.minusMinutes(3).isAfter(Ventilation)) {
        // vEQ_Ventilation_Gateway_Reload_Timer.sendCommand(ON)
        logInfo("homie_rules", "Reload Homie Devices")
        mqttActions.publishMQTT("homie/zehnderq450gateway/controls/reload/set","true", false)
    }
end
1 Like

That seems to work. So if I understand correctly, the solution was to convert some of the logic to a variable and use that?

yes, but also convert from a DateTimeType correctly to a ZonedDateTime() at the same time. You could do it in just one line I suspect but this is my preferred option for more clarity in the rule. ( just my 2 cent on this )

3 Likes

I appreciate the suggestions. It has been a learning experience.

Hello folks,
can you please give me a hand how to convert in OH3 this from OH2?

now.toString("dd.MM")

?
now.toInstant.toString(“dd MM”) does not do the jobm nor error either :wink:

Thanks

Example of use of DateTimeFormatter here

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: