Merging Blockly snippets

Is there any way to merge blockly snippets from Rules/Scripts?

Scenario:
Rule A with Blockly code (‘execute a given script’).
Script B with Blockly function foo.

Plan:
Insert the Blockly code of foo into the Blockly code of Rule A.

Sure, you could switch to JavaScript and merge the JavaScript code snippets, but you would sacrifice the Blockly visualization …

I would suggest adding a ‘Code’ tab to Blockly Scripts:

While the Blockly source code within Rules can be copied from the Code tab

there is no such possibility for Blockly Scripts:

As an alternative, a third toggle button for switching to Blockly source code could be added to:
grafik

Any thoughts?

It is possible, not nice, but possible:

Create a rule, e.g this one:

GO BACK and you are back on this screen:

Click on on code:

Copy the part BETWEEN

<xml xmlns="https://developers.google.com/blockly/xml"> 

and

</xml> 

Which would be

<block type="oh_log" id="mN2s}wmmq3TL]a98gS3b" x="87" y="263"><field
name="severity">info</field><value name="message"><shadow type="text" id="PMF^~Fi|6O}[HKZ(Jue@"><field name="TEXT">abc</field></shadow></value><next><block type="oh_event"
id=".a*jJUM$+nXO3hQPaHk|"><field name="eventType">sendCommand</field><value name="value"><shadow type="text" id="%h;rga9L8pX]Miruq}8e"><field name="TEXT">value</field></shadow></value><value name="itemName"><shadow type="oh_item" id="s:tgkg!@jDRG@sdSj;ea"><field name="itemName">MyItem</field></shadow></value></block></next></block>

Now create a new rule, open the CODE tab as above and copy the XML into the same two xml-opening and endings.

I just tried and it works.

Thanks for testing; my point is that the same procedure doesn’t work for openHAB Scripts: you can’t switch to XML code view because there is no “Code” tab for openHAB Blockly Scripts. I’m sure you can extract the XML code for openHAB Blockly Scripts from the openHAB configuration file, but that would be a bit too cumbersome (and error prone) for the average user …

tl;dr
Do not use openHAB Blockly Scripts if you plan to copy part of them to Blockly scripts within openHAB Rules at a later time.

What do you mean with openHAB Blockly Scripts? I am confused…

grafik

Please read Merging Blockly snippets - #2 by Ap15e.

Ok, now I know what you mean. Yes, that’s true because you are missing that page. You can create a feature request for that if you like.

In the mean time, you can use the API explore to remove the “script” tag and the script will appear as a normal rule.

But this use case seems like you are looking for more of a library call. Under Developer Tools is a place where you can create Blockly Libraries which basically means creating your own blocks which can be reused in your Blockly scripts.

Also, there is the ability to call other rules. So you could create your Script (which is just a special case of a rule) and directly call that from your other rules.

You can also call one rule/script from another rule.

Your code foo can be in script C, what’s used by rule A and script B

Thanks for the suggestions.

Using API Explorer, I managed to extract the Blockly source code from my Script and paste it into the code window of my Rule. The Blocky parser/renderer doesn’t seem to like duplicate variable names (Blockly ids were different though), so I had to remove the duplicate variable from the XML code and fix the ids - it works, but it’s an error-prone mess.

Lesson learned: Avoid prototyping using Scripts. :slight_smile:

@stefan.hoehn : I just stumbled over the copy&paste plugin of blockly (@blockly/plugin-cross-tab-copy-paste Demo). Maybe you already know it? What do you think about integrating it in openhab?
With this plugin you can copy&paste blocks between different tabs. Maybe even copying blocks from the marketplace could be possible in the future?

1 Like

Thanks for the hint, I noted it down. That looks nice.

I am just in the midst of rewriting Blockly for the new GraalVM which we need to finish first but then I’ll have a look at it.

1 Like

Quick feedback: Unfortunately it requires blockly 9.0.0 and we are still on 6.x. Upgrading is major topic as it requires other dependencies as well where I definitely need to have support from @ysc

Thanks for your quick reply. Then maybe one day… Anyway: it’s really fun with blocky and astonishing what you can realize with it. Merry Christmas!

Thanks for that - “applause feeds the hunger of the artist” :wink:

1 Like

See Increase blockly version to 9.x · Issue #1608 · openhab/openhab-webui · GitHub :wink:

Actually it wasn’t one day but four but I hope that’s okay? :laughing:

I had created a little animated gif to show it in action but it is too big to upload it here, though I can tell you it really works - unfortunately the pictures are not as impressive as if it would be shown animated:

a big thx goes to @ysc who quickly helped me to upgrade blockly to the latest version (and also added support for the dark-mode).

3 Likes

That’s amazing! Can’t wait to use it. So this will be part of the next 4.0 milestone?

Yes, it will be. It is working on my machine already.

1 Like