Waiting a certain amount of time in blockly

  • Platform information: Openhabian on RPi4
    Hello,
    I am trying to realise a simple sunrise script with an rgbw mqtt light. Now I wanted to realise this with a script, but I cannot find a delay block in blockly. From what I understand, blockly is based on JS, so there is a wait logic in JS, which IOBrocker implements, but Openhab does not seem to?

If this is not there, this should be implemented asap, as this seems to be a very important feature for scripts to have.
Thx for any answers and any help :slight_smile:

The blocky interface is not 100% complete, it’s true. If you really wish have a delay or wait block you can head over to github and make a feature request, or even better make your own PR for it.

There are, however, many other options also available to you. If you are willing to expand just a little beyond blocky you can write your script in one of the support languages that will include a delay function.

Also, there are often much better solutions to timing than just adding a delay to a script. If you provide a little more detail about what you want your script to do it may be that someone can help you find a solution without waiting for an full OH update that includes your feature request.

Okay, yeah, this sounds resonable…
I would love to get into developing for this. I am familiar with java from the university and have some time at hand, as the semester is over.
What would be a good starting point for getting into developing for openhab?
I never worked on any big project, but I hava some experience programming my own stuff, Arduino, ESP32 and such.
Is there a good starting point for getting into OH?

For what I want to do without having a delay…
I want to slowly dimm up the lights. This should be triggered when the alarm on my phone rings, but this is a different issue…
I think it should be possible by just getting the epoch time and checking wrather it has changed enough to perform the action, but is there any way to get time down to miliseconds in the scripts?

The two best starting points are the openhab docs and to spend some time just search and browsing these forums. You’ll want to decide which scripting language to focus on. The three most common options are the Rules DSL, which many people find easy to start with as it is fairly readable, but doesn’t have quite as much functionality. Other common options are python, or javascript (groovy is available but has some of the least community support as far as I can tell). JS and python are far more powerful (but as a result a little more in depth). You’ll find many posts of the various scripting language options discussing pros and cons. You’ll also find many posts that are just examples of common script actions and hints about efficient ways to do things and links to helper libraries that other users have created to make certain common tasks less cumbersome.

If you really want to stick with the visual building block technique you will find posts here about how to connect node-red up to openhab and create node-red flows with far more complexity than what is available through blocky.

Blocky, however, could be a very valuable resource, if you decide to try javascript. When you make a blocky script, there’s a little blue button image in the bottom right-hand corner. Pushing that will show you the underlying javascript that corresponds to your blocky code and this is a very good way to start to get comfortable with the basics of javascript in OH.
image
becomes

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);

if (itemRegistry.getItem('MyItem').getState() == 'Some State') {
  logger.info(('My item just changed to: ' + String(itemRegistry.getItem('MyItem').getState())));
}

Obviously, you can only learn the js that has corresponding blocky blocks at the moment, but that’s a pretty solid foundation.

I have precisely this functionality enabled in my system. I use a timer for this. There is a mountain of information on timers for all of the different languages here in the forums, but the basic idea is that when you create a timer you give it a set duration and some commands. When you start it up it waits for that duration and then executes the commands.

In my case, I have a function that checks if my light is already at full brightness. If not, it then increases the brightness by 5% and creates timer with a command to run the same function again after 30 seconds. So, when my alarm goes off, the function gets called for the first time and then every 30s it gets called again until the light is at full brightness.

Delays/Wait/Sleep got a bad rep from OH2 because of thread handling. The issue isn’t as much of a problem with OH3 so a loop with a delay statement is also an acceptable solution in this case, particularly as it seems unlikely that instances of this rule might need to overlap.

Thank you for the pointers, I did not know you could write scripts in python, sounds compelling.

But my question actually was regarding on how to get started implementing the blockly functionality. I have the eclipse oh installed, but am quite overwhelmed by the amount of definitions. As I said, I never developed for such a large project.

The last two sections in the docs I linked in my previous post give guidelines and pointers for contributing. Beyond that, Asking questions in this forum under the developers topic should get your specific questions answered.

Okay, thank you!

There is already a PR started to implement a number of the missing things in Blockly you can find at Initial Blockly updates by bigbasec · Pull Request #875 · openhab/openhab-webui · GitHub.

FYI, blockly has been very much extended with 3.2.0 and should now cover most of what you usually do within rules (your mileage may vary though ;-)), so it is much more complete now.

Have a look here Blockly Reference and the discussions that led there. As an experience Java Dev I used to be writing everything in DSL and JS before but now I do probably 90% in blockly which is faster and more failsafe.

@javamaster10000

Jep, I updated about a week ago and blockly is quite powerful now. For me it was the inconvenience of having to pull up a network share to edit rules, that kept me from doing much more. Blockly seems to do the job quite well now.
Thanks for the hint!

1 Like

This seems to work for me

image