DateTime Conversion (openHAB 3.x)

Duration is the perfect class for it and you don’t have to stick to DateTimeType. Here is a good explanation for it java.time.Duration.

In your case you’d just have to add .getSeconds() that is

val Differenz = Duration.between (Spaeter, Epoche).toSeconds()

1 Like

Thank you, I will try that later…

Hello,

i need help. Since over 2 hours i want to transform an PY Script vom OH2 to OH3.

I have use before the joda time argument.
Now i only want reschedule a timer. Before i write:
.reschedule(now().plusSeconds(interval))

But i dont became a valid code. Im confused, what import statement is necessary and how i can use the NOW Stratement in Jython.

Help please, I am stuck.

This bit of code used to work in OH2.5 but doesn’t anymore since moving to OH3. It seems the formatter bit isn’t working.

import java.text.SimpleDateFormat  


val String timestamp_str = MobileAlertsHeatPumpSupplySensorTimestamp.state.toString
val SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a")
val DateTime timestamp = new DateTime(formatter.parse(timestamp_str))          
val Number elapsedtime_minutes = (now.toInstant().toEpochMilli() - timestamp.millis)/60000 //minutes
val Number elapsedtime_hours = elapsedtime_minutes/60 //hours

The input string is a time stamp and looks like this 1/8/2021 12:32:01 PM
I am trying to work out how much time has elapsed . The calculation was done in millis but it doesn’t have to be. What needs to be changed?

The error I get is:

failed: An error occurred during the script execution: null in mobilealertsTimestamp

Thx M

I got same issue. As I saw due to different configuration of jdk11 the strings including time zone in clear (+0100 [Eutope/Berlin]) at the end in my case.

I didn’t solve it until now, but maybe you let print the string in the logs to look if it the same?

How can I format a DateTime Item shown in a Cell of a Page?

Label says it accepts expression so I was thinking something like this would work?

- component: oh-label-cell
  config:
    item: LocalTime_DateTime
    label: =item.format("%1$tA, %1$tB  %1$td, %1$tY")
1 Like

I am trying to convert a String time stamp to Epoch in order to do a duration calculation. My code used to work in OH2.5 but doesn’t work in OH3.0. As already posted above.

I tried many things and wasted a lot of time with trial an error. The main things I observed is that the formatter approach doesn’t work at all. Direct conversion from ISO8601 String to Epoch doesn’t work.

The aproach I got to work, which is a bit ugly and requires a lot more code, are the following steps. I can post the code if anyone is interested:

Step1 Split time stamp string and reassemble into ISO8601 formated String with time zone
Step2 Convert ISO8601 String to DateTime
Step3 Convert DateTime to Epoch milliseconds
Step4 Compare current Epoch time to timestamp Epoch time

Hello Friends,

(Context: different behaviour of item.postUpdate vs postUpdate(item…)

I have a curious observation in relation to this, perhaps someone can use this information or educate me on something I have overlooked.

I repaired an old rule of mine to make it OH3 compatible. In this, I’m using dynamic item names which get filled with a DateTimeType.

Now, this works:
item0.postUpdate(currentDateTimeType)
item1.postUpdate(currentDateTimeType)

This leads to an error:
postUpdate("item0", currentDateTimeType)

This leads to the error:
failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.postUpdate(java.lang.String,java.lang.String) on instance: null in trashUpdate

Just in case someone runs in something similar :slight_smile:
Love the update btw and thanks a lot for all the work!

Just a guess:

Maybe

postUpdate("item0", currentDateTimeType.toString)

would do the job. But the method is the better option anyway…

1 Like

You’re right, @Udo_Hartmann - that’s curious behavior indeed!
Generally, I completely agree with you that the method is the preferred way - in this case though I have a dynamic item name and had to use some very dirty if/else fiddling to clear that up.
(And yes: That kind of logic shouldnt be done in the rules, but I cant figure out sorting in the frontend so I worked around my skillset more than what would make sense from an architectural point of view :D)

Thanks for the hint!

I’m pretty sure this can be done in a more elegant way. A typical issue is to set one Item dependent to another Item, both items match through parts of the name. I’m using groups for that purpose.

postUpdate() as an action is defined as action(String,String), and openHAB obviously is not aware of .toString as method to convert to String. Why? I have no idea…

1 Like

Hm perhaps you have an idea for a more elegant approach I’m always happy to learn:

The use case is an overview over the next dates when the trash is being picked up, sorted by date. My approach is pure brute force: I have four trashlabel string items and four trashdate datetime items.
I scrap a public ics calendar for my “trash types”, sort them by date and write them into the four items, soonest in trashlabel0 and trashdate0 up to label3 and date3.

I’m aware that this approach is barbaric (but working!) and perhaps leading too far for a discussion here… Let me know if I should delete/move!

Cheers,
Georg

Yes, would be better to move to an extra discussion. Maybe some of the @moderators could split the discussion?

Anyway, given the items, if passing all 8 items to a group gTrash, you could sort the date items this way:

myList = gTrash.members.filter[d|d instanceof DateTimeType].sortBy[ "state" ]

now myList contains all DateTime Items of the Group gTrash, sorted ascending (if I remember it correctly)
for getting the text, you have to do something like this:

myList.forEach[i|
    mytext = gTrash.members.filter[s|s instanceof StringType && s.name.endsWith(i.name.right(1))].head.state.toString
]

I’m not 100% sure about right(n), though. I’m using Itemnames like this: myText_0 myText_1 and so on. Then I can use split("_").get(1) to get the second part. But I think you got it anyway.

Would be great if you could post the code since I have the same problem coming from OH 2.5

Will such rule with timer will work in OH3:

rule "Test rule 1"
when
    Item test_item_1 changed to OFF
then
    createTimer(now.plusSeconds(15)) [|
		test_item_2.sendCommand(OFF)
    ]
end

or such way to define wariable:

var VARIABLE = ITEM.averageSince(now.minusHours(24)) as Number

I don’t have OH3 yet but want to be prepared for it.

Ask for help!
var org.joda.time.DateTime easterSunday = parse (year + “-” + month + “-” + day) as in java?

I now figured out that on an Item you can add State Description Metadata and in that you can set Pattern to the formating you want. This will be reflected on the Item and in Pages but not in the Items list.

I believe you want to determine the holiday, so you also need stAdvent. I’ am not really sure if it is in the best way, but it works for me

var LocalDate easterSunday =  ZonedDateTime.parse(year + "-" + month +"-" + day + "T00:00:00.000Z").toLocalDate()
var LocalDate stAdvent = ZonedDateTime.parse(year + "-12-25T00:00:00.000Z").minusDays(ZonedDateTime.parse(year + "-12-25T00:00:00.000Z").getDayOfWeek.getValue + 21).toLocalDate()

Thanks for the quick answer, I’ll test it and then mess it up

unfortunately I get an error at stAdvent
The value of the local variable stAdvent is not used (org.eclipse.xtext.xbase.validation.IssueCodes.unused_local_variable)