Skip to content

Conversation

@dilyanpalauzov
Copy link
Contributor

I add example only for Groovy, as I do know how to use it in the other automation add-ons. Besides, the other rule engines provide other ways for the same functionality: script_unloaded in JRuby, and for others I do not know if the functionality is from openhab-core, or from the add-on.

@netlify
Copy link

netlify bot commented Oct 2, 2025

Thanks for your pull request to the openHAB documentation! The result can be previewed at the URL below (this comment and the preview will be updated if you add more commits).

Built without sensitive environment variables

Name Link
🔨 Latest commit 1c96144
🔍 Latest deploy log https://app.netlify.com/projects/openhab-docs-preview/deploys/68de632918037a0007e68fb8
😎 Deploy Preview https://deploy-preview-2567--openhab-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dilyanpalauzov dilyanpalauzov marked this pull request as ready for review October 2, 2025 11:39
@netlify
Copy link

netlify bot commented Oct 2, 2025

Thanks for your pull request to the openHAB documentation! The result can be previewed at the URL below (this comment and the preview will be updated if you add more commits).

Built without sensitive environment variables

Name Link
🔨 Latest commit 746786d
🔍 Latest deploy log https://app.netlify.com/projects/openhab-docs-preview/deploys/68de63e6806756000875d2f8
😎 Deploy Preview https://deploy-preview-2567--openhab-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Oct 2, 2025

Thanks for your pull request to the openHAB documentation! The result can be previewed at the URL below (this comment and the preview will be updated if you add more commits).

Name Link
🔨 Latest commit c27dbdd
🔍 Latest deploy log https://app.netlify.com/projects/openhab-docs-preview/deploys/695aa2f34151f600081de2a5
😎 Deploy Preview https://deploy-preview-2567--openhab-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dilyanpalauzov dilyanpalauzov force-pushed the lifecycle_preset branch 4 times, most recently from a09886c to b1f7fa7 Compare October 3, 2025 11:34
@dilyanpalauzov
Copy link
Contributor Author

Now includes Nashorn example.

@dilyanpalauzov
Copy link
Contributor Author

The current proposed change is correct, but only part of the truth. My understanding is that if a class implements ScriptExtensionProvider and returns in getDefaultPresets() non-empty set, then it is loaded by default by org.openhab.core.automation.module.script.internal.ScriptExtensionManager:

    public List<String> getDefaultPresets() {
        List<String> defaultPresets = new ArrayList<>();
        for (ScriptExtensionProvider provider : scriptExtensionProviders) {
            defaultPresets.addAll(provider.getDefaultPresets());
        }
        return defaultPresets;
    }

That is, the MediaScriptScopeProvider, LifecycleScriptExtensionProvider and DefaultScriptScopeProvider script extensions, or in other words the default, media and lifecycle presets, do not require importPresent.

@dilyanpalauzov dilyanpalauzov force-pushed the lifecycle_preset branch 2 times, most recently from 5a27a98 to a13b13b Compare October 4, 2025 11:48
@dilyanpalauzov
Copy link
Contributor Author

Besides, one of the presets, which is loaded without importPreset is called default, not “Defaut”: In https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/DefaultScriptScopeProvider.java: private static final String PRESET_DEFAULT = "default";

@dilyanpalauzov dilyanpalauzov force-pushed the lifecycle_preset branch 2 times, most recently from 7d1cde9 to 1923155 Compare October 4, 2025 13:08
@dilyanpalauzov dilyanpalauzov changed the title JSR223 mention lifecycle preset JSR223 mention lifecycle and media presets Oct 4, 2025
@dilyanpalauzov
Copy link
Contributor Author

I added also the media preset - variables org.openhab.core.audio.AudioManager audio and org.openhab.core.voice.VoiceManager video.

Copy link
Contributor

@stefan-hoehn stefan-hoehn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@florian-h05 please check the content.


#### `lifecycle` Preset (`importPreset` not required)

It provides a mechanism to execute code, when the input file is deleted. Modifying a file deletes it and creates it again.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2nd sentence has be in the next line. (make sure the first sentence doesn't have a trailing space then.

@dilyanpalauzov dilyanpalauzov changed the title JSR223 mention lifecycle and media presets JSR223 mention lifecycle and media presets, scriptLoaded and scriptUnloaded functions Oct 11, 2025
@dilyanpalauzov
Copy link
Contributor Author

I have described also the scriptLoaded(String) and scriptUnloaded() functions, and included examples in Groovy, JavaScript and Python.

@dilyanpalauzov dilyanpalauzov force-pushed the lifecycle_preset branch 6 times, most recently from bf4b509 to a9b1964 Compare October 11, 2025 19:50
@dilyanpalauzov dilyanpalauzov force-pushed the lifecycle_preset branch 4 times, most recently from f27cdec to a5603b2 Compare October 13, 2025 11:41
@dilyanpalauzov dilyanpalauzov changed the title JSR223 mention lifecycle and media presets, scriptLoaded and scriptUnloaded functions JSR223 mention lifecycle, media and provider presets, scriptLoaded and scriptUnloaded functions Oct 13, 2025
Signed-off-by: Florian Hotze <[email protected]>
Copy link
Contributor

@florian-h05 florian-h05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I've reworded a few things during review.

@florian-h05 florian-h05 changed the title JSR223 mention lifecycle, media and provider presets, scriptLoaded and scriptUnloaded functions JSR223: Add lifecycle, media, provider presets & scriptLoaded, scriptUnloaded functions Jan 4, 2026
Signed-off-by: Florian Hotze <[email protected]>
Signed-off-by: Florian Hotze <[email protected]>
@florian-h05 florian-h05 merged commit 571c984 into openhab:main Jan 4, 2026
4 of 5 checks passed
@florian-h05 florian-h05 added this to the 5.2 milestone Jan 4, 2026
All instances from this preset have an `addPermanent(...)` method, in addition to `add(...)`.
With the `add(...)` method, elements added over the above instances are removed, when the script, which inserted the elements, is deleted or changed.
When `addPermanent(...)` is used, the inserted elements `...` are not removed.
`itemRegistry.addPermanent(...)`/`metadataRegistry.addPermanent(...)`/`thingRegistry.addPermanent(...)` from this preset offer the same functionality as `itemRegistry.add(...)`/`metadataRegistry.add(...)`/`things.add(...)` from the `default` preset.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a script file is loaded, the `scriptLoaded(String)` function is invoked if it is defined.
The path of the script file will be passed as parameter.
Similarly, when a script file is unloaded, the `scriptUnloaded()` function is invoked if it is defined.
Note that `scriptLoaded(String)` and `scriptUnloaded()` are only executed for script files - not Script Actions and Script conditions of rules.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original text suggests that for transformations (file in etc/openhab/transform) scriptUnloaded() is executed, but scriptLoaded(String) is not executed. After the changes this information is lost.

Similarly, when a script file is unloaded, the `scriptUnloaded()` function is invoked if it is defined.
Note that `scriptLoaded(String)` and `scriptUnloaded()` are only executed for script files - not Script Actions and Script conditions of rules.

[`lifecycleTracker`](#lifecycle-preset-importpreset-not-required)'s `addDisposeHook(Disposable)` and the `scriptUnloaded()` function serve similar purposes, but it is recommended to use `addDisposeHook(Disposable)` as it's functionality is not limited to script files only.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

”it`s" → “its”.

Why was this removed:

When a script is executed several times, on each execution it adds a Disposable, and then the script is changed, its scriptUnloaded() is executed once and each added Disposable is also dispose()d.

This is not what I meant:

The lifecyleTracker also supports adding multiple dispose hooks, contrary to scriptUnloaded() that can only be defined once.

I meant that defining once scriptUnloaded() and one Disposable in a script, then executing the script several times, then modifying the script, will execute scriptUnloaded once, and the Disposable many times. (as many times as the script was executed, provided that each execution created unconditinonally a new Disposable - this can be tested in transformations)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should a script be executed several times? A transformation can be, but a script not. Since scriptUnloaded is not available in transformations that information provides no benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote that tests I believe I created transformations with scriptUnloaded(), scriptLoaded(String) and lifecycleTracker. scriptLoaded(String) was not executed, scriptUnloaded() and the Disposable were executed. I mean transformations with a file under etc/openhab/transform/. So an openHAB-Groovy-filebased-Transformation is a Groovy-Script and it can be executed several times.

`itemRegistry.addPermanent()`/`thingRegistry.addPermanent()` from this preset offer the same functionality as `itemRegistry.add()`/`things.add()` from the `default` preset.
All instances from this preset have an `addPermanent(...)` method, in addition to `add(...)`.
With the `add(...)` method, elements added over the above instances are removed, when the script, which inserted the elements, is deleted or changed.
When `addPermanent(...)` is used, the inserted elements `...` are not removed.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second ellipsis I used on that line was supposed to “mark a pause in speech” - as allowed by https://en.wikipedia.org/wiki/Ellipsis , not to cite the parameters introduced by the first ellipsis.

@dilyanpalauzov dilyanpalauzov deleted the lifecycle_preset branch January 4, 2026 18:16
::: tab Python

When the setting Use scope and import wrapper is on, the `scope` module is enabled:
When the setting "Use scope and import wrapper" is enabled, the `scope` module is enabled:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats “enabled” twice in the same sentence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants