Since my rule using Promise has been working, I didn’t bother to change it to use gatekeeper. But over the past two years I have come to appreciate what gatekeeper can do, so I decided to use this rule to practice using it. When I initially changed the rule I ran into some errors, which I was able to solve by changing, “var gk = cache.put…” to “var gk = cache.private.put”, and by changing "if(trigger = “LivingroomSensor_MotionAlarm’)” to "if(trigger == “LivingroomSensor_MotionAlarm’)”
I then ran into some problems that were a bit trickier. The errors in the logs were:
2024-08-22 21:37:11.357 [WARN ] [tion.script.ui.LivingRoom_NightLight] - require Gatekeeper instead of gatekeeper and use Gatekeeper() instead of new gatekeeper.Gatekeeper().
2024-08-22 21:37:11.459 [ERROR] [ipt.javascript.LivingRoom_NightLight] - Failed to execute script: TypeError: Cannot read property "addCommand" from null
at <js>.:program(<eval>:71)
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 java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
... 21 more
2024-08-22 21:37:11.459 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'LivingRoom_NightLight' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "addCommand" from null
2024-08-22 22:44:03.929 [WARN ] [tion.script.ui.LivingRoom_NightLight] - require Gatekeeper instead of gatekeeper and use Gatekeeper() instead of new gatekeeper.Gatekeeper().
2024-08-22 22:44:03.929 [ERROR] [ipt.javascript.LivingRoom_NightLight] - Failed to execute script: TypeError: gk.addCommand is not a function
at <js>.:program(<eval>:71)
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 java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
... 21 more
2024-08-22 22:44:03.930 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'LivingRoom_NightLight' failed: org.graalvm.polyglot.PolyglotException: TypeError: gk.addCommand is not a function
So this morning I copied the gatekeeper code into a script, and worked on it. The script would just turn on the lights sequentially, then turn them off sequentially. Here’s the script:
var {gatekeeper} = require('openhab_rules_tools');
var gk = cache.private.put('test_gatekeeper', () => new gatekeeper.Gatekeeper());
gk.addCommand(600, () => { items.getItem('HuespotHallway2_Color').sendCommand('5,100,30'); });
gk.addCommand(600, () => { items.getItem('HuespotHallway1_Color').sendCommand('5,100,30'); });
gk.addCommand('PT1m', () => { items.getItem('HuespotDR4_Color').sendCommand('5,100,30'); });
gk.addCommand(600, () => { items.getItem("HuespotHallway2_Color").sendCommand("30,65,0"); });
gk.addCommand(600, () => { items.getItem("HuespotHallway1_Color").sendCommand("5,100,0"); });
gk.addCommand(0, () => { items.getItem("HuespotDR4_Color").sendCommand("5,100,0"); });
This script threw the following errors:
2024-08-23 09:39:20.911 [WARN ] [nhab.automation.script.ui.scratchpad] - require Gatekeeper instead of gatekeeper and use Gatekeeper() instead of new gatekeeper.Gatekeeper().
2024-08-23 09:39:21.003 [ERROR] [omation.script.javascript.scratchpad] - Failed to execute script: ReferenceError: "gk" is not defined
at <js>.:program(<eval>:3)
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 java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
... 75 more
2024-08-23 09:39:21.003 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'scratchpad' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "gk" is not defined
2024-08-23 09:39:51.174 [WARN ] [nhab.automation.script.ui.scratchpad] - require Gatekeeper instead of gatekeeper and use Gatekeeper() instead of new gatekeeper.Gatekeeper().
2024-08-23 09:39:51.238 [ERROR] [omation.script.javascript.scratchpad] - Failed to execute script: TypeError: Cannot read property "addCommand" from null
at <js>.:program(<eval>:4)
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 java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
... 75 more
2024-08-23 09:39:51.238 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'scratchpad' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "addCommand" from null
2024-08-23 09:40:34.370 [WARN ] [nhab.automation.script.ui.scratchpad] - require Gatekeeper instead of gatekeeper and use Gatekeeper() instead of new gatekeeper.Gatekeeper().
2024-08-23 09:40:34.470 [ERROR] [omation.script.javascript.scratchpad] - Failed to execute script: TypeError: Cannot read property "addCommand" from null
at <js>.:program(<eval>:4)
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 java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
... 75 more
2024-08-23 09:40:34.470 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'scratchpad' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "addCommand" from null
So, after looking at some other examples of gatekeeper usage, I modified the script to the below, and everything worked fine.
var {gatekeeper} = require('openhab_rules_tools');
var gk = new gatekeeper.Gatekeeper;
cache.private.put('lrNightLight_gatekeeper', time.ZonedDateTime.now())
gk.addCommand(600, () => { items.getItem('HuespotHallway2_Color').sendCommand('5,100,30'); });
gk.addCommand(600, () => { items.getItem('HuespotHallway1_Color').sendCommand('5,100,30'); });
gk.addCommand('PT2m', () => { items.getItem('HuespotDR4_Color').sendCommand('5,100,30'); });
gk.addCommand(600, () => { items.getItem("HuespotHallway2_Color").sendCommand("30,65,0"); });
gk.addCommand(600, () => { items.getItem("HuespotHallway1_Color").sendCommand("5,100,0"); });
gk.addCommand(0, () => { items.getItem("HuespotDR4_Color").sendCommand("5,100,0"); });
I have encorporated the above code into my rule, and will wait till tonight, when the conditionals are met, to see how it works.