openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[Feature Request] Add the option to add/create rule based on Template to JSRule or Rule Builder #363

Open ThaDaVos opened 1 month ago

ThaDaVos commented 1 month ago

It would be great if one could instantiate a rule template from a script in the automations/js folder - this way one can create the necessary items/groups etc and instantiate the rule template from one place - so when the script is removed, all gets cleaned up. (At least this is my understanding how automation/js is used.

Your Environment

florian-h05 commented 1 month ago

JSRule and Rule Builder work different to UI-based rules wrt to their structure and rule templates are a UI-only feature.

It is possible to create something like JSRule Templates, e.g. by writing a function that creates the rule (I do this really much, e.g. https://github.com/florian-h05/openhab-js-tools/blob/f5e48a2ec0325df920c4ba15dfd1a3d77973220b/src/thingsx/health.js#L129, but instantiating UI-rule templates from script languages is not possible I think and would be high effort to get it working.

I think this feature request is a "Won't fix" and I will therefore close this, sorry.

ThaDaVos commented 1 month ago

It's sad that this is marked as Won't fix as it would would mean one has to copy the templates manually from marketplace to include them when all other rules are file-based - I rather have my rules file-based so I can version control them

florian-h05 commented 1 month ago

I think it is easier to rewrite this template to a rule generator function in that case — as long as one is proficient enough with writing code. I can re-open this feature request, but I personally won‘t work on it and as long as no one else contributes this, I guess the feature request will not be implemented.

florian-h05 commented 1 month ago

To implement this, one would need to check how rule templates are handled in detail, if it is UI-only or if openHAB core does the work of applying the template (i.e. add triggers, actions, conditions of the template to the rule). If is done in core, it should be easier to do that from scripts.

ThaDaVos commented 1 month ago

According to below file it's added to the persistent storage: MarketplaceRuleTemplateProvider.java

And it seems it's also stored inside the Rule itself: Rule.java#L67

And there's also a constructor for it: RuleImpl.java#L90

So with a quick search it seems it's all done inside the openhab-core luckily - I haven't checked how it's wrapped from openhab-js side yet, will do that when I have time

ThaDaVos commented 1 month ago

Could it be as simple as allowing it to be passed here? rules.js#L258

florian-h05 commented 1 month ago

Probably yes - we need to check if it is working and if lifecycle is properly handled. But this should be an easy test.

ThaDaVos commented 1 month ago

Yeah I was thinking about manually editing the source code by manually loading it inside Openhab (through the manual install option) and seeing what it does - probably won't be today - think weekend on it's earliest

florian-h05 commented 1 month ago

I don't think it will be as easy as we hoped - file-based/unmanaged rules created through scripts are managed by the ScriptedAutomationManager, which handles the rules different from UI-based rules.

ThaDaVos commented 1 month ago

Could be easier than you think - seeing the following line: RuleBuilder.java#L60 The template id is passed through - also in the unmanaged rule - at least, if the method withTemplateUID was called 🤣

ThaDaVos commented 1 month ago

So something as follows is needed to pass it through I think: https://github.com/ThaDaVos/openhab-core/commit/027958b397266e1b73d0d8ac6575ae616394734e

I added the check if ruleUid != templateUid because of rules.js#L258

florian-h05 commented 1 month ago

I added the check if ruleUid != templateUid because of rules.js#L258

Rather fix this line in rules.js.