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.
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
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 )
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
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.