-
-
Notifications
You must be signed in to change notification settings - Fork 431
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
[automation] Provide equivalent of scriptUnloaded for UI generated rules #2173
Comments
This issue has been mentioned on openHAB Community. There might be relevant details there: |
FYI the way I achieve this is that I have a library (Java script extension) which allows a script to register code to run at specific times in it's lifecycle. I used to use multiple points in time, but now I only use 'unloaded'. Personally I believe that this method is superior to the 'special function' approach because:
The actual boilerplate code that needs to be in place in either solution is similar. |
I'm not tied to any specific solution or approach. I like the idea of an approach that works for all languages (including Rules DSL). The main need is to be able to clean up when a rule gets unloaded. Though that Java script extension would need to be added to and built into the core, right? So using it would be out of reach for most users right now. |
I think we're agreed; my approach handles what you need plus is more flexible than the And yes, I'm not suggesting that users should install a new script extension, I agree this should go into core. I just wanted to link to something to demonstrate how it could work in case anyone wants to code it (or any individual really needs it now and can install it). (Plus BTW I actually did submit a PR a while back related to this: #1635 although my code included some bits to handle OSGi too, which was maybe a little too far.) |
And for UI DSL rules, there's another issue that may be related. |
@rkoshak With regard to #2173 (comment), can this be closed? It looks like this can be used to achieve what you want. |
Yes, I think this can be closed now, though it's not clear that's available in Rules DSL. It was my intent to include that as well as the other languages. |
This is a feature request.
When writing rules in Python and JavaScript (and probably Groovy as well) one can define a special function called
scriptUnloaded
(there is also ascriptLoaded
which should be covered by this also. This function gets called when, as the name implies, the script file is unloaded.This function is particularly useful for Timer management and working with rules that create other rules (an advanced but useful use case that is helpful for library developers), dynamically created Items, etc. In this function one can cancel, delete, and otherwise clean up all the stuff the rule created that exists outside the script itself. The most useful thing to do here would be to cancel any running timers created by the script.
This capability is missing from UI generated rules. See https://community.openhab.org/t/oh-3-examples-writing-and-using-javascript-libraries-in-mainui-created-rules/108526/33 for some discussion on this. So any Timers or other stuff created by a Script Action that exists outside of the Script Action gets orphaned and continues to exist. In the best case this will result in errors in the logs as orphaned Timers go off. In the worst case it can leave a looping Timer running forever until OH is restarted and a bunch of artifacts (Items, Rules, etc) that were dynamically created that need to be manually cleaned up afterwards.
To take one extreme case, there is a Community Library in the Helper Libraries that would consolidate the hourly forecast Items into a daily forecast. This script would create dozens of Items to accomplish this. Now that the daily forecast is supported by the API, if that capability were created in the UI it would require manually deletion of those dozens of no longer needed Items, and the links which could be quite tedious and result in problems like orphaned links which can cause problems themselves.
The text was updated successfully, but these errors were encountered: