Exit rule with return false

Are we talking about the same thing? I’m looking for the issue ticket you’ve opened at https://github.com/eclipse/smarthome/issues -> could you please link it here? I want to stay informed about the progress.

https://github.com/eclipse/smarthome/issues/3831

Here we are…

Ah, already got closed, there’s the reason I didn’t find it :slight_smile:

Hmm.
Just installed the latest Snapshot some minutes ago.
The expression
return
doesn´t stop the execution of the code.
The issue is closed, but maybe the solution wasn´t tested?

I think that the Eclipse Smarthome core gets updated every few days, so the fix that was committed will reach the openHAB 2.2.0 Snapshot release soon (but it’s not here yet).

The latest ESH distribution was build a few hours ago (after a gap of 20 days :slightly_smiling_face: )… so the next OH 2.2 snapshot should have it included.

edit: I see status “aborted” on build #213 :frowning:

https://hudson.eclipse.org/smarthome/job/SmartHomeDistribution-Stable/

Link above thankx to @sihui ! (by the way… several fixes, including the astro scheduler thingy 3811 should be coming soon to a snapshot theater near you!)

Ps: I think that the issue gets closed the moment the PR is merged. They don’t wait for the fix to reach the users :slight_smile:

1 Like

Hi,

after some updates I must confirm, that the change doesn´t work.
I use build #989 and the command
return
doesn´t exit the execution of a rule.

You need to contact @sjka in the GitHub issue, I’m not sure if he will notice your comment here.

Your are right :wink:
Added a comment to the referencing issue log in GitHub.

To clarify this here: the fix made it into #989. And it indeed works.

However, you got to make sure to write return; (i.e. with a semicolon), as otherwise the next line would still be executed and used as a (meaningless) return value.

That is a subtle and important distinction. Nowhere else in the Rules DSL is a semicolon required.

@ThomDietrich, should this be documented in the Rules docs?

Nowhere else in the Rules DSL is a semicolon required.

Well, technically it is not required. And if it is followed by a block end (i.e. }), then it really makes no difference.

It’s just needed because of the optional return value the Java/Xbase language is ambiguous. See the explanation here: RULES: Error aborting execution rule with return · Issue #3831 · eclipse-archived/smarthome · GitHub

Nevertheless, I think this should be documented - I already created added documentation of early returns by sjsf · Pull Request #457 · openhab/openhab-docs · GitHub for this.

1 Like

What’s your opinion on the alternative? https://github.com/eclipse/smarthome/issues/3831#issuecomment-318030730

The semicolon feels like a very unintuitive workaround and while something like this needs documentation, I’d rather not have it in the first place…

1 Like

Sorry stepped into the same problem when using the old

return false

as I read here

return;

should work… I haven’t tested that. but get no warnings or errors anymore.

but I have to ask if there is any further progression on this, or do I still have to use the semicolon workaround ??

thanks in advance

You still have to use the semicolon.

1 Like

Thanks Rich!

Hi All, If i want to check if something is already OFF and just skip over it and continue to run the rule, is the return; the correct syntax after an IF check?

No because return quits the rule

1 Like

Thanks!

break might do what you are after but typically the best approach would be just to use if/else.

if(MyItem.state != OFF){
    // do stuff
}

or

if(MyItem.state == OFF){
    // don't do stuff
}
else {
    // do stuff
}

Using break is typically done with a while loop or something else that loops to break out of the loop. I think it works with if statements though. Something like

if(MyItem.state == OFF){
    break
}

But that construct is essentially a noop. It doesn’t actually do anything.

1 Like

What is the latest on this subject for OH3.0.1?

I want to prematurely exit a rule without log error such as:

if (myCondition) break   // this appears the legal method?
or
if (myCondition) break;  // this looks like a kludge
or
if (myCondition) return