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.

@rlkoshak, I have a similar issue where I can’t seem to get the “conditions rule” to fail in a script. I’ve tried creating a script via dsl, javascript, and blockly per some of the suggested solutions. I have OH3.3 installed and eventually I want to do complex comparisons in the “but only if” section through scripts. For now, I’m just trying to understand how to do it with one comparison. I understand that the last line must be a boolean statement. To get started, I created a simple script and tested on scratchpad.

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID); ('Vacation Mode is currently set to: '+ itemRegistry.getItem('vacationMode').getState()); (itemRegistry.getItem('vacationMode').getState() == 'OFF');
itemRegistry.getItem('vacationMode').getState() == 'OFF';

I see in the log entry that I get a lowercase ‘false’ entry. However, when I copy this in the “but only if” script section, It neither fails or displays an entry in my log. I have no idea how to troubleshoot this.

I couldn’t get the “but only if” script section to fail when I clicked on “Run Now” button on the upper-right of the rule (located below code); no matter what scripting language I used. Today, I decided today to bypass the “Run Now” command and use an item to trigger and voila, it worked. So should the “Run Now” button evaluate the “but only if” section before running action?

The “Run Now” button does exactly what it says, runs the rule now. Conditions are not taken into consideration, the rule just runs. That’s by design. The only way to cause the conditions to run is to trigger the rule by an event, or call the rule from another rule with the argument to consider conditions set to true in the function call.

Is this simple script condition (in the “but only if” part) also possible in blockly?
If yes, how should it look like? Thanks!

Absolutely. The last block executed needs to evaluate to a boolean (true/false). There’s an example in the Getting Started Tutorial.