Failing to execute script in OH4.0.3

  • Platform information:
    • Hardware: _Intel I3, 16 Gb
    • OS: _Linux mint Victoria 21.2 Mate 64 bit all updates installed
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 4.0.3
  • Issue of the topic: failed to execute script
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue:
      The blockly rule has to be executed form this rule.
configuration: {}
triggers:
  - id: "1"
    configuration:
      event: END
      channelUID: astro:sun:a4ef62ea88:daylight#event
    type: core.ChannelEventTrigger
conditions:
  - inputs: {}
    id: "3"
    configuration:
      itemName: LocalWeatherandForecast_Cloudiness
      state: 80%
      operator: ">="
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "2"
    configuration:
      considerConditions: true
      ruleUIDs:
        - 4bc082e8b3
    type: core.RunRuleAction


Rulescode cereated in Blockly

var Day;

function deibich_blockly_get_zdt_component_from_returned_value(value) {
  if(typeof value == 'number')
  {
    return value;
  }
  return value.getValue()
}

var deibich_blockly_ZonedDateTime = Java.type('java.time.ZonedDateTime');

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


Day = deibich_blockly_get_zdt_component_from_returned_value(deibich_blockly_ZonedDateTime.now().getDayOfWeek());
if (Day == 1) {
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(30000);
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(40000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  console.info('Het is dagnr 1, maandag, alle lampen aan');
} else if (Day == 2) {
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(30000);
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(35000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  thread.sleep(10000);
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  console.info('Het is dagnr 2, dinsdag, alle lampen aan');
} else if (Day == 3) {
  thread.sleep(10000);
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(35000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  console.info('Het is dagnr 3, woensdag, alle lampen aan');
} else if (Day == 4) {
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(35000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  console.info('Het is dagnr 4, donderdag, alle lampen aan');
} else if (Day == 5) {
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(35000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(50000);
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  console.info('Het is dagnr 5, vrijdag, alle lampen aan');
} else if (Day == 6) {
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(30000);
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(40000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  console.info('Het is dagnr 6, zaterdag, alle lampen aan');
} else if (Day == 7) {
  items.getItem('Staandelamprechtsvoor_Status').sendCommand('ON');
  thread.sleep(20000);
  items.getItem('Woonkamerhanglamp_Status').sendCommand('ON');
  thread.sleep(25000);
  items.getItem('Woonkamerdressoir_Status').sendCommand('ON');
  thread.sleep(45000);
  items.getItem('Staandelamplinksvoor_Status').sendCommand('ON');
  thread.sleep(35000);
  items.getItem('Eetkamerstaandelamp_Status').sendCommand('ON');
  thread.sleep(50000);
  items.getItem('Halfotokast_Status').sendCommand('ON');
  console.info('Het is dagnr 7, zondag, alle lampen aan');
} else {
  console.warn('Dagnr =??, dagnr is onjuist!!!');
}
console.info('I did it!!!!!');

  • Services configuration related to the issue
  • If logs where generated please post these here using code fences:
2023-09-21 19:09:11.107 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: "events" is not defined
	at <js>.:program(<eval>:61) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1039) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl$1.runNow(RuleEngineImpl.java:248) ~[?:?]
	at org.openhab.core.automation.internal.module.handler.RunRuleActionHandler.execute(RunRuleActionHandler.java:106) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:997) ~[?:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
2023-09-21 19:09:11.119 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '4bc082e8b3' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "events" is not defined

Since a couple of days I’ve a strange issue. After updating from OH 3.4.4 to OH 4.03 I get an error when a rule should be executed. I’m a beginner, but as far as I can see is has something to with java and event. But what is the problem now? This rule has worked for about 3 years.

Any help would be welcome.

This almost certainly isn’t the rule generating that error. events doesn’t occur in it anywhere.

Assuming all the rest of your rules are also Blockly, I’d guess there’s one script action or script condition that you’ve not opened and saved to regenerate the code.

events was the way in Nashorn JS that one sends commands or posts updates to Items. In JS Scripting there is a sendCommand and postUpdate on the Item.

The developer sidebar will search inside Rules but I don’t know if it searches inside Blockly rules the same way. Press alt-shift-d and search for events and see if anything comes up. If not you’ll have to search through $OH_USERDATA/jsondb/autmation_rules.json to find the rule where this still exists.

Note, given what this rule does, Timers would be preferable to sleeps. A rule that takes seconds to run, let alone minutes,can become a problem in some circumstances.

1 Like

HI Rich, Thanks for the information. But what could have happened, that the rule isn’t running any more. I’ve nothing changed in the past months in these rules or events. Has something changed in OH 4.0.3, that I’m not aware of. For me is that the strange part of this story.
Shift-Alt-d returns 3 Blockly rules including the rule ```
4bc082e8b3


BTW. You are right for what you say about the sleeps. I've to change that part during the coming months.

As discussed in the release notes and the updated docs for Blockly, you must open each script that you have written in Blockly and save it to regnerate the code based on the blocks for JS Scripting instead of Nashorn JS.

Rich,
Thanks for the information provided. After having opened and saved each script, my lights were activated by the rules this evening. Still have to remove the sleep codes in the rule.

Yes you should definitely do that. As a general rule the use of sleep in rules can cause the OH core to block and/or execute very slowly. If you want to delay something, the use timers instead.