DateTime Calculations in Openhab 3.2

DateTime Calculations in Openhab 3.2

Hello forum,
apolgies upfront for this very basic Q and confusion I am having right now with DateTime in oH3.2

I am currently running Openhab 3.2 on a Pi3 and trying to calculate the difference (ideally in minutes) between 2 timestamps (DateTime) when one of my
digital inputs changes. There are many examples here in the forum, for me none worked. I believe these are compatability issues with older Openhab versions (?)

Ideally I would like to have a very simple rule with JS (CMAScript (ECMAScript 262 Edition 11) that gives me the time difference.

This here is my last try which throws an error like “getZoneDateTime is not a funcion”

var pumpe_ein = items.getItem(“BrunnenPumpe_timestamp_EIN”).state;
var pumpe_aus = items.getItem(“BrunnenPumpe_timestamp_AUS”).state;

var diff = java.time.Duration((pumpe_ein.state).getZonedDateTime(), (pumpe_aus.state).getZonedDateTime());
var diffSecs = diff.seconds();
var diffStr = diff.toString();


Try it with getTime(). The result is in milliseconds.



In the updated JS scripts (ECMAScript 262 Edition 11), items.getItem(some_item_name).state returns a stringified version of the state. In this case, getZonedDateTime is not a string method.

In order to get the non-transformed version of the state you need to use .rawState instead of .state. The raw state can then be successfully transformed with getZonedDateTime().

1 Like

Thank you very very much Justin, I was about to give up …

You made my day and hopefully also for many others wondering why it is suddenly so difficult to substract 2 timestamps.

.rawState instead of just calling .raw does the trick.

For everyone who is using Openhab 3.2 with the ECMAScript 262 Edition 11 scripting trying to get the diff from two timestamps, this here worked for me:

var pumpe_ein = items.getItem(“BrunnenPumpe_timestamp_EIN”).rawState.getZonedDateTime();
var pumpe_aus = items.getItem(“BrunnenPumpe_timestamp_AUS”).rawState.getZonedDateTime();

var diff = pumpe_ein - pumpe_aus;