Not (just) for kids: Try Blockly

(Preliminary remark: I wrote this as an answer to somebody looking for help regarding syntax problems with DSL. But I noticed it’s off-topic.)

Try Blockly!

It greatly reduces or eliminates

  • nasty type conversations
  • finding the right syntax
  • debugging time

In OH4 you can solve almost any problem of your rules with Blockly.

Blockly has been improved a lot in OH4 because of the contributions from @stefan.hoehn.
Currently @rlkoshak is developing a new set of blocks for advanced stuff. (Check it out in the marketplace)

I promise that almost any non-daily programmer (like me or all DSL users) can save tons of time.

Caveat: For a joyful experience you should have 64-bit OH4 installed and this requires at least the hardware-performance of a Pi4 with 4GB.

I wished there would be more questions, discussions and examples about your Blockly rules and less repetitions how to get newState in DSL.
Enjoy your home automation with Blockly!

11 Likes

… also I noticed that it has become a pretty good source of learning to write Javascipt rules as you design it first with Blockly and then dig into the code tab to learn how it is written in Javascript.

And if anyone is looking for the docs and tutorials:

6 Likes

Yes, the documents and tutorials are really helpful. I’ve seen that “cross-rule copy” is now very well documented. One of the many great new features that many users probably don’t know about (you can copy block blocks from one rule to another).

For my taste, the reference’s introduction feels a bit dated: Blockly is no longer just for novice programmers (I’d consider myself fairly experienced :wink:), but a powerful tool suitable for almost anyone who’s not expert in Javascript. Or if you like to play with your rules on the phone, Blockly works quite well. In my opinion no one should start using DSL anymore. Blockly is much more powerful and gives you an easy way to switch to Javascript later (if you ever want to do that).

3 Likes

I liked the blockly based rules since they were introduced in OH. And since I’ve looked at Homeassitant, I’ve really come to appreciate them and never want to miss them again :smiley:

2 Likes

Feel free to provide a PR for the docs to describe that in a different way.:+1:

While at Fosdem I had a lot of conversations with HA users who, after seeing Blockly, told me that definitely give openHAB a try because they loved what the saw here in particular because Blockly is not a dead end but you can always copy the generated code and proceed from there and also learn from the generated code which provides a really flag learning curve.

And the other way around is possible too: If Blockly is not enough then you can use the inline-script-block and paste any JavaScript-code into it. Great for interactions with the system like reading or writing a file, like e.g.:

I second this. Rules DSL was great in this day. It was simplified in a way to make it easier to grasp for non-programmers but it also came with some limitations and weirdness.

Now I see no advantage in using Rules DSL. jRuby and JS Scripting with the helper libraries are no more complex than Rules DSL now and Blockly far exceeds Rules DSL in both understandability and usability for non-programmers. All of these surpass Rules DSL in what they can do for there are a number of OH features that cannot be accessed from Rules DSL.

If I were to make one change, it would be too make it easier to create personal Block Libraries. I know this probably isn’t feasible but it would be really awesome! Maybe a way to export a function block into a Block Library could be a compromise between defining new blocks and having a way for users to create a personal library of reusable code. But even without that the copy and paste between rules is a boon.

I’m currently doing the manual version: a rule where I copy all the code snippets I want to remember, each snippet as a separate action, encapsulated in a repeat block so they can be easily cross-copied.

And wouldn’t it be great if it was possible to display Blockly programs in community discussions without screenshots, e.g. with the ability to copy blocks directly from them.

Based on what I know that’s not going to be doable in a way that is going to be intuitive. If you look at the “code” tab of the rule, you’ll find a block of XML. The XML is the actual blocks definition. So if you post the XML it can be copied and pasted from the forum. You’ll need to paste it into the Code tab though and the XML is almost unreadable for mere humans.

But it is possible. We just need the screen shots too.

Example:

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="oh_log" id="i=X5Vb{vY|KY4^=np6by" x="237" y="183"><field
        name="severity">info</field><value name="message"><shadow type="text"
        id="`r`A*|l8_/!Mbeg;0Mh7"><field name="TEXT">abc</field></shadow><block
        type="text_join" id="`DdE!nyD:AE;e;aG?xM8"><mutation
        items="2"></mutation><value name="ADD0"><block type="text"
        id="re`/ntQCh;u_bv^}yad|"><field name="TEXT">triggering item
        is</field></block></value><value name="ADD1"><block
        type="oh_context_info" id="IU@Z5/[TwV-MO`ZxPI@X"><mutation asType="null"
        contextInfo="itemName"></mutation><field
        name="contextInfo">itemName</field></block></value></block></value></block></xml>
      type: application/javascript
      script: |
        console.info(('triggering item is' + String(event.itemName)));
    type: script.ScriptAction

The blockSource field is the actual blockly definition that gets rendered to

You can copy the blockSource from a forum posting and insert it into your rule. But it takes some work to map that block of XML to the actual fields you are seeing so “Adding” the blocks to your existing rule takes some finesse.

1 Like

What are you thinking of? In what way would you want to “copy” blocks? By allowing a “blockly screenshot” from openHAB? AFAIK there were several proposals in the blockly community for this but I couldn’t find any out of the box-working blockly plugin for that unfortunately

The helper libraries are I guess THE thing that makes the difference.

Everything related to openHAB that can be done from JS Scripting can also be done from Nashorn, but the helper library makes it much much easier to use and brings very detailed docs.
The other JS Scripting benefits such as CommonJS module support come on top.

In fact I think that the jRuby helper library is even more powerful than openhab-js, e.g. jRuby allows the creation of profiles or additional Item providers, which is no part of openhab-js (yet?).

ok. I started the PR (although it is difficult for me to match the lyrical brilliance of the original)

Which PR?

It’s just on sentence that I changed

1 Like

That can be avoided if the code of the whole configuration block or even of the whole rule is provided in the forum. Then you can paste that code into a new rule and use the new cross-rule-copy feature of Blocky. If necessary with an encapsulating block so that all blocks can be copied in one move.

The downside: Working in the code tab feels uncomfortable or dangerous for inexperienced users. That’s why it would be great if this manual copy-paste could somehow be avoided by directly entering the block editor from a forum rule. I’m gonna check if something like that exists in the blockly world (but if I understand @stefan.hoehn 's comment he already checked that).

Any idea how this works and in particular could work with openHAB, Jim?

No, my son told me about it. I have a few more stuff for blockly waiting for the V10 upgrade to get merged

Unfortunately I had to take care about the doc build upgrade on the weekend, I hope to approach yours next weekend.