openHAB Rules Tools [4.1.0.0;4.9.9.9]

Without the logs there’s nothing to tell you that an upgrade is required. I make all my rule templates and this block library test to see if the version of the library is recent enough and generate a meaningful error in the log. Otherwise it would throw some random exception.

I also put the minimum version in the dependencies section of the original post, but there is no section in the template for block libraries.

Hi,
I am trying the new Countdown Timer but i am facing this error.
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘4cf9f4f077’ failed: org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).private.get(…).isActive is not a function.

The first block of timer its working but the second block with the countdown it gives the above error.
Any thoughts??
Thanks
Nikos

The OHRT timers do not support (yet) all the capabilities of a generic OH timer. CountdownTimer does not have an isActive() method. It has the hasTerminated() but that’s it. It’s a todo to add the rest but for for now you can’t use “is active” with OHRT timers.

Thanks for your fast respond.
So i must wait for an update.
Nikos

Or change your approach to test for hasTerminated() or test to see if the timer exists instead of using isActive().

if(cache.private.exists("test1") && !cache.private.get("test1").hasTerminated())

Which block is the timer exists ??

image

i dont have this block :frowning:
edit ok i found it.

Unfortunately it doesn’t make what i want. i will wait until you implement the rest functions on the count timer.

Thanks

What is it you want, because I have little time and cannot guarantee when I’ll add the rest of the functions. It could be months. And you should be able to do exactly what you are after with just the exists and hasTerminated blocks.

But the first thing to note is that your code as written doesn’t make sense. When the if statement runs the timer will always be active so it will always be canceled.

The following should work,.

Looking at the code this generates, it turns out that the “has terminated” block already tests whether the timer exists in the cache for you so we don’t need to do that separately.

Per the docs, isActive

returns true if the timer will be executed as scheduled, i.e. it has not been cancelled or completed.

and hasTerminated

returns true if the timer has been cancelled or the code has run and completed.

So isActive is just the inverse of hasTerminated.

image
is the same as
image

The above example was just to demonstrate that it gives an error.
I just want to avoid that the item that hold the count timer to run multiply times.
if item x=ON run a count timer but if x=ON again must stop the timer and not creating another timer. because the item that hold the coun timer showing 2 or 3 different count timers :slight_smile:
Hope that i have explain it good for you.

Then do the if statement before creating the timer.

if not [private] timer "test1" has terminated
do  
    log info "cancel timer"
    cancel [private] timer "test1"
else
    after [10] [seconds] with [private] "test1" using countdown item [item Testnumber]
      log [info] "Do something"

If the timer already exists and it hasn’t run, it will be cancelled. Only if there is no timer or the last timer was cancelled or ran to completion will it create a new timer.

if you run this blockly 2 or 3 times and observe the item you will see that the item counting 2 or 3 different timers so the “has terminated” block doesn’t work or i understand it wrong??

Yes you understand wrong.

You still need an if statement to test for “not has terminated” before the block that creates the timer.

You still have to cancel the timer if it exists and not hasTerminated before you create a new one. And that doesn’t happen automatically. You need to add an if block to test whether the timer has not terminated and cancel it if that’s true.

Bear with me i am not a programmer this is why i am trying with the blockly :slight_smile:


like this the timer its doesn’t start at all

What do you see in the log?

Rule ‘4cf9f4f077’ has been updated.
2024-04-19 17:01:28.267 [INFO ] [nhab.automation.script.ui.4cf9f4f077] - cancel timer

OK, we do need to test for existence separately. I charted out the values and the current code is returning true in cases we don’t want it to.

The current code is

not (exists && hasTerminated)

Charting out the values

exists True False
hasTerminated True True False
hasTerminated False False False

And then we negate that value resulting in

exists True False
hasTerminated True False True
hasTerminated False True True

That means it will be true even when the timer doesn’t exist. That means we’d never get to the case where we actually create the very first timer.

So we need to test for existence separately even though it’s already tested in the code of the hasTerminated block.

That results in:

if not (timer test1 doesn't exist) and not (timer1 exists and timer1 has terminated)

Using the values from above that give us the table:

exists True False
not (exists and hasTerminated) True True False
not (exists and hasTerminated) False False False

That’s what we want. It’s only true when the timer exists (i.e. not is undefined) and it has not yet terminated (i.e it exists and hasTerminated returns false).

All the “not” blocks is unfortunately but they are a result of how the other blocks work.

I modify the blocks like yours, now the timer its running but again if i run again the code its creating another timer and so one, without to cancel the existed ones.

Logs?

Rule ‘4cf9f4f077’ has been updated.
2024-04-19 17:47:24.949 [INFO ] [openhab.event.RuleUpdatedEvent ] - Rule ‘4cf9f4f077’ has been updated.
2024-04-19 17:47:51.722 [INFO ] [nhab.automation.script.ui.4cf9f4f077] - Do something
2024-04-19 17:47:57.389 [INFO ] [nhab.automation.script.ui.4cf9f4f077] - Do something