Extending Blockly with new openHAB commands

@ysc here is the PR to be reviewed and merged.

TIA
Stefan

Hi Rich,

I need your feedback. We have an issue in the timer handling and there is a bug to it: https://github.com/openhab/openhab-webui/issues/1214

It has to do with your proposal to set the timers to undefined. Either I misunderstood something and implemented it wrong or we have an issue that has to be fixed.

See the following

The intention is obvious: it should start after 5 seconds and retrigger over and over again but it doesn’t … and I will explain why…

First let’s make this simpler first by taking out the manual retrigger in the timer like so:

which generates the following code:

1 if (typeof this.timers['MyTimer'] === 'undefined') {
2  this.timers['MyTimer'] = scriptExecution.createTimer(zdt.now().plusSeconds(5), function () {
3   logger.error('timer triggered');
4   this.timers['MyTimer'] = undefined;  // see THIS HERE
5  })
6 } else {
7 this.timers['MyTimer'].reschedule(zonedDateTime.now().plusSeconds(5));
8 }

As far as I remember you wanted to reset (=undefined) the timers after having used it, so I implemented it to appear in line 4.

The problem is if I put back the manual reschedule

it is added before line 4 from above (becoming line 7) and then the whole thing doesn’t work anymore because upon entry in line 4 it is undefined and then line 5 won’t be executed (which is good because it would result into an NPE anyway)

1 if (typeof this.timers['MyTimer'] === 'undefined') {
2 this.timers['MyTimer'] = scriptExecution.createTimer(zdt.now().plusSeconds(5), function () {
3    logger.error('timer triggered');
4    if (typeof this.timers['MyTimer'] !== 'undefined') {
5       this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(5));
6    }
7    this.timers['MyTimer'] = undefined;
8  })
9 } else {
10  this.timers['MyTimer'].reschedule(zonedDateTime.now().plusSeconds(5));
11}

So the only way to fix this is to remove

this.timers[‘MyTimer’] = undefined;
from the generated code but I remember you wanted to have this for a good reason…

It needs to be an if/else.

if (typeof this.timers['MyTimer'] === 'undefined') {
this.timers['MyTimer'] = scriptExecution.createTimer(zdt.now().plusSeconds(5), function () {
   logger.error('timer triggered');
   if (typeof this.timers['MyTimer'] !== 'undefined') {
      this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(5));
   }
   else {
     this.timers['MyTimer'] = undefined;
   }
 })
} else {
  this.timers['MyTimer'].reschedule(zonedDateTime.now().plusSeconds(5));
}

If one is going to create a looping timer, there really needs to be additional criteria to determine whether or not to reschedule or not or else the timer will loop forever. The user must define some other test (e.g. an Item reaches a given state, a certain number of loops have been performed, a certain amount of time has passed, etc.).

A looping timer is a pretty advanced need so I don’t know how much we need to spend on supporting them right now. The more I think about it, it’s probably going to require a whole new block to generate that if/else construct. There really is no way to code a looping timer without that sort of construct.

Removing the setting of the timer back to undefined breaks a bunch of other more common timer use cases.

1 Like

While thinking about finding a solution I wonder of that makes sense because isn’t “this.timers[‘MyTimer’]” in line 6 undefined already because it is the else branch of !== ‘undefined’?
So why would I set it to undefined if is already undefined?

I think the right thinking is that want to suppress the setting to undefined only in case I reschedule a timer? :thinking:

Here is a less theoretical example of a looping timer.

var count = 5;
var this.timer['MyTimer'] = ScriptExectution.createTimer(zdt.now().plusSeconds(5), function() {
    if(count > 0) {
        count = count - 1;
        this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(5));
    }
    else {
        this.timers['MyTimer'] = undefined;
    }
});

Compare that to your code. You’ve missed the whole inside the Timer function part of it all.

But now you have to consider the case that the Timer exists and is counting down when the rule is triggered again. So we add:

if(this.timer['MyTimer'] === undefined) {
  var count = 5;
  var this.timer['MyTimer'] = ScriptExectution.createTimer(zdt.now().plusSeconds(5), function() {
    if(count > 0) {
      count = count - 1;
      this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(5));
    }
    else {
        this.timers['MyTimer'] = undefined;
    }
  })
}
// else do nothing

If the timer doesn’t exist (i.e. is undefined) when the rule runs we initialize a count variable. We create a looping timer. *Inside the function that the timer runs we check to see if it’s run five times. If not, we decrement the count and reschedule the timer. If it has we set the timer to undefined and are done.

If the timer does exist when the rule runs we do nothing. Of course, if the timer did exist you could cancel the old one and start a new one. That’s a valid use case to.

But the key point is inside the body of the Timer you have two choices: reschedule or exit. The User needs to define under what circumstances the timer is rescheduled (or conversely when the timer should stop looping).

It’s not as simple as not setting the timer to undefined when looping and setting it to undefined all other times.

NOTE: the count variable is just an example. A user may want to wait for an Item to reach a certain state, or a certain amount of time to have passed, or some other criteria.

Hi Yannick,

I only today stumbled over something that really took me quite a while to find out why it didn’t work and I wonder how we can improve this.

I took the following block

and then I used the cog to add a value like so

and ended up having this

then I tried to attach a value like so

but it wouldn’t connect. I even looked into the code and didn’t find a setCheck that validates the block type. There isn’t any. The problem is completely different and completely unobvious.

You must not use the default dictionary block (the shadow block, that is) that comes with the run script

Instead you have to bring in a new dictionary block (which by the way has a darker color than the shadow_block)

and then everything works as expected

Do you have any idea what the root case is that the shadow block doesn’t behave like the real one and what we can do to guide the user more clearly the s/he must add a real dictionary first before it will work?

At least we should make this clearer via the tooltip, I think.

Yes I noticed too but don’t have an explanation. Maybe blocks with mutations simply cannot be put as shadow blocks, or maybe it’s possible to hide the mutation “cog” button so the shadow block is an empty dictionary that you can’t change unless by dragging a real block. Otherwise I’m not hostile to the idea of making it a real block, it’s contrary to the “norm” but at least it would work.

What about something like

if (typeof this.timers['MyTimer'] === 'undefined' || this.timers['MyTimers'].hasTerminated()) {
...

I suspect the rationale was also to avoid keeping references to terminated timer instances indefinitely in memory causing them to never be GC’ed, but I’m not sure either setting them to undefined would do that.
And there’s also the logic flaw that the “timer has terminated” block
image
which generates code like

typeof this.timers['MyTimer'] !== 'undefined' && this.timers['MyTimer'].hasTerminated();

is pretty much useless: it would never return true - either the timer is not set, or pending, or running, or has effectively terminated and thus unset again.

I’m not sure either. One would have to look at the scheduler itself to see how well it cleans up after itself.

That would certainly be a little more fool proof I think. Indeed the the timers would never be GC’d but I can’t imagine one would have so many stale timers hanging about where that would make a difference. My assumption is that the scheduler that handles the timers would be smart enough to clean up terminated timers so it should only be the reference in the rule that would keep it from being GC’d. But that’s an assumption and not actual knowledge.

Changing to a check like that would let us eliminate the setting of the timer back to undefined automatically at the minor expense of some extra memory usage.

So for this

image

the result would become

// Change LINE 1
1 if (typeof this.timers['MyTimer'] !== 'undefined' && this.timers['MyTimer'].hasTerminated()) {
2 this.timers['MyTimer'] = scriptExecution.createTimer(zdt.now().plusSeconds(5), function () {
3    logger.error('timer triggered');
4    if (typeof this.timers['MyTimer'] !== 'undefined') {
5       this.timers['MyTimer'].reschedule(zdt.now().plusSeconds(5));
6    }
7    // this.timers['MyTimer'] = undefined; <= take out this line
8  })
9 } else {
10  this.timers['MyTimer'].reschedule(zonedDateTime.now().plusSeconds(5));
11}

or simplified

image

the code would be as follows

// change LINE 1
1 if (typeof this.timers['MyTimer'] !== 'undefined' && this.timers['MyTimer'].hasTerminated()) {
2  this.timers['MyTimer'] = scriptExecution.createTimer(zdt.now().plusSeconds(5), function () {
3   logger.error('timer triggered');
4   // this.timers['MyTimer'] = undefined;  // Take this out 
5  })
6 } else {
7 this.timers['MyTimer'].reschedule(zonedDateTime.now().plusSeconds(5));
8 }

Note that I haven’t tried that out yet (but I sure will as soon as you confirm)

I think those will work. Once the timer is created there will always be that Timer. But I think that is probably OK.

The looping timer is a little contrived and would like to see any docs or examples include an if statement to determine whether or not to reschedule inside the timer. That is a more typical use of a looping timer instead of a timer that loops forever. If someone wants that, a cron triggered rule would be a better choice.

No, if the timer is undefined or has terminated - the first code fence in my post above. The above condition will never be satisfied since the timer is defined just below in the if branch.

Apart from that this seems reasonable. We would keep timers indefinitely in this.timers, and the create timer would simply redefine them after they are terminated. The reference to the old timer would be lost and maybe it would be GC’d eventually.
A minor optimization would be to simply reschedule them if they still exist but are terminated (see the remark in Timer (openHAB Core 4.2.0-SNAPSHOT API) - “This can also be called after a timer has terminated, which will result in another execution of the same code.”), but it would lead to more code we can do without.

Doh! I must be tired. Of course it needs to be or. Can’t believe I missed that.

That assumes that the function called by ‘MyTimer’ will always be the same. You couldn’t have an if statement and schedule a different function to be called in different circumstances under the same Timer name.

Of course you can just use a different Timer name in that case but it might force the user to writing more complicated code than they otherwise would have to.

Dummy me - I actually had noticed that or and by copying I did it wrong. I love the 6-eyes-principles - good you are here.

I will try to fix that and add to the already pushed PR. Maybe, Yannick, you are able to review and merge.

Btw, just for the record, I have updated the first post at the very top to reflect the latest version of the blocks of M5.

1 Like

Yes that would be great. I can’t accept any more features but bugfixes are still ok.

you mean make another one? To avoid Git snafus for single-file edits you can use the GitHub edit file feature (Sign in to GitHub · GitHub).
Sign-off in the commit message is still required, get it from a previous commit.

Sorry for the confusion. I hadn’t seen you have merged the PR. Thanks. Will hopefully be only minor things to fix this issue above.
I would create a new PR for that fix.
New features will then come with a new PR and as you mentioned lately I will target them with small PRs.

@stefan.hoehn I ended up doing the PR: Fix Blockly timer code by ghys · Pull Request #1236 · openhab/openhab-webui · GitHub as I saw another issue and I wasn’t sure if I will be available to review yours before the RC1 build. Thanks!

Ok, thanks.

I can confirm it works now as expected. This is how I tested it (for the curious, I used two different trigger items and I distinguish those with the new block that retrieves event information, i.e. in this case the event item’s name):

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