- Platform information:
- OS: OpenHABian 3.4.2 release
- Java Runtime Environment:Zulu11.62+17-CA (build 11.0.18+10-LTS)
I’m trying to replace my existing UI-based javascript (ECMAScript-2021) rules into file-based rules, in order to make the project easier to maintain and update. I created a sample rule file called tst.js, and uploaded it to /etc/openhab/automation/js, as per the documentation for file-based javascript rules (JavaScript Scripting - Automation | openHAB).
Sample file:
rules.JSRule({
id: "r_TEST_1",
name: "FILE TEST 1",
description: "This is a test.",
triggers: [triggers.ItemStateUpdateTrigger('tst_item')],
execute: (event) => {
var tst = "hello world";
}
});
The rule does not show up in the list of rules on the web GUI, and when I check the openhab.log file from /var/log/openhab, I see the following error saying that “rules is not defined”:
[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/js/tst.js': org.graalvm.polyglot.PolyglotException: ReferenceError: "rules" is not defined
I looked at the documentation linked above, and do not see anything about needing to include any additional include directives or java imports. I have the javascript add-on installed, and all my javascript ECMAScript rules made in the web GUI are working.
I also tried putting the file in /etc/openhab/automation/jsr223 (I’m not clear on the difference between js and jsr223). If I do that, I get the following error saying that it expects a comma instead of the function injection:
[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/tst.js': /etc/openhab/automation/jsr223/tst.js:7:19 Expected comma but found =>
execute: (event) => {
^ in /etc/openhab/automation/jsr223/tst.js at line number 7 at column number 19
2023-10-23 14:39:28.346 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/tst.js': /etc/openhab/automation/jsr223/tst.js:7:19 Expected comma but found =>
What am I missing here? Does this file belong in automation/js or in automation/jsr223? Thanks in advance for any help.
Oh, and a tangential question: If I wanted to include multiple trigger conditions, how would I delimit them? Are they separated by commas, or by an “or” keyword, or something else? I’m guessing from the rule builder documentation that I would use “or” to delimit multiple triggers, but I’m not sure. Thanks again.