Now minus Date


very humiliating for myself, but I am not able to get it done… :frowning:

All I want to do is filling an item with the (milli)seconds from Now minus another item (DateTime type) in a rule.

How can I do this? I tried everything, like:

val DateTime datetimenow = new DateTime()
val datetimeupdate = new DateTime((ItemXXX.state as DateTimeType).calendar.timeInMillis)
postUpdate(ItemYYY_TimeAgo, datetimenow - datetimeupdate)

Thank you and cheers,

val millis = now.millis - (ItemXXX.state as DateTimeType).calendar.timeInMillis

Your ItemYYY_TimeAgo must be a Number Item.

Do you know how this will need to change now that calendar is deprecated on DateTimeType? I want to get in the habit of doing this correctly for newly developed code.

I believe this will work, but I’m not sure it’s the most straightforward way to do it.




I’ve not been posting it because it only works beyond a certain version of OH where the calendar is only marked as a warning for now. The

Once 2.2 is released I’ll probably start switching to the new way.

val millis = now.millis - ((ItemXXX.state as DateTimeType).getZonedDateTime.toEpochSecond * 1000)

I think that will work. I haven’t found hot to get at the milliseconds though, only seconds, hence the 1000.

Thank you @rlkoshak and @mhilbush for your replies. :slight_smile:

The first code worked, the second more modern version unfortunally not:

2017-12-13 21:12:00.043 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ItemYYY_TimeAgo: An error occurred during the script execution: The name '<XCastedExpressionImpl>.getZonedDateTime' cannot be resolved to an item or type.

The first code works without errors. Thank you!

If the second version is better because it will get depreaced in the future I will change to this one. But as you can imagine I do not have any clue why it is throwing this error.

Right, the second one will not work until you update to a version that starts complaining in the logs about calander being deprecated. The getZonedDateTime doesn’t exist in early versions of OH. That is why I’m not using the new way yet in my replies.

I see, thank you again! I will bookmark this topic to visit it after I upgrade to 2.2 :slight_smile:

@rlkoshak I just upgraded to OH 2.2, unfortunally the new code does not work:

2017-12-21 00:22:00.209 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Update TimeAgo': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 6, column 32, length 44

Any idea? The old code works but throws depreaced messages. Thank you a lot! :slight_smile:

Sorry. It works. The *_Update items were NULL as I changed a lot in the configs and did not restart OH. I restarted OH now, the items were filled through persistence, now everything works again.