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
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.
- 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.
- 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
- 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 !
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.