Conditional checking of time of day fails in JS11 vs JS 5.1

  • Platform information:
    • Release = Debian GNU/Linux 11 (bullseye)
      Kernel = Linux 5.10.0-14-amd64
      Platform = VMware Virtual Platform/440BX Desktop Reference Platform
      Uptime = 35 day(s). 9:35:12
      CPU Usage = 0.5% avg over 2 cpu(s) (1 core(s) x 2 socket(s))
      CPU Load = 1m: 0.00, 5m: 0.00, 15m: 0.00
      Memory = Free: 0.07GB (4%), Used: 1.86GB (96%), Total: 1.93GB
      Swap = Free: 0.71GB (76%), Used: 0.23GB (24%), Total: 0.95GB
      Root = Free: 3.57GB (25%), Used: 10.36GB (75%), Total: 14.70GB
    • Java Runtime Environment:
      openjdk 11.0.18 2023-01-17
      OpenJDK Runtime Environment (build 11.0.18+10-post-Debian-1deb11u1)
      OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Debian-1deb11u1, mixed mode, sharing)
    • openHAB version: 3.4.4 - Release Build
  • Issue of the topic: please be detailed explaining your issue
    • I have a script that I converted from ECMAScript to ECMAScript-2021 and I noticed that events were not triggering based on day of week. So I wrote a simple script to do a conditional test based on the day of week in javascript 11 versus javascript 5.1. The scripts are almost identical but the JS 11 version fails versus passing in the script written in JS5.1.

Javascript 11

this.ZonedDateTime = (this.ZonedDateTime === undefined) ? Java.type("java.time.ZonedDateTime") : this.ZonedDateTime;
currentday = ZonedDateTime.now().getDayOfWeek();
currenthour = ZonedDateTime.now().getHour();
currentmin = ZonedDateTime.now().getMinute();
console.log ("-------------");
console.log ("   Running Debug Date JS11");
console.log ("   Today is " + currentday);
console.log ("   Cleaning day is: " + (currentday == "SATURDAY"));
console.log ("-------------");

Javascript 5.1

this.ZonedDateTime = (this.ZonedDateTime === undefined) ? Java.type("java.time.ZonedDateTime") : this.ZonedDateTime;
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
currentday = ZonedDateTime.now().getDayOfWeek();
currenthour = ZonedDateTime.now().getHour();
currentmin = ZonedDateTime.now().getMinute();
logger.info ("-------------");
logger.info ("   Running Debug Date JS51");
logger.info ("   Today is " + currentday);
logger.info ("   Cleaning day is: " + (currentday == "SATURDAY"));
logger.info ("-------------");
  • If logs where generated please post these here using code fences:
2023-05-13 08:52:08.553 [INFO ] [tomation.script.ui.tempDateDebugJS11] - -------------
2023-05-13 08:52:08.554 [INFO ] [tomation.script.ui.tempDateDebugJS11] -    Running Debug Date JS11
2023-05-13 08:52:08.554 [INFO ] [tomation.script.ui.tempDateDebugJS11] -    Today is SATURDAY
2023-05-13 08:52:08.554 [INFO ] [tomation.script.ui.tempDateDebugJS11] -    Cleaning day is: false
2023-05-13 08:52:08.554 [INFO ] [tomation.script.ui.tempDateDebugJS11] - -------------
2023-05-13 08:52:17.665 [INFO ] [org.openhab.rule.tempDateDebugJS51  ] - -------------
2023-05-13 08:52:17.665 [INFO ] [org.openhab.rule.tempDateDebugJS51  ] -    Running Debug Date JS51
2023-05-13 08:52:17.675 [INFO ] [org.openhab.rule.tempDateDebugJS51  ] -    Today is SATURDAY
2023-05-13 08:52:17.677 [INFO ] [org.openhab.rule.tempDateDebugJS51  ] -    Cleaning day is: true
2023-05-13 08:52:17.678 [INFO ] [org.openhab.rule.tempDateDebugJS51  ] - -------------

Not sure what 5.1 is doing differently, but the issue is that getDayOfWeek doesn’t actually return a string and the comparison you perform isn’t capable of converting the value to a string automatically.

If you use:

console.log ("   Cleaning day is: " + (currentday.toString() == "SATURDAY"));

It should work.

Thanks. It work.

It is probably in l better to use the Helper Library in JS11 rather than raw Java. The Helper Library provides access to the joda-js libraries that let you work with date times in pure JS and does automatic conversion to Java ZonedDateTime for you as necessary.

var now = time.toZDT();
currentday = now.dayOfWeek();
currenthour = now.hour();
currentmin = now.minute();
console.log ("-------------");
console.log ("   Running Debug Date JS11");
console.log ("   Today is " + currentday);
console.log ("   Cleaning day is: " + (currentday == "SATURDAY"));
console.log ("-------------");

tim.toZDT() is kind of a universal conversation tool. Just about anything that it makes sense to convert to a ZDT can be passed. For example "00:15" returns a ZDT 15 minutes after midnight today and 8:00 AM returns a ZDT at 8 this morning. 1000, QuantityType 1 s, and "PT1S" all return a ZDT one second from now. You can pass it a DateTime Item, DateTimeType, Java ZonedDateTime, JS Date, ISO 8601 date time string, and more.

When you pass it nothing it returns now.