Blocky how to use day of the Week? (MON, TUE, WED....)

Hello Colleagues,

I migrated from OH3 → OH4
One of my rule need to calcualte the Day of the Week (MON, TUE, WED…)

Initially I used a code function like this:


function getDayOfTheWeek(myZDT) {
  retval = (time.toZDT(myZDT)).getDayOfWeek();
  return retval;
}
org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).toZDT(...).getDayOfWeek is not a function

Now I tried this:
image

but it returns:

org.graalvm.polyglot.PolyglotException: JsJodaException: Pattern using (localized) text not implemented, use @js-joda/locale plugin!

Whole JS Code:

var DurationMowerMinutes, myZDT, oldDateObj, minutes, AmountofRuns, retval, StartMowingTime, WeekdayShort, BaseWeekDayProp;

var thread = Java.type('java.lang.Thread')

// Describe this function...
function getHour(myZDT) {
  retval = myZDT.getHour()
  return retval;
}

// Describe this function...
function getMinute(myZDT) {
  retval = myZDT.getMinute()
  return retval;
}

// Describe this function...
function getDayOfTheWeek(myZDT) {
  retval = (time.toZDT(myZDT)).getDayOfWeek();
  return retval;
}


DurationMowerMinutes = 69;
AmountofRuns = items.getItem('WorxLandroidSRunsPerDay').state;
DurationMowerMinutes = DurationMowerMinutes + 1.7 * DurationMowerMinutes * (AmountofRuns - 1);
StartMowingTime = (time.toZDT(items.getItem('LocalSun_DaylightEndTime').state)).plusMinutes(DurationMowerMinutes * -1);
WeekdayShort = String(getDayOfTheWeek(time.toZDT(StartMowingTime))).slice(0, 3);
BaseWeekDayProp = 'WorxLandroidS_' + String(WeekdayShort);
if (WeekdayShort == 'SUN' || actions.Ephemeris.isBankHoliday(0)) {
  DurationMowerMinutes = 0;
} else {
  DurationMowerMinutes = Math.round((DurationMowerMinutes + 20));
}
for (var count = 0; count < 2; count++) {
  items.getItem((String(BaseWeekDayProp) + '_ScheduleDuration')).sendCommand(DurationMowerMinutes);
  thread.sleep(1000);
  items.getItem((String(BaseWeekDayProp) + '_ScheduleStartMinutes')).sendCommand((getMinute(StartMowingTime)));
  thread.sleep(1000);
  items.getItem((String(BaseWeekDayProp) + '_ScheduleStartHour')).sendCommand((getHour(null)));
  thread.sleep(1000);
}

Surrounding Blocky image:

Unfortunately the localization for the JS-Joda library cannot be included by default as it increases the size of the library way beyond acceptable levels. Without that getting the names of days of the week or months will not work. I don’t think we ever found a solution for this.

But you should be able to get at the number for the day of the week or the full name of the day of the week (maybe depending on your local language).

For me the following code:

console.info(time.toZDT().dayOfWeek());
console.info(time.toZDT().dayOfWeek().value());
console.info(time.toZDT().dayOfWeek().ordinal());

Produces:

2023-08-03 08:21:55.496 [INFO ] [nhab.automation.script.ui.scratchpad] - THURSDAY
2023-08-03 08:21:55.497 [INFO ] [nhab.automation.script.ui.scratchpad] - 4
2023-08-03 08:21:55.498 [INFO ] [nhab.automation.script.ui.scratchpad] - 3

so the following should work

function getDayOfTheWeek(myZDT) {
  retval = (time.toZDT(myZDT)).dayOfWeek().ordinal(); 
  return ["SUN", "MON", "TUE", "WED", "THR", "FRI", "SAT"][retval];
}

What’s surprising is that it’s not complaining that getDayOfWeek() doesn’t exist as it’s not listed as a function here at all. Also note that it’s .hour() not .getHour() and .minute() not .getMinute().

I don’t know what you are doing with these Items but there probably isn’t any reason to separate the hour and minute into separate Items any longer. There is now a Time is <item> rule trigger. If you set a DateTime Item to the date and time when you want the schedule to start, you can just trigger the rule based on that Item.

Also, why the for loop? That doesn’t make much sense to me at all.

Hi @rlkoshak ,

okay the “get” itself was added as i was running it in the Naashorn JS.
This is a bit difficult for me to understand but okay.

As I don’t want to run again into this issue I did this: (A bit rusty but codeless’)

The loop was added because of an issue in the Plugin that might not always update the property properly.

Thx have a good day
/Franz

It’s definitely different in Nashorn JS. On OH 3.4 Blockly does convert to Nashorn. In OH 4 it converts to JS Scripting and all the DateTime stuff is different. In Nashorn it’s still using the Java ZonedDateTime Objects where-as in JS Scripting it uses the JS-Joda library.

As part of the upgrade between OH 3 and OH 4 the release notes indicate you need to open and resave all your Blockly rules to have them converted.

However, inline script blocks and a few other situations cannot be handled automatically and require some manual changes.

Also note, by appearances, you are using a third party library to “getDayOfWeek” and such. If that library wasn’t updated for OH 4, it’s not going to be compatible. That might be the source of the problem.

How about this?

1 Like

Merci @stefan.hoehn!

Somehow my brain was not aware that “day of the week” is an attribute like “year” .
I used this inteterpretation with “+1” and start on monday because “get” of the list starts with 1 and Sunday is “0” afaik.

Have a good weekend!
/Franz

Hi @rlkoshak ,

the other facts with save Blocky and so on where clear to me.
But what is the reason why “get” is no longer required in front of Properties?
The usage of openhab.js which is wrapping the original Java objects?

THX
/Franz

Because in OH 3 Blockly “compiles” to Nashorn JavaScript. In Nashorn JavaScript uses the Java java.time.ZonedDateTime and related Classes.

In OH 4 Blockly “compiles” to JS Scripting. JS Scripting goes to great lengths to present pure JavaScript Classes and Objects in rules and avoid using Java. JS Scripting uses the js-joda library for ZonedDateTime and related classes.

It does not wrap the Java Classes and Objects. It’s a completely separate library and implementation. The add-on handles converting between js-joda and Java on the fly where needed.

1 Like