Calculation times with DateTime items

Hi,

I have a DateTime item and now I want to check if the time stamp is more than half an hour ago.
The Designer says this is not possible (Cannot cast element of sealed type java.lang.String to org.joda.time.DateTime):

val DateTime timestamp = datetimeitem.state as DateTime
if( now.isAfter( datetimeitem.plusMinutes(30))) {
    //dostuff
}

How do I do the correct cast to yodaTime?

I’m working on an examples wiki page for a binding that has a little time logic in it, still a work in progress, but this would do it:

import org.joda.time.*
import org.openhab.core.library.types.*

// ...
var DateTime dateTime = new DateTime((datetimeitem.state as DateTimeType).calendar.timeInMillis)
if (dateTime.plusMinutes(30).isBefore(now)) {
  // dostuff
}
3 Likes

Thank you for your reply. I’ll try this out this evening.
How do you come up with his? How could I find a solution like this myself?

I think I found examples here as a starting point.

I tried it and while it works in one rule, the Designer gives me errors in another one.
So I found a simpler way for measuring the time between to events:

var long last_change    = now.millis

rule "duration"
when
    Item item_which_duration_you_want_to_measure changed
then
    //duration as Number in minutes
    var Number min    = ((now.millis - last_change) / 60000)
    ts_state_change    = now.millis
end
1 Like