Extending Blockly with new openHAB commands

Announcement: New Blockly Workspace Zooming

I provided another enhancement for blockly: From now on, you can zoom the Blockly Workspace:

image

You can find this in the bottom right corner. This also allows pinching on touch screens or touch pads.

Not to conflict with the “old” Code-Button, this was “moved” into a new version into the footer of the workspace (Kudos to @ysc):

5 Likes

I have a generic Blockly script that runs when a light group is triggered by a motion sensor. It then waits a certain amount of time to turn off the light or in the case of a retrigger of the motion sensor keep the lights on. I’ve tried to do this with the timers available in the OpenHAB section but that doesn’t work properly as timers (or probably the event context) is lost when a different light group is triggered.

I’m now using the common blocks from this community marketplace post: Collection of common blocks. Basically the timer block that keeps the triggering event context. That block actually works great and allows me to create the desired automation.

Is it possible to include the common blocks (or at least the specific timer one that keeps the event context) into the main OpenHAB section? It’s to avoid that I’m dependent on an ‘external’ block instead of what is available in OpenHAB out of the box. Thanks for considering!

As @rlkoshak is our genie on timers I loop him in here:

Rich, the first block is the one that Marco mentions, the second is ours

image

The code difference is minimal

if (typeof this.timers['MyTimer'] === 'undefined' || this.timers['MyTimer'].hasTerminated()) {
  this.timers['MyTimer'] = scriptExecution.createTimerWithArgument(zdt.now().plusSeconds(10),event, function (event) {
  })
} else {
  this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(10));
}

if (typeof this.timers['MyTimer'] === 'undefined' || this.timers['MyTimer'].hasTerminated()) {
  this.timers['MyTimer'] = scriptExecution.createTimer(zdt.now().plusSeconds(10), function () {
    })
} else {
  this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(10));
}

The only difference is that it uses

  • createTimerWithArgument
  • add the event-variable as an additional parameter and forwards it to the function

Could we not just do that in general?
If not, we could give the timer-blocks an additional choice to choose from but I need to check of that would be backward compatible, so it doesn’t break old blocks.

What do you think?

There are subtitles involved here that probably needs to be explored before deciding how to proceed. The big concern is that

  1. sometimes you want to have the event and possibly other stuff as it was when the timer was created

  2. other times you want to have the event and other stuff as it is right now, possibly having been changed since the timer was created on a subsequent rule trigger.

The difference is subtle but important. Ideally both use cases should be supported. I personally prefer using function generators because it lets you “fix” multiple variables rather than just the one variable. For example:

var timerGenerator = (one, two) => {
  return function() {
    // function called by the timer which uses arguments one and two somehow
  }
}

...

scriptExecution.createTimer(zdt.now().plusSeconds(10), timerGenerator(event, foo));

We call a function passing the arguments and get back a function that will get called by the timer. The passed in arguments become fixed and won’t be overridden when the rule runs again. But that’s a pretty big lift for the typical blockly user (though I think it should be possible).

I think maybe splitting the difference would be be better. Rather than forcing the argument passed to the timer to be the event, why not let us pass our own argument, including event? But I think that would involve creating a new “timer with argument” block where one can select the event or any other variable defined.

The first Blockly video tutorial has been published on the 21st of June on the openHAB Video Channel.

See the announcement here for further information.

1 Like