How to convert DateTime-Item (DateTimeType) to DateTime in rules

Thanks - I’m on OH 2.5.3 so this was helpful:

Using that, these work to get hours:

rule "testing"
when
    System started
then
    logInfo("rules", "Testing")

    // now is Joda DateTime class
    var hours = now.getHourOfDay()
    logInfo("rules", hours.toString())

    // OfficeMotion_time is a DateTime object so convert to Joda
    val jodaValue = new DateTime((OfficeMotion_time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
    var hours_2 = jodaValue.getHourOfDay()
    logInfo("rules", hours_2.toString())

end
1 Like