DateTime Conversion (openHAB 3.x)

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.

Astro binding with offset example is here if you are interested:

1 Like

Thanks for your reply - I changed the coding but now I get the following error. Somehow the getZonedDateTime is not recognized (all together there are two errors within the first line of code)

(And yes your assumption is right - DateTime Item and Number item is correct!)

The method getZonedDateTime() is undefined for the type DateTimeItem(org.eclipse.xtext.diagnostics.Diagnostic.Linking)
Cannot cast from NumberItem to Number(org.eclipse.xtext.xbase.validation.IssueCodes.invalid_cast)

In both cases, you’d be wanting to operate on the state of the Items, e.g.
(Sunrise_Time.state as DateTimeType).getZonedDateTime
Post #1, example #4

1 Like

Thank you so much for your help! That solved my problems.

Hello, if i can hook myself into this thread, i have a problem with importing the org.joda.time library - (from org.joda.time import DateTime).

When starting the script i get the message that the library cannot be found. I assume that i will have to manually download the library from https://www.joda.org/ and then move the library to a local folder.

Could someone please tell me what exactly has to be done, i.e. which part of the download (zip-folder) i have to move where so that the library is correctly recognized. It should also be mentioned that openHAB runs in a Docker container with version 3.x.

Many thanks in advance!
Christoph

Start a new thread for that, this one is all about avoiding use of joda.

Start a new thread for that, this one is all about avoiding use of joda.

Oh you’re right, sorry for that :sweat_smile:

Hopefully, this is the right spot - another quick example.
I was wrapping my head around all day:

old rule syntax in openhab2.x

Explanation of the condition:
is true only if hour of day is between 19 and 22.

if((now.isAfter((now.withTimeAtStartOfDay.plusHours(19))) && now.isBefore(now.withTimeAtStartOfDay.plusHours(22)) {
		sendCommand(blablabla)
}

new rule syntax in openhab3:

if ((LocalDateTime.now().isAfter(LocalDate.now().atTime(19, 0))) && (LocalDateTime.now().isBefore(LocalDate.now().atTime(22, 0)))) {
	sendCommand(blablabla)
}

Well at least its one way with an example that i needed - so i leave this here for everyone who don’t know this crazy java syntax - like me :wink:

Update: As @ubeaut brilliantly pointed out, here is the code syntax example for such a rule that could be pasted in the code section:
image

triggers:
  - id: "1"
    configuration:
      itemName: MyItemName
      state: ON
    type: core.ItemStateChangeTrigger
  - id: "4"
    configuration:
      command: ON
      itemName: MyItemName
    type: core.ItemCommandTrigger
conditions:
  - inputs: {}
    id: "3"
    configuration:
      startTime: 18:00
      endTime: 19:00
    type: core.TimeOfDayCondition
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: sendCommand(MyItemName,
        MyItemNameControl.state.toString)
    type: script.ScriptAction

Or you just use the but only if in the rule?

image

2 Likes

Oh boy… Well, that’s the problem if you’re to focused on one thing… You don’t see the things right in front of you…

I didn’t even thought about that. Tanks mate for the hint!

I’m getting to old for this :smile:

I am older than you.
60 plus…
I go for the easy way. :grinning_face_with_smiling_eyes:

1 Like

I upgraded to OH3 2 days ago and im getting so fed up with this that i’m about to downgrade and reinstall OH2.5. Non of my scripts converted and half the house was dark last night because i use quite some java datetime in all scripts so they just don’t run anymore.
I will have to rebuild all scripts from scratch.
even your suggestions of val currMonth = now.getMonthOfYear → val currMonth = now.getMonth end up giving errors.
I even get errors for cancelled out lines. → failed: //executeCommandLine