Extending Blockly with new openHAB commands

I saw you working on the new blocklib (Block Libraries - initial implementation (#1225))
which refers in its editor to “https://openhab.org/link/blocklib-tutorial”.
image

I checked the latest openhab-docs if I find something there but couldn’t. Have you written something there already so that I can start learning about it?

The link works now!
It points to:

Thanks for the writeup, it works now.

can you do me a favor and merge this last PR. It is a small extension to allow retrieving rules parameters within a script that was written with blocklies. This concludes the blockly “Run & Process” section.

Here is how it can be used when having provided it via a script call as shown in here

The PR is pretty small and should be straight forward

1 Like

We have now released this major update of openhab blocklies as part of openhab 3.2.0. It now contains almost 50 specialised openHAB blocks. Thanks to all who helped by contributing their ideas, reviews and feedback.

8 Likes

Thank you Stefan for your great work on the blockly blocks.
Are there any plans to integrate binding actions like sending a telegram notification or a push notification through the myopenhab service?
I work with telegram notifications and this is the last point I need to change to the blockly editor.

Thanks a lot for your effort!

Thanks Johannes.

Currently, I haven’t planned this but you may be able to contribute this in a way similar to the new Block library approach like in this example that was done for Twitter

see the Tutorial: How To Write Block Libraries

Check the Blockly section of the Marketplace. A number of these Actions have already been built and published there. And as @stefan.hoehn pointed out, there is a really good tutorial for writing your own.

Actually I had a Telegram library in mind for the initial set but lacked the time. :man_shrugging:

myopenHAB push notifications are in the built-in set under openHAB > Notifications.

It’s I think the only case of a set of built-in blocks that depend on an add-on but it makes sense to make an exception for OH Cloud.

1 Like

Hi Rich

I just noticed that when I retrieve tags or groups from items it does return a

java.util.Collections

Do you by chance know how to convert that into a Javascript list like so?

var list = [‘tag1’, ‘tag2’];

Unfortunately Collections is probably too generic. Do you know if it’s a List, Map, or Set? I think List and Set are treated the same but a Map is different.

It’s almost certainly a List or Set though given what is being returned. In my Nashorn rules I tended to just keep them as Java and used Java’s Stream API to do the filters, forEach, map and reduce. It also works with for(var I in list) .

There is a way to convert too a JS array and back though. I think it’s Java.from(list) which will convert the Java List or Set to a JS array.

Thanks for the guidance. That helped me fixing the blockly problem I detected.

Hi Rich,

did you ever notice that sending colors in OH Blockly doesn’t really work?

actually generates

events.sendCommand(‘Nano_Squares_Jonas’, ‘#ff0000’)

which isn’t really surprising but of course OH doesn’t understand #ff0000.

I wonder if we should make the send command block smarter by detecting the color-# (note that I am not able to recognize that there is a color block - I can only parse the output) and convert it into “255,0,0”.

The other option would be to provide an OH-Block that converts the normal color block to a OH Color.

What do you think?

I don’t have anything that uses color in my setup so I wouldn’t have ever noticed that.

They both have their pluses and minuses. The former means that users can manually send an RGB string in other ways, not just the color block. However if there is ever a case (and there will be cases) where someone needs to use a # for any other reason that use case will break.

The latter would be a bit more transparent to the user though and it should avoid breaking anything so if that’s possible I’d use that approach.

1 Like

Just to be clear, you would propose to add an openHAB conversion block that converts e.g. #A0B0C0 into “160,176,192” ?

Another option would be add a “send Color Command” to the block like so

How do you like that idea?

Not really a color user I’m not sure I have a preference either way. I just would not want to have it implemented in a way that the code will always treat any string starting with # as a color. That will potentially break other use cases.

That was my idea to provide that particular send command that has this special behavior.

@ysc Any preference you would have in this case?

image

Definitely not a new “send color command” IMO!
There are plenty of RGB-to-HSB conversion functions available, just choose one that’s concise, bear in mind they might have other ranges for the H,S,B components than openHAB’s 0-359,0-100,0-100.

Also perhaps one to build a HSB color from its components:

image
(only have to join them with ‘,’)

Here’s a RGB to HSB function that works in Nashorn:

function hex_to_hsb(hex) {
  var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  if (!rgb) return '';
  var r = parseInt(rgb[1], 16) / 255, g = parseInt(rgb[2], 16) / 255, b = parseInt(rgb[3], 16) / 255;
  var v = Math.max(r, g, b), n = v - Math.min(r, g, b);
  var h = n === 0 ? 0 : n && v === r ? (g - b) / n : v === g ? 2 + (b - r) / n : 4 + (r - g) / n;
  return [60 * (h < 0 ? h + 6 : h), v && (n / v) * 100, v * 100].join(',');
}
print(hex_to_hsb('#ff0000'));

Taken from framework7/utils.js at ce7a2dc21b29a05d6c782bd53a4155f542867d9c · framework7io/framework7 · GitHub (colorHexToRgb) and RGBToHSB - 30 seconds of code.

Thanks Yannick, very much appreciated. I will then do a block for that soon as part of the new series of smaller blocks to be added to 3.2M1 3.3M1.

3.3.M1 :wink:

1 Like