i need help with rules in a loop aproach. I want to simplife rules code - below just a short example based on irrigation and many valves - right now i have duplicated code for every valve 1-4 and i want to achieve sth like this:
split code into 2 sections :
a) configuration
var int numberOfValves = 4
b) rules
for each … 4 do :
// watering timers
var Timer WtrValve1StartTimer = null
var Timer WtrValve1StopTimer = null
var Timer WtrValve2StartTimer = null
var Timer WtrValve2StopTimer = null
var Timer WtrValve3StartTimer = null
var Timer WtrValve3StopTimer = null
var Timer WtrValve4StartTimer = null
var Timer WtrValve4StopTimer = null
additional code which should be multiplicated for4 valves
There really isn’t enough here to provide concrete advice. It’s not even clear what language you are using and whether this is in a text file or in the UI.
ok i used jsr223 to auto create some rules in a while (10 PWM+PID controller rules).
Do you know if this is possible to see the rule in GUI now its treated as a rule created from text definition = don;t wannt to edit them, just to see te created config - now i see:
You can’t see the actual code of file-based rules on the UI. I am 100% file-based for rules and items, and don’t use the UI at all for rule-related stuff.
ok, is there a smart way to generate a test.items file from /jsr322 script? i need to create some items and rules within one “master file”. .js will be fine for that. I didn’t find the solution how to add items from js… i can do rules with:
scriptExtension.importPreset("RuleSupport");
scriptExtension.importPreset("RuleSimple");
+
var sRule = new SimpleRule()
To get more detail, click on that “See openhab-js : items (opens new window)for full API documentation” link where you’ll get a pretty good description on what each of these do.
ok thank you. tried to create some items from js but with no luck. Don’t know where to start:
/automation/PWM.js →
items.createItem("testowyitem","String","","","Testowy item label");
Error during evaluation of script 'file:/etc/openhab/automation/jsr223/PWM.js': TypeError: items.createItem is not a function in /etc/openhab/automation/jsr223/PWM.js at line number 22
cant find a reference on community how to use .createItem, i cant even use .getItem
item = items.getItem("testowyitem");
Error during evaluation of script 'file:/etc/openhab/automation/jsr223/PWM.js': TypeError: items.getItem is not a function in /etc/openhab/automation/jsr223/PWM.js at line number 23
i want to use jsr223 rules to manipulate openhab items. There is no items. namespace available by default. i installed Home - Documentation - how to use it in jsr223 ?
I was under the incorrect assumption that you were using the JS Scripting addon with ECMAScript 2021 instead of the very old and soon to be deprecated built in JavaScript with ECMAScript 5.1.
openhab-js only works in the new JavaScript add-on and it comes with that add-on by default. You don’t have to install anything extra.
I don’t know how to do it in the old one and would strongly suggest, if you do what to use JavaScript, to use the new add-on. Not only is it well supported, but it is much better documented and much easier to do just about everything in.
In the old JavaScript you’d have to use the raw Java ItemRegistry and import a lot of extra classes instead of it being just that one liner function call.
var sRule = new SimpleRule() {
execute: function(module, input) {
print("input:" + input); // input contains all the output values
}
};
Loading script ‘/etc/openhab/automation/js/ItemCreator.js’
21:57:38.563 [ERROR] [ab.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: “SimpleRule” is not defined
So i need to rewrite my rule to this format. My old rule was created using SimpleRule builder - i red the docs and not sure how to re-write such PWM rule using rules.JSRule.
also no luck with using .createItem - i cant even find one example of usage in whole documentation and community
const item = items.createItem("test", "Switch","","", "label test");
console.log("nowy item", items.getItem("test"));
Error during evaluation of script 'file:/etc/openhab/automation/js/ItemCreator.js': org.openhab.core.items.ItemNotFoundException: Item 'test' could not be found in the item registry
That might be a timing thing - I wouldn’t expect the new Item to be available within milliseconds. Have you checked via UI whether the Item got created?
I don’t know where RadiatorACn comes from and what that trigger configuration does is inscrutable to me. If it’s supposed to be an Item received update replace the ??? above with [triggers.ItemStateUpdateEvent('my_item')]. It’s an array so if you’ve more than one separate them by commas.