DateTime Conversion (openHAB 3.x)

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

In fact, you used joda time, which is deprecated :slight_smile: you have to use javaTime from now on.

1 Like

That

is because of the changed syntax of executeCommandLine which is mentioned during the upgrade to OH3 and in the breaking changes section of the release notes.

2 Likes

Thanks but not my point. why would it bother with cancelled out lines. It should ignore that line after the double line. I ended up downgrading. I will try this again in the future and maybe combine it with some shiny new hardware and then a bit slower and planned. Upgrading to 3.0 is going to take some time. Just need to plan for it.

HI All, just moved over to OH 3.
Im sorting out all the small issues but there is one rule i just cant seem to get working.
**Im posting below the rule that worked on OH2.5 but if someone could point me in the right direction to make it work for OH3 i would highly appreciate it.

Items:
//up time//
String OH_Uptime
String OH_Uptime_HumanReadable

Rule:

rule “Openhab Uptime”

when

System started

then

postUpdate(OH_Uptime, new DateTimeType(now.toString))

end

rule “Openhab Uptime readable”

when

Item OH_Uptime changed or

Time cron "1 1 * * * ?" 

then

var String tmp

    

if(OH_Uptime != NULL) {

    var DateTime dateTime_OH_Uptime = new DateTime((OH_Uptime.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)

    var diff = now.millis - dateTime_OH_Uptime.millis

    

    val Number SECOND_MILLIS = 1000;

    val Number MINUTE_MILLIS = 60 * SECOND_MILLIS;

    val Number HOUR_MILLIS   = 60 * MINUTE_MILLIS;

    val Number DAY_MILLIS    = 24 * HOUR_MILLIS;

    if (diff < MINUTE_MILLIS) {

        tmp = "Just now"

    } else if (diff < 2 * MINUTE_MILLIS) {

        tmp = "a minute"

    } else if (diff < 50 * MINUTE_MILLIS) {

        tmp = String::format("%.2f", diff / MINUTE_MILLIS) + " Minutes"

    } else if (diff < 90 * MINUTE_MILLIS) {

        tmp = "an hour ago"

    } else if (diff < 24 * HOUR_MILLIS) {

        tmp = String::format("%.2f", diff / HOUR_MILLIS) + " Hours"

    } else if (diff < 48 * HOUR_MILLIS) {

        tmp = "since yesterday"

    } 

    else {

        tmp = String::format("%.1f", diff / DAY_MILLIS) + " Days"

    }

    OH_Uptime_HumanReadable.postUpdate(tmp.toString)

      }

end

The now.toString is redundant. You never needed it.

OH_Uptime.postUpdate(new DateTimeType)

See Rules | openHAB, it is recommended to use the function instead of the Action where possible.

var DateTime dateTime_OH_Uptime = new DateTime((OH_Uptime.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)

As the original post indicates, there is no such thing as DateTime any more in OH 3. You need a ZonedDateTime and as shown in #4 there is a getZonedDateTime function on DateTimeType.

var diff = now.millis - dateTime_OH_Uptime.millis

See #7 to get epoch from both now and your variable.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.