[SOLVED] JSR223 Javascript Rule From DSL

Hi

I’m trying to convert a very simple rule from the Rules DSL to JSR223 Javascript and can’t for the life of me work out why it doesn’t execute properly…

Here’s the Rules DSL rule which works fine…

rule "Packed Lunch Reminder"
    when
        Time cron "0 0 22 * * ? *"
    then
        var Number DayOfWeek = now.getDayOfWeek
        if ((DayOfWeek == 2) || (DayOfWeek == 7)) {
            ECHOFRONTROOM_TTS.sendCommand("blah blah blah")
        }
    end

And the rule in JSR223 Javascript which fails… The rule triggers fine but the if statement never evaluates to true and therefore the command is never sent.

load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/jsr223/jslib/JSRule.js');

JSRule({
    name: "Packed Lunch Reminder",
    description: "Packed Lunch Reminder",
    triggers: [
         TimerTrigger("0 0 22 * * ? *")
    ],
    execute: function (module, input){
        // Get day of week
        var DayOfWeek = new Date().getDay;
        // Get required items as JSON object
        var cTTS = getItem("ECHOFRONTROOM_TTS");
        logInfo("Packed Lunch Reminder Running");
        if (DayOfWeek == 1 || DayOfWeek == 7) {
            curr = "blah blah blah";
            sendCommand(cTTS, curr);
        }
    }
});

I’m sure I’m missing something really really obvious, just can’t see it…

Also, is it possible to output the value of a variable in a log message?

I am not sure how strict the js I’d handled, so you could try using brackets for getDay.
getDay()

And yes you can log variables with logInfo.
If the rule throws an error after adding a log with a variable, try putting it in a String function.

logInfo("Value= " + String(varName));

Indeed, it should be getDay(). All these are methods, not properties, of a Date object.
Next to that, keep in mind that Javascript uses a 0 based system. So Sunday is 0, Monday is 1, and so on, meaning DayOfWeek will never be 7.

1 Like