Stop rule execution

Hi,
I have need to stop rule execution.
In OpenHab 1.X I used the command:

return

with OpenHab 2, it raise an error in the logs.

Is there a way to do this in OpenHab 2?

Many Thanks

I usually structure my rules to avoid stoping execution of the rule like this so I don’t know for sure.

I have in the past tried to use a break statement and it exited out of my rule. You can try that and see if that does what you want.

If not you will probably have to restructure your rule.

Hi @rlkoshak,
break is not recognized by designer, but i’ll give it a try.
Is there something like GoTo supported?

Thanks

Not that I’m aware of. A really bad way to handle it would be to throw an exception. However, if return or break do not work the “correct” solution would be to restructure your rule to eliminate the need for this.

I am pretty sure I have previously used the following in OH2 without any errors in the log:

return false

I seems to remember some forum discussion on this, in particular the need for returning a (false) value to avoid an error, but I am not sure…

I suggest you give it a try.

1 Like

I do not undestand why “return” command is a bad pratice.
I would like to implement something like this:

rule "Watering"
    when
        Time cron "0 0 0/1 1/1 * ? *"   //starts every hour
    then
       if <current hour != hour set by user when watering garden should start> then
          return
       end

     < many lines of code that implement complicated watering algorithm..>

I can write it in such way:

rule "Watering"
    when
        Time cron "0 0 0/1 1/1 * ? *"   //starts every hour
    then
       if <current hour == hour set by user when watering garden should start> then
          < many lines of code that implement complicated watering algorithm>       
       end

But in my opinion it is worse for analysis / read.

Michal
http://blog.szymanskich.net

1 Like

Under normal circumstances, it wouldn’t be. However, there are some quirks with how the Rule’s DSL works and in my experience, using a return in Rules leads to unexpected and undesireable behavior. The same goes for the break command.

The problem I believe has to do with the fact that there is a whole bunch of stuff that takes place before your rule code executes and there is some stuff that takes place after your rule completes. But the return and break commands exits the whole thing so the code that is supposed to execute after your rule runs never gets to run. That is my theory at least based on the weird behavior I’ve seen in the past.