Extending Blockly with new openHAB commands

@rlkoshak @ysc

For quite a long time it annoyed me that you cannot log something directly that isn’t a string like this number

image

or this

so finally today I took a look at it and the reason is more than simple: We have actually addeda check on that block so that it only allows Strings to be connected. Even though this makes sure it is fool-proof but in most cases it does work with everything else as JavaScript is pretty clever to convert a Number to a String or many blocks return a String anyway (even if it was complex object) like here

So in a nutshell: I would recommend taking out the check as it would make a lot quite a bit simpler when the user wanted to log it (instead of artificially adding a " " or apply it to a variable first).

What do you think?

4 Likes

Sounds reasonable but one wonders why the check was put in there in the first place? There might be some reason the check is needed that I don’t know. Or it might have been a default and we don’t need it.-

1 Like

I checked it in the repo and it was originally added by Yannick as the 2nd commit of the blocklies, probably because a log expects a string. Anyways, I have worked with it intensively over the last days and works pretty well and it so much more convenient. Hence, I’ll remove the check with the next PR.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.

Announcement

A new major addition to Blockly has been contributed to the lastest version 3.3M6. It contains the following

Auto-Expansion of openHAB section

From now on the openHAB section is automatically expanded (on non-mobile devices)

image

A new Help Button

From now on, each openHAB section contains a “button” that sends you directly to the documentation page of that section by clicking on the grey “Help” button. For example this Help button

opens the page https://www.openhab.org/docs/configuration/blockly/rules-blockly-timers-and-delays.html

I hope that this will allow learning the blocks even faster because more people will know that there is comprehensive documentation without having to search for it (thanks Tweini from the blockly community for helping me here)

Comprehensive Extension to Datetime

Based on @deibich 's block library many new blocks are now available in openHABs Blockly and have been made even more flexible (note that it is not completely the same). Here is a visual representation of the blocks available:


image

As this is part of 3.3 which is not released yet, you can find the new documentation only at next.openhab.org for the time being:

https://next.openhab.org/docs/configuration/blockly/rules-blockly-date-handling.html

(thanks Tweini from the blockly community for helping me on some of the really tricky things here)

Logging

The log block has become more flexible. Previously you were only able to attach string blocks. Now you can attach any block returning a value and it tries to do its best to print out the blocks return value.

CRLF block

In some cases it is necessary to insert a line feed into a string. This can now be achieved via the CRLF block like in the following example (thanks to @Confectrician for requesting it). It can be found in the text section:

2 Likes

@stefan.hoehn

Hi!

How can i use your blocks? Im on 3.3M6. I need the event.itemname block but i cannot find it in marketplace.

br

This is fantastic, well done!

I am converting to the openHAB blocks, and have a question or two. I used to have this:

image

deibich_blockly_ChronoUnit.DAYS.between(deibich_blockly_ZonedDateTime.now(), waste) == 0;

I now want to use this:
image

chronoUnit.DAYS.between((zdt.now()),waste);

But it gets rejected by the blank = blank block. When I try to put it into the first blank slot, it gets pushed back out. Any idea why?

Second question: is there an openHAB block that will give me the day of the week, as from this marketplace block?

image


(A couple of minor points so far:

  • substract can be changed to subtract, which is far more commonly used in English)
  • I preferred the time between blank and blank text versus what’s now time blank between blank. The first is more friendly.)

image

…from openHAB → Run & Process

1 Like

soo blind…

thank you

What type is waste? Is it an item that returns a datetime? The block will not accept it as it only accepts a block of type datetime. Use the a datetime block with an item-getState like so:

Regarding the “day of week”: Good catch … I basically overlooked that. I will add that soon (to be done)

vs (new)

“substract” → changed it to “subtract” (done)

“time between” → you are right - it looks better: (done)

Hopefully I can push the changes pretty soon (update: is merged)

1 Like

waste is whatever type gets generated by this

I presumed this variable would be a datetime variable?

Note that my problem isn’t in getting waste into the new date block - it’s getting that date block into the blank = blank block.

I am confused what is happening :confused:

  1. A variable is a neutral type, so it fits always in any input entry

But it gets rejected by the blank = blank block. When I try to put it into the first blank slot, it gets pushed back out. Any idea why?

This works for me. I am sure I misunderstand your sentence.

Maybe you can provide a animated gif what you are doing and show what is rejected?

No problem - I’ll try tonight.

In your most recent screenshot: take that days between now and waste block and try to insert it into one of the blank spaces in the following block:

Screenshot_20220615-085922

That’s the bit that doesn’t work for me.

Hi . not sure if that is the right place to ask this question…

I really start liking blocky, as it now can do nearly everthing i was scripting before and is - for me - easier readable (specialy month or years later).

However i have the following challenge: I want to iterate trough a group and if an item is a groupitem iterate through that again…thats how i tried:

But it fails when it comes to the 2nd for each claiming it cant find an item

Just to explain: “gRooms” is the group of all my rooms, and “Kueche” is the kitchen which also is a group (containing all items in the kitchen).

Any good ideas, how to acomplish that ?

Ok, I found out what the root cause is: The condition block seems to make sure that the block type of both blocks you add have to be the same. If you add either one (dateblock or number block) of each it stays there.

As variables are neutral type you can use the following workaround (which isn’t nice but at least works for the time being):

As a quick side note as most people aren’t aware of it: the colors actually ought to express the return type and as you can see the colors are different here. With the fix I have now implemented (it is done already) it will then become blue expressing a number type.

Also the additional values have been now provided for the following block:

Thanks for the valuable feedback :+1:

1 Like

This works for me

There is a group called lights, which contains several subgroups which themselves contains items.
I intentionally used variables here to make the “code” more explicit.

Just to be clear, and because I promised you a GIF:

  • The days between now and waste on the left is made from the Marketplace (@deibich) block, using the now block from the same library. This creates the code:
var deibich_blockly_ChronoUnit = Java.type('java.time.temporal.ChronoUnit');
var deibich_blockly_ZonedDateTime = Java.type('java.time.ZonedDateTime');

deibich_blockly_ChronoUnit.DAYS.between(deibich_blockly_ZonedDateTime.now(), waste);
  • The days between now and waste on the right is made from the openHAB (3.3M6) block, using the now block from the same openHAB Dates & Times library. This creates the code:
var chronoUnit = Java.type("java.time.temporal.ChronoUnit");
var zdt = Java.type("java.time.ZonedDateTime");

chronoUnit.DAYS.between((zdt.now()),waste);
  • Save for the variable names, the code looks identical to me.
  • The block from the Marketplace is able to be inserted into the comparison block.
  • The block from openHAB is not able to be inserted into the comparison block

Peek 2022-06-15 17-58

So I’m still not quite sure why it’s not working. Is the Marketplace block identifying as a number, and the openHAB one isn’t?

Before I fixed my block it had the type “datetime” which is not equal to “number” on the right hand side. When I say type this is something that is defined in the code by the developer of the block (which is me). You can define what the output type of that block is and you can define what type is allowed as input for “holes” in a block. The library version is defined as “Number” (i double checked it), mine was mistakenly defined as datetime. The equals block makes sure that the left and the right hole get the same type (the blue “0” is a number type block), so it worked with the library block but not with the core block. This is why it it didn’t match the type in the comparison and it prevented it to connect.

However, I have fixed that and will provide the fix today (note that it needs to be reviewed and merged first before the fixes will become available)

2 Likes

That solves it then! Thanks for checking!