Exit rule with return false

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

What do you think has changed? Does return; (with semicolon) not work for you?

I think it’s not possible at least within JavaScript:

I can’t speak for RulesDSL since i’ve moved everything to JS with OH3

I’ve encountered this issue on OH3 today

if statements with a single return clause

if ( ... ) return

cause compile/load errors:

Void functions cannot return a value

if statements with a code block and return clause don’t cause compile/load/runtime errors

if ( ... ) {
   return
}

OH 3.1.0 (stable) rule:

var logger =
        Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Test");

        scriptExtension.importPreset("default");

        this.OPENHAB_CONF = (this.OPENHAB_CONF === undefined) ? java.lang.System.getProperty("openhab.conf") : this.OPENHAB_CONF; load(OPENHAB_CONF + "/automation/lib/javascript/personal/utils.js")

        var ZonedDateTime = (this.ZonedDateTime === undefined) ? Java.type("java.time.ZonedDateTime"): this.ZonedDateTime;

        load(OPENHAB_CONF + "/automation/lib/javascript/personal/utils.js")

        logger.info("Test rule part 1")

        var trigger = event.itemName

        if(trigger == "test_1"){
          return
        }

        logger.info("If test_2 is used, then only, we shall see this log")

Gives me:

2021-09-17 10:46:32.680 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test' failed: <eval>:9:2 Invalid return statement
  return;
  ^ in <eval> at line number 9 at column number 2

:sleepy: :thinking:

This is still true I think

Same error:

2021-09-17 11:12:47.021 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test' failed: <eval>:9:2 Invalid return statement
  return;
  ^ in <eval> at line number 9 at column number 2

Line in question in the rule now reads:

if(trigger == "test_1"){
    return;
}

Can we see your whole rule code? That doesn’t look like a DSL message.

EDIT - yes it’s a javascript rule I think. This thread is all about DSL, so it won’t help you, it’s a different language.

This thread is about javascript early return -

but no-one has found the answer yet.

My bad. I am indeed coding in JS

Another question about “return” from DSL rule.
I have every our trigger which should toggle the state of Datain00_ValueasSwitch.

if (Datain00_ValueasSwitch.state == OFF)
{
  Datain00_ValueasSwitch.postUpdate(ON);
  return;
}
if (Datain00_ValueasSwitch.state == ON)
{
  Datain00_ValueasSwitch.postUpdate(OFF);
  return;
}

but when rule run i see in the log (Datain00_ValueasSwitch had OFF state before rule ):

2021-10-18 10:09:54.437 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datain00_ValueasSwitch' changed from OFF to ON

2021-10-18 10:09:54.746 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datain00_ValueasSwitch' changed from ON to OFF

So the function “return” doesn’t work? Please help me to implement the toggle of Datain00_ValueasSwitch.
Thank you.

Wouldn’t it work to use a construct like:

if ( )
{
}
else if ()
{
}

Find out for yourself

logInfo("test", "my state is " + Datain00_ValueasSwitch.state.toString)
if (Datain00_ValueasSwitch.state == OFF)
{
  Datain00_ValueasSwitch.postUpdate(ON)
  logInfo("test", "returning now A"
  return;
  logInfo("test", "didn't work A"
}
if (Datain00_ValueasSwitch.state == ON)
{
  Datain00_ValueasSwitch.postUpdate(OFF);
  logInfo("test", "returning now B"
  return;
  logInfo("test", "didn't work B"
}
logInfo("test", "carrying on"

Wolfgang_S, rossko57 thank you for feedback.

  1. There are an script errors if i try to use line “logInfo(“test”, “didn’t work A”)” after line with “return” statement. I deleted these lines.
  2. Code
logInfo("test", "my state is " + Datain00_ValueasSwitch.state.toString);
if (Datain00_ValueasSwitch.state == OFF)
{
  logInfo("test", "returning now A");
  Datain00_ValueasSwitch.postUpdate(ON);
  return;
//  logInfo("test", "didn't work A");
}
else if (Datain00_ValueasSwitch.state == ON)
{
  logInfo("test", "returning now B");
  Datain00_ValueasSwitch.postUpdate(OFF);
  return;
//  logInfo("test", "didn't work B");
}
logInfo("test", "carrying on");

return in log:

2021-10-18 14:21:51.493 [INFO ] [org.openhab.core.model.script.test ] - my state is OFF
2021-10-18 14:21:51.498 [INFO ] [org.openhab.core.model.script.test ] - returning now A
2021-10-18 14:21:51.501 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datain00_ValueasSwitch' changed from OFF to ON
2021-10-18 14:21:52.424 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datain00_ValueasSwitch' changed from ON to OFF
  1. I changed postUpdate to sendCommand and it works fine:
2021-10-18 14:24:15.003 [INFO ] [org.openhab.core.model.script.test  ] - my state is OFF
2021-10-18 14:24:15.006 [INFO ] [org.openhab.core.model.script.test  ] - returning now A
2021-10-18 14:24:15.008 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Datain00_ValueasSwitch' received command ON
2021-10-18 14:24:15.011 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Datain00_ValueasSwitch' predicted to become ON
2021-10-18 14:24:15.015 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datain00_ValueasSwitch' changed from OFF to ON

Thanks again for your support !

1 Like

So your Item is linked to some external device, you were updating the Item.
Shortly afterwards the “real device” says nope, that is not my state, here is another update representing my real state.

That’s how it is supposed to work.

If you want to change an external device, send it a command.