OH2 to OH3 rule DateTime rule porting from joda to Java time

Hey,
I’ve just moved from OH2.5 to OH3.4.4 after many years of stable work.
Migration was smoother than I expected, some minor hiccups with rules except one which I can’t get my head around. I read obviously many posts about ‘how to’, apparently many people suffer from it, and probably it’s my brain limitation that I can’t still get this working.
Background
I have eBus binding and one of the items is controller which has time/date in it. After some time it gets out of sync, not the place here to explain what exactly I used it for.
In OH2.5 I had
item:

DateTime VaillantVRC470Time "Time [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]" <time>

rule snippet:

import org.joda.datetime.DateTime
[...]
var DateTimeType timeVRC = VaillantVRC470Time.state as DateTimeType
var Number diff = Minutes::minutesBetween(new DateTime(timeVRC.zonedDateTime.toInstant().toEpochMilli), now).getMinutes()
        if (diff >= 5) {
[...]

In OH3 I get error messages that Minutes are unknown (don’t have exact error but I don’t think it’s important here, I think it’s obvious mapping to Java time is needed regardless; if error makes a difference I can collect it).
Question
Can someone give me example of how to map/find minutes difference in my case?
Thanks in advance!

You need the Duration class to calculate the amount of time between to Instants.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html

You also need to get rid of that import.

thanks @rlkoshak ! this was easier than expected with right Class used.

Hi,

I need your help. I’m not able to get this script running. (Rule: application/javascript)
Made a short test version:

//import java.time.Duration
//import org.joda.datetime.DateTime

var now

// just to see, if the script runs
now=500 
events.postUpdate("Handy_OffDuration",now)

When I uncomment any of the import lines, the script does not run. Do I have to import/install the libraries first???

Thank you for your help!
Peter

Well, this is not proper JavaScript :slight_smile:

You don’t say which version of OH you are running. If OH 3.4 and that rule type you have to use the standard Nashorn JavaScript method to import Java classes:

var Duration = Java.type('java.time.Duration');

Given this is your first post it means you are just getting started. In that case I strongly recommend against using Nashorn JS and instead use the much more up to date JS Scripting add-on. In OH 3.4 the MIME type for that is application/javascript:ECMAScript 2021 (or something like that) and in OH 4 that becomes the default application/javascript and Nashorn becomes application/javascript:ECMAScript 5.1.

If you use the JS Scripting add-on instead, see the reference docs for the add-on which has pretty much everything you need documented.

It is a much better development environment, not the least in that it provides a decade newer version of JavaScript.

Still with 3.0.1. Trying to update to 3.4.

Thank you!
Peter