Rule script

OH3 on Rpi4

I want to set up a OH3 rule which fires on a Cron rule but then with a “But Only If” script. The documentation doesn’t seem to quite cater for this yet, only saying “enter a rule like it is described below in this article” i.e. as per OH2 written scripts. The GUI says I need to put in “a given scripts [which] evaluates to true”.

What is the syntax for this please? I have the following code as an expression I want it to evaluate:

if (FreyaPhone_Online.state == ON || HannahPhone_Online.state == ON)

However, when the rule is triggered I get the following error:

Script execution failed: if (FreyaPhone_Online.state == ON || HannahPhone_Online.state == ON ___ )
   no viable alternative at input '<EOF>'; line 1, column 67, length 1

if (condition) do something

You’re missing the code that you want to run if the condition is true.

EDIT - oh yes,ignore that, I misunderstood where you were putting this script. It is what the failure means, but you are not trying to do an inline if() at all here.

Create a Script Condition.

Choose Rules DSL.

And as the comment suggests, the script must evaluate to true. Put another way the last line of the script must be a boolean experssion.

FreyaPhone_Online.state == ON || HannahPhone_Online.state == ON

Ok. So I should just leave out the if(…) ?

Yes, if doesn’t evaluate to anything. The last line of the Script Condition must evaluate to a boolean.

To put it another way, in their simplest form rule Conditions are the condition part of an if statement.

1 Like

Many thanks again. That works now. I also notice that the “Run Now” button doesn’t evaluate the script. Is that expected behaviour?

Hi. Rikoshak has responded below and it’s now working. Yes, using a fully scripted rule I would have when this, do that. But in the case where I enter a rule through the GUI the only way of evaluating an OR condition is to add the boolean on its own. I now get what needs to be done. Thanks for responding.

You’ll have to look at the logs to see if something is going wrong. You also have to check your condition to make sure that it’s returning true. I believe that when you run the rule manually it will follow the condition. When you call a rule from another rule you have the option to tell it to ignore the condition.

I can confirm that Run Now does not run the script. At least if I deliberately set this up to evaluate as FALSE and use the Run Now button it still executes the action as if it were TRUE. The log doesn’t help unless I can somehow put a log command in the boolean condition.

It’s a Script Condition. You can put what ever you want in there. The only constraint is the last line of the script has to evaluate to a boolean.

Got it thanks. OK I have done that. The script is not being evaluated with the Run Now button.