Store time in item, then calculate time past

I have put this together from a bunch of examples, but its not working… any ideas?

//in Items file defined as
DateTime plum_25_motionName_Last “Plum Basement Playroom [%1$ta %1$tR]”

//set last motion stamp with this in a rule
postUpdate( plum_26_motionName_Last, new DateTimeType() )

This does not work, trying to determine if its been more than 20 minutes since last motion

rule "Check Date Time Stamp"
when
Item Kitchen_Movement changed to ON
then
var DateTime dateTime = new DateTime((plum_26_motionName_Last.state as DateTimeType).calendar.timeInMillis)
if (dateTime.plusMinutes(30).isBefore(now)) {
// dostuff
}
end

Getting this error…

20:48:22.566 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘Kitchen_Movement’ received command ON
20:48:22.567 [INFO ] [marthome.event.ItemStateChangedEvent] - Kitchen_Movement changed from OFF to ON
20:48:22.636 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Check Date Time Stamp’: An error occured during the script execution: null
20:48:23.099 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘Kitchen_Movement’ received command OFF
20:48:23.099 [INFO ] [marthome.event.ItemStateChangedEvent] - Kitchen_Movement changed from ON to OFF

Unfortunately a DateTimeType is not a Joda DateTimeType. It doesn’t have the same methods as now (e.g. plusMinutes doesn’t exist on a DateTimeType). And the two are not all that compatible with each other so you have to get down to the milliseconds. So you need to switch around your test:

if(now.minusMinutes(30).isBefore(dateTime.timeInMillis))

@rlkoshak tried that and I am getting this error

13:33:07.876 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘Kitchen_Movement’ received command ON
13:33:07.877 [INFO ] [marthome.event.ItemStateChangedEvent] - Kitchen_Movement changed from OFF to ON
13:33:07.877 [INFO ] [lipse.smarthome.model.script.Kitchen] - 2016-11-16T12:11:24.860-0500
13:33:07.877 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Check Date Time Stamp’: An error occured during the script execution: null
13:33:08.341 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘Kitchen_Movement’ received command OFF

I am using the exact example here.

Here is my rule as it currently sits… (I tried it with .toString and without same error)

rule “Check Date Time Stamp”
when
Item Kitchen_Movement changed to ON
then
logInfo(“Kitchen”,plum_26_motionName_Last.state.toString)
var DateTime dateTime = new DateTime((plum_26_motionName_Last.state as DateTimeType).calendar.timeInMillis)
if(now.minusMinutes(30).isBefore(dateTime.timeInMillis)) {
logInfo(“Kitchen”,“*************** TEST TEST TEST TEST”)
}
end

A DateTime is a Joda class and it has different methods from DateTimeType.

So DateTimeType has a .timeInMillis and Joda has a .millis method.

It’s all confusing I know. But I highly recommend using Designer and take advantage of the <ctrl>-<space> to “discover” the methods on a class. For example, if you typed dateTime.<ctrl>-<space> you will get a little menu of all the valid ways to complete the statement.

So your if statement should actually be:

if(now.minusMinutes(30).isBefore(dateTime))

Which works because now and dateTime are both Joda DateTime Types.

sorry…Still having trouble (tried the designer, but it did not work for me, will give it another go)…

I must be missing something simple…let show you what I am doing again.

rule "Basement Hall Pots Motion"
when
Item plum_26_motionName changed from CLOSED to OPEN
then
postUpdate( plum_26_motionName_Last, new DateTimeType() )
end

############

rule “Check Date Time Stamp"
when
Item Kitchen_Movement changed to ON
then
logInfo(“Kitchen”,plum_26_motionName_Last.state.toString)
var DateTime dateTime = new DateTime((plum_26_motionName_Last.state as DateTimeType).calendar.timeInMillis)
if(now.minusMinutes(30).isBefore(dateTime)) {
logInfo(“Kitchen”,”*************** TEST TEST TEST TEST")
}
end

I still get the error…

14:58:09.370 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘Kitchen_Movement’ received command ON
14:58:09.371 [INFO ] [marthome.event.ItemStateChangedEvent] - Kitchen_Movement changed from OFF to ON
14:58:09.442 [INFO ] [lipse.smarthome.model.script.Kitchen] - 2016-11-16T12:11:24.860-0500
14:58:09.442 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Check Date Time Stamp’: An error occured during the script execution: null
14:58:09.749 [INFO ] [marthome.event.ItemStateChangedEvent] - zwave_serial_zstick_6f88206a_serial_sof changed from 2999 to 3000
14:58:09.750 [INFO ] [marthome.event.ItemStateChangedEvent] - zwave_device_6f88206a_node4_sensor_binary changed from ON to OFF
14:58:10.037 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘Kitchen_Movement’ received command OFF

Try skipping all these conversions and just do:

logInfo("Kitchen",plum_26_motionName_Last.state.toString)
if(now.minusMinutes(30).isBefore((plum_26_motionName_Last.state as DateTimeType).calendar.timeInMillis)) {
    logInfo("Kitchen","*************** TEST TEST TEST TEST")
} 

NOTE: It is a lot easier to read code when you wrap it in code fences so indentation is preserved.

```
code goes here
```
1 Like

Thank you! That worked perfect!