[OH3] ECMA Script reports "Failed to execute rule xx': Fail to execute action: 2"

I tried to migrate a DSL rule to ECMA.
The action code looks like this:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
var timerAlarm = null;
var AlarmTime =itemRegistry.getItem('NaechsterAlarm').getState();
var WarmWasserSollTemperature = itemRegistry.getItem('WarmWasserSollTemperature').getState();
    if (AlarmTime == 0) {
        if (timerAlarm !== null) {
            timerAlarm.cancel;
            timerAlarm = null;
        }
        logger.info("All alarms are cancelled");
    } else {
        var epoch = AlarmTime - 300000;
        logger.info("Scheduling Alarm for " + epoch);
        if (timerAlarm !== null) {
            logger.info("Reschedule Alarm");
            timerAlarm.reschedule(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));
        } else {
            logger.info("New Alarm");
            timerAlarm = ScriptExecution.createTimer(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()), function(){
                    if (itemRegistry.getItem('ActiveProgram').getState()=="reduced") {
                        var result =Exec.executeCommandLine(Duration.ofSeconds(10), "php", "/etc/openhab2/scripts/SetOneTimeCharge.php");
                        logger.info("result: {}", result);
                    } else {
                        logger.info("Warmwasser wird schon erzeugt!");
                    } 
                logger.info("Alarm is expired");
                timerAlarm = null;
            });
        }
    }

I had it working under OH3 as a DSL rule, including the php-script!
All the log shows is the Failed to execute rule xx': Fail to execute action: 2 (2 is the above code)
I am out of ideas on this one.

Wrap the entire thing in a try/except and log the error if one occurs. This is basically how the Helper Libraries print Python stack traces for rules in Jython. I can provide a specific example when I’m at a computer if needed.

Thanks for the hint, however the log doesn’t show more info with a try ..catch.

I got the following error trying to execute your (modified) script:

2021-01-18 15:03:47.079 [INFO ] [org.openhab.rule.2d2009c5ed         ] - Scheduling Alarm for -300000
2021-01-18 15:03:47.084 [INFO ] [org.openhab.rule.2d2009c5ed         ] - New Alarm
2021-01-18 15:03:47.088 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '2d2009c5ed' failed: ReferenceError: "Instant" is not defined in <eval> at line number 21

Instant is not defined :slight_smile:

Try to add the following:

var ZonedDateTime = Java.type("java.time.ZonedDateTime");
...
timerAlarm = ScriptExecution.createTimer(ZonedDateTime.ofInstant(epoch, ZoneOffset.UTC), function(){

Found my problem! A Typo in an itemname, that way I was trying to get the state of a non-existing item. Why doesn’t it show a more descriptive error!

Thanks for the help, your last will help overcoming the next observed problem!

Initially I got an ItemNotFoundException: (obviously :stuck_out_tongue: )

2021-01-18 15:03:13.727 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: script
java.lang.RuntimeException: org.openhab.core.items.ItemNotFoundException: Item 'NaechsterAlarm' could not be found in the item registry

Thanks for looking, If I would have gotten this Error description…
Now I’m struggeling with the timer. Working on it…

hm, sorry, maybe that sounded a bit harsh… I meant obviously as in I don’t have an item called “NaechsterAlarm” :slight_smile:

1 Like

No worries, it never sounded harsh to me!!

You would have to log the error yourself from the catch block.

Here is the outline of what I mean, fill in your real logger:

var logger = ...
try {

} catch (e) {
  logger.error(e)
} 

Thanks for the reminder, however already done :wink:
…the logger did not show the messages tsmit got.

I didn’t thinking you’d misunderstood since you hadn’t asked how to do it, but it’s such a simple construct I can’t see why it wouldn’t print the error unless the log statement wasn’t there.

I’ll try to remember to post my stack trace builder when I get home.