openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

setTimeout only accepts number of millis, add back documentation to create OH timer that accepts ZDT #119

Closed rkoshak closed 2 years ago

rkoshak commented 2 years ago

As currently implemented, setTimeout() only supports the use case of creating a timer based on a delta/delay from now. However, it does not support absolute scheduling of a timer based on a full DateTime (e.g. scheduling a timer based on the state of a DateTime Item or the result of some sort of calculation). Because it doesn't support this latter use case, and I'm not sure it would be a good idea to add support for passing a Joda ZDT to setTimeout() which would break the standard, I propose we add back documentation for how to create a native OH Timer to handle that missing use case.

Otherwise, if we insist on only "officially" supporting creating timers based on a delay in milliseconds I think that's a step backwards. It forces the end users to do all sorts of calculations and time manipulations.

digitaldan commented 2 years ago

Wouldn't you use the newly added .getMillisFromNow() when passing into a setTimeout call?

rkoshak commented 2 years ago

I never looked at what that function does.

It still seems a bit awkward but I guess doable. If I wanted, for example to set a timer for 13:02 it would be something like

setTimeout(() => { do something }, time.toZDT('13:02').getMillisFromNow());

It'll be a hangup for users who are migrating from one of the other rules languages but not insurmountable.