Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ThaDaVos opened this issue Jul 21, 2024 · 12 comments
Labels
enhancement New feature or request

Comments

@ThaDaVos
Copy link

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

  • Version used: (e.g., openHAB and JS Scripting add-on version) 4.3.0 4.3.0.SNAPSHOT
  • Environment name and version (e.g. Chrome 76, Java 8, Node.js 12.9, ...): Docker Container
  • Operating System and version (desktop or mobile, Windows 10, Raspbian Buster, ...): Docker Container
@ThaDaVos ThaDaVos added the enhancement New feature or request label Jul 21, 2024
@florian-h05
Copy link
Contributor

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.

@florian-h05 florian-h05 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2024
@ThaDaVos
Copy link
Author

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
Copy link
Contributor

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 florian-h05 reopened this Jul 24, 2024
@florian-h05
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

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

@florian-h05
Copy link
Contributor

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

@ThaDaVos
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

ThaDaVos commented Jul 24, 2024

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
Copy link
Author

ThaDaVos commented Jul 24, 2024

So something as follows is needed to pass it through I think:
ThaDaVos/openhab-core@027958b

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

@florian-h05
Copy link
Contributor

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

Rather fix this line in rules.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants