Calculate how many minutes in the last 24 hours the windows of a room have been opened

Hi there,
does anyone has an idea how to implement a ECMAScript 2022+ file based automation rule fo the following:
I want to calculate how many minutes in the last 24 hours the windows of a room have been opened.

So maybe save everytime a window closes the minutes it was open in a number item OpenDuration_Room and at then sum up all the minutes between the las 24 hours.

My problem is that I don’t come along with the time parameters in the javascript rules… So even calculating the minutes between last timestamp and now doesn’t work.

Does anyone has a solution here for me?

You could try to use persistence and averageSince. If window open = 1 and window close = 0 and averageSince( 24 h ) = 0.1, then the result is 24 * 60 min * 0.1 = 144 min.


@Ap15e probably has the solution but as for calculating with time see the add-on docs. In particular JavaScript Scripting - Automation | openHAB and do not neglect to follow the link there to the joda-js library docs. The class you are looking for will be referenced as time.Duration. These classes are almost exactly the same as the Java classes so tutorials like DateTime Conversion (openHAB 3.x) will also apply almost unchanged. Though you can ignore a lot of the stuff talking about how to convert something to a ZonedDateTime. In JS Scripting time.toZDT() can convert just about anything that can be converted to a ZonedDateTime into a ZonedDateTime for you.

Some additional remarks re persistence:
Results depend on persistence strategy and database used. For highest accuracy use a lossless database (e.g. influxdb - the default database rrd4j is lossy) and strategy everyChange.

Whow thats a lot of great hints and ideas! Thank you all.
@Ap15e: Thats a very neat and easy solution with need much less calculation then my own idea.
You think rrd4j will be enough for the last 24hours? Are do I still need to change to influxdb?
@rlkoshak: Thanks for pointing me out to all the documents. I played around with exact these last evening but couldn’t get at Duration between to Dates in Minutes. I was always getting errors. I tried it with until, with Duration and with Date1-Date2/1000/60. These Datetime are always complicated. There can’t be enough examples in the docs for me. :slight_smile:

Just try it - if it doesn’t meet your requirements, fine-tune the rrd4j parameters or use influxdb.

time.Duration.between(datetime1, datetime2).toMinutes();

Whow thats easy! Thank you so much!