Loop through rules

Is it possible to get a list of all UI-based rules in JS scripting like

rules.getRules()
// in analogy to items.getItems()

There isn’t a function in openhab-js that will let you do that. But all of the registries in OH have a getAll method so this is possible. We’ll just have to break out of JS and use the raw Java APIs.

rules.ruleRegistry.getAll()

Note, you’re going to get a Java Collection of Java Rule Objects. Pay attention to they types. These do not behave the same as Arrays in JavaScript. untils.javaListToJsArray() will convert it from a Java Collection to a JavaScript array, but the Rule Objects are still going to be Java.

Note, if you want to do stuff like enable/disable/run any of these Rules, you’ll probably need to use rules.RuleManager.

Note: I’ve not actually tried these things myself. These symbols are not actually exported by the library so they may not be available in your rule. You may have to look at the code for how to import and use these yourself in your rule.

1 Like