DateTime to epoch rule example?

Hi helpers,

I was reading on forums, but couldn’t find exactly what I’m looking for.

My test:
-Door changes status to OPEN
-if a motion detected in last 5 min (for testing only) turn on the light

rule:

rule "Door opens - hue light"
when
    Item HallwayDoorSensorIsOpen changed from CLOSED to OPEN
then
    val Number KitchenLM = new DateTime(KitchenMotionSensorLastMotion).millis
    if( KitchenLM > now.minusMinutes(5).millis ) {
        KitchenLightSwitch.sendCommand("ON")
    }
end

But i receive an error:
Rule 'Door opens - hue light': No instant converter found for type: org.eclipse.smarthome.core.library.items.DateTimeItem

Can you help? :slight_smile:

See if this works…

    val KitchenLM = new DateTime(KitchenMotionSensorLastMotion.state.toString)
    if( KitchenLM.isAfter(now.minusMinutes(5)) ) {

What is that, an Item perhaps? You’d usually be interested in the item.state

Example of datetime handling here

There are other ways to accomplish the task with timers and no timestamps.

Yes, this is an DateTime item. I tried some options to convert KitchenMotionSensorLastMotion.state to epoch but some errors…

I was stuck at “Get Epoch from DateTimeType” as the KitchenMotionSensorLastMotion is a DateTime Item.

This is not doing the trick :slight_smile:
I receive:

Rule 'Door opens - hue light': 'isAfter' is not a member of 'java.lang.Number';

rule:

val Number KitchenLM = new DateTime(KitchenMotionSensorLastMotion.state.toString)
if( KitchenLM.isAfter(now.minusMinutes(5) ) {
    KitchenLightSwitch.sendCommand("ON")
}

Item events are like:

KitchenMotionSensorLastMotion changed from 2019-07-10T16:25:22.773+0200 to 2019-07-10T16:26:23.793+0200

Well no, KitchenLM is a number as you requested, and will not have an .isAfter method like a datetime.

You could convert both to numbers or both to datetimes for comparisons.

I see. Found my (stupid) mistake. Thanks for hints and help!

Please post your working version for the benefit of others!

This is it:

rule "Door opens - hue light"
when
    Item HallwayDoorSensorIsOpen changed from CLOSED to OPEN
then
    val KitchenLM = new DateTime(KitchenMotionSensorLastMotion.state.toString)
    if( KitchenLM.isBefore(now.minusMinutes(15)) ) {
        KitchenLightSwitch.sendCommand("ON")
        LivingRoomLightSwitch.sendCommand("ON")
    }
end

Thanks again.

1 Like