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
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?
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
(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
Love the update btw and thanks a lot for all the work!
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)
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…
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!
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:
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.
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.
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)