For the most part you should pretty much only be meeting with JavaScript. OH is written in Java so there are some rare cases where a Java Object might pop up (this is usually called out in the docs). But great possible have been taken to minimize that to a minimum.
When in doubt, the first place to look should be JavaScript Scripting - Automation | openHAB. These reference docs are pretty thorough and complete.
If you are looking at an old example, it might be Nashorn JS but that version of JS is 5.1. It’s ancient, not supported and does very little to help you along.
If that’s the error, it’s not from the get all, it’s from the set enabled.
And indeed, if you’ve continued to override rules
from openhab-js which presents a pure JS interface to the RuleRegistry with your own variable it’s going to fail with that error.
This is why it’s important not to just import random stuff from @runtime and assign them to variables. items
, rules
, things
and variable names like that are already taken by openhab-js and if you overwrite then you bow away your ability to use openhab-js to interact with OH. you are on your own.
Unfortunately I’m this case openhab-js doesn’t provide a way to get all the rules directly, so we use osgi
, another part of openhab-js, to pull the raw RuleManager service. But I made a mistake. We need the RuleRegistry. which is different. RuleManager doesn’t have a getAll() method.
So I need to adjust the code somewhat:
var { ruleRegistry } = require('@runtime/RuleSupport');
// ---------------- Logger settings
console.loggerName = "org.openhab.rule." + ctx.ruleUID;
// ---------------- Script Configuration Settings
// Rules that should never be reinitialised
var exclude_from_reinit = ["rule_reinit_enabled_rules", "delay_start_rules"];
// -------- Main -------
var allRules = utils.javaSetToJsArray(ruleRegistry.getAll());
allRules.map( r => r.getUID())
.filter( r => !exclude_from_reinit.includes(r) )
.filter( r => rules.isEnabled(r) )
.forEach( r => {
rules.setEnabled(r, false);
Java.type("java.lang.Thread").sleep(200);
rules.setEnabled(r, true);
});
We don’t need the manager, we can access the parts of the manager we need through openhba-js’s rules
Object which is available be default.
Note, I’m not hurtling across South Park at night at 60 MPH in the snow (in the passenger seat of course) so was able to actually test the code this time).
See Design Pattern: Working with Groups in Rules for all sorts of ways to loop through, reduce, find and filter Arrays/Lists, etc in most of the languages OH supports.
Also pay attention to the way the array is created above. You can just create the array, you don’t need to define the array as a String and then use a bunch of replace and split operations to convert the String to an actual array.