Skip to content

Comments

[blockly] change event attributes, add return block#3376

Merged
florian-h05 merged 2 commits intoopenhab:mainfrom
stefan-hoehn:blockly_migrate_event_attributes
Oct 28, 2025
Merged

[blockly] change event attributes, add return block#3376
florian-h05 merged 2 commits intoopenhab:mainfrom
stefan-hoehn:blockly_migrate_event_attributes

Conversation

@stefan-hoehn
Copy link
Contributor

@stefan-hoehn stefan-hoehn commented Oct 8, 2025

Depends on: openhab/openhab-addons#19443

The event object/attributes in script actions and conditions is now the same as in JSRule and Rule Builder.
Backwards compatibility will be retained for some time, though it is advised to adjust, which is done in this PR according to:
https://github.com/openhab/openhab-js/blob/51f164dc9f11a29e6223e24268848aa8f0be168f/src/rules/rules.js#L471

Additionally a new return block was added to the "Run & Process" category with return parameter.

image image
  1. The block without a return parameter is a nice improvement to easily leave a script without using non-elegant if/else statements. Bei adding the return statement the script is immediately ended. In this case the return parameter is not important.
  2. By adding a return parameter (in particular true or false) this can be used for condition, e.g. by using this condition:
image

which can implemented by using an inline blockly script that evaluates to true or false.

@relativeci
Copy link

relativeci bot commented Oct 8, 2025

#3495 Bundle Size — 11.38MiB (~+0.01%).

5585292(current) vs f0117c8 main#3490(baseline)

Warning

Bundle contains 2 duplicate packages – View duplicate packages

Bundle metrics  Change 1 change
                 Current
#3495
     Baseline
#3490
No change  Initial JS 2.01MiB 2.01MiB
No change  Initial CSS 577.56KiB 577.56KiB
Change  Cache Invalidation 18.48% 17.7%
No change  Chunks 254 254
No change  Assets 277 277
No change  Modules 3036 3036
No change  Duplicate Modules 163 163
No change  Duplicate Code 1.85% 1.85%
No change  Packages 99 99
No change  Duplicate Packages 2 2
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#3495
     Baseline
#3490
Regression  JS 9.59MiB (+0.01%) 9.59MiB
No change  CSS 875.33KiB 875.33KiB
No change  Fonts 526.1KiB 526.1KiB
No change  Media 295.6KiB 295.6KiB
No change  IMG 140.74KiB 140.74KiB
No change  HTML 1.39KiB 1.39KiB
No change  Other 871B 871B

Bundle analysis reportBranch stefan-hoehn:blockly_migrate_eve...Project dashboard


Generated by RelativeCIDocumentationReport issue

@rkoshak
Copy link

rkoshak commented Oct 8, 2025

When you add the docs, make sure to make it clear that return can only be used when the wrapper is enabled in the JS Scripting configs (MainUI -> Settings -> JS Scripting.

@florian-h05
Copy link
Contributor

Our idea was to make blockly automatically set the directive to enable the wrapper when return is used.

@stefan-hoehn
Copy link
Contributor Author

you can actually see this in line 397 that creates "use wrapper" in the code.

@rkoshak
Copy link

rkoshak commented Oct 13, 2025

Is that something that can be done from a JS script? For example a rule template?

I think I've found ways for my templates to work around that but knowing it's possible might prove helpful in the future.

@florian-h05
Copy link
Contributor

Is that something that can be done from a JS script? For example a rule template?

I haven't written docs for that yet, but the 'use wrapper' directive can be used in any JavaScript condition in the UI.
It just has to be first or second line, i.e. top of the script.
For actions, you don't need it, there the wrapper is always on (unless you explictly disable it, but that's mainly implemented for the case there is any unexpected issue).

@rkoshak
Copy link

rkoshak commented Oct 14, 2025

So I can enable the wrapper but not disable the wrapper? I'm not trying to argue about anything, just understand in case it's helpful to me in the rule templates and so I understand how it works in Blockly so I can help on the forum better.

And don't you have actions and conditions backwards in your description. I thought the decision was to always enable it for Conditions and optionally enable it for Actions based on the add-on property. I could be remembering backwards, but that was what I thought. The reasoning was having the return statement in Conditions was worth keeping it always enabled, and doing so had minimal impact.

@stefan-hoehn
Copy link
Contributor Author

Not sure if it helps. As soon as Blockly adds one of these blocks, it adds "'use wrapper'" to the top of the script that it generates.

@florian-h05
Copy link
Contributor

florian-h05 commented Oct 22, 2025

You can also disable it, there are three ways of using the wrapper:

  • 'use wrapper'
  • 'use wrapper=true'
  • 'use wrapper=false'

The first two are equivalent.

And don't you have actions and conditions backwards in your description. I thought the decision was to always enable it for Conditions and optionally enable it for Actions based on the add-on property. I could be remembering backwards, but that was what I thought. The reasoning was having the return statement in Conditions was worth keeping it always enabled, and doing so had minimal impact.

I think you have to switch actions and conditions there. For actions, it is always enabled, since it has minimal to no impact, for conditions, there is the global add-on setting and the use wrapper directive to control the behaviour.

@rkoshak
Copy link

rkoshak commented Oct 22, 2025

You can also disable it, there are three ways of using the wrapper:

Excellent! Thanks!

@stefan-hoehn stefan-hoehn requested a review from a team as a code owner October 28, 2025 10:39
Signed-off-by: Stefan Höhn <mail@stefanhoehn.com>
Signed-off-by: Stefan Höhn <mail@stefanhoehn.com>
@stefan-hoehn stefan-hoehn force-pushed the blockly_migrate_event_attributes branch from 3902d38 to 5585292 Compare October 28, 2025 10:47
@florian-h05 florian-h05 merged commit 20db04f into openhab:main Oct 28, 2025
5 checks passed
@florian-h05 florian-h05 deleted the blockly_migrate_event_attributes branch October 28, 2025 17:12
@florian-h05 florian-h05 added this to the 5.1 milestone Oct 28, 2025
@florian-h05 florian-h05 added enhancement New feature or request main ui Main UI labels Oct 28, 2025
florian-h05 added a commit to florian-h05/openhab-webui that referenced this pull request Dec 20, 2025
Regression from openhab#3376.

Signed-off-by: Florian Hotze <dev@florianhotze.com>
jsjames pushed a commit that referenced this pull request Dec 20, 2025
Regression from #3376.
Also simplifies the generated code (the members of the event object are
now JS strings).

Signed-off-by: Florian Hotze <dev@florianhotze.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request main ui Main UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants