OH3 rule conditions AND | OR

Hey Guys,
i playing around with some rules on the new OH3 gui.
I would like to achieve the following if all 3 conditions are fulfilled the rule should be executed… currently only one condition is needed to execute the rule.
Is it possible to link the conditions with an AND?

conditions:

  • inputs: {}
    id: “6”
    configuration:
    itemName: C_EG_simum
    state: ON
    operator: “!=”
    type: core.ItemStateCondition
  • inputs: {}
    id: “5”
    configuration:
    itemName: SimonHandy_Online
    state: OFF
    operator: =
    type: core.ItemStateCondition
  • inputs: {}
    id: “4”
    configuration:
    itemName: AnniHandy_Online
    state: OFF
    operator: =
    type: core.ItemStateCondition

Thanks
Simon

To my understanding, and I am happy to be corrected, the conditions are always OR, hence to achieve 3 conditions with AND you would need to write a script and include them in there with &&.

No. Conditions refer to events, not states, and there’s never two events at the same time.
Test for states inside the rule, return if they do not match.

If “executed” as in “triggered”, Markus is correct, triggering conditions can only be check with OR. However you did show the conditions part of your rule (known as the “But only if” part), these condtions are checked with AND!
So the question arises why are your conditions not linked with an AND?

thx, so they should be linked with an AND, so i think a Test rule may hit. i would Test it without.

I have made a test-rule with 3 conditions and the rule runs only if all of them are met!
Creating a log-entry for each checked state in the action part of the rule will give some hints!

1 Like

yes, right it was a Script causing the issue.
i was testing variables to check the Status of an item.

Hello, i’ve a question about rules. From UI i would like to make a rule to:

When
BathRollershutter changed
BedroomRollershutter changed
KitchenRollershutter changed

Then
Update morningshutters_switch to OFF

But only if
BathRollershutter state != 100
BedroomRollershutter state != 100
KitchenRollershutter state != 100

The point is that i want OR in “But only if” when if just one state change (or all of them).
Let’s suppose that kitchen is at 90% and others are at 100%, and i move one of them to something different from 90% (for kitchen) or 100% (for bedroom or bathroom).

Is it possible? How can be created a rule for what i need?

Hi, I have the same issue, or in rule condition and and in but only condition. That was possibel in OH2.5 rules.

any clue ?

IMHO rules in OH2 had no explizit “but only if” section, you had to write such as an if statement in the Code (“then”) section. Doing it in the code section is possible in OH3 as well.

The IF conditions in UI rules are intentionally limited to AND to keep things simple. As @opus noted, you can make more complex rules by choosing “Run Script” as the action, which enables you to use Blockly, JavaScript, or Rules DSL.

Or you can just keep your rules in text files, same as OH2.

1 Like

It existed, but as is the case in OH 3, it only existed in UI created rules.

@KenkiCK if what you mean by “the same issue” is that you have multiple conditions where if any one of which is true the rule can run you will need to code it in a Script Condition.

To be clear how this all works.

Triggers always use OR. If this event or that event occurs run the rule.

Conditions are always AND. All of the conditions listed in the But Only If section must evaluate to true for the rule to be allowed to run.

However, one of the conditions you can apply is a Script Condition. This lets you write arbitrary code that needs to either evaluate to true or false. For more complicated cases such as defining OR conditions you need to use a Script Condition.

Note that a Script Condition is different from the Script Action (i.e. stuff that goes in the “Then” section of the rule). The last line executed in a Script Condition must evaluate to true.

For example, if you only want the rule to run when the new state is not NULL or UNDEF create a Script Condition with the following line:

event.newState.class != UnDefType.class;

That will evaluate to true or false. Another common one is only run the rule if the previous state was not NULL or UNDEF.

event.previousState.class != UnDefType.class;

This second example isn’t even possible except as a Script Condition.

1 Like

Thanks for clarifying, Rich. I meant to write “as the condition”, not “the action”. Though I suppose you can also just script IF conditions and case statements into the rule action.

True but there are some differences. For example, when one calls a rule from another rule, there is an option to ignore the conditions and run the rule anyway or to take the conditions into account. You can’t easily do that with if statements.

Rules in the UI very much lend themselves to being broken up into rules that call other rules. For example, my Alarm Clock Rule template. All it does is handle scheduling another rule to be called based on the state of a DateTime Item. But then you, as the user only need to worry about coding that rule that does something at the scheduled time. In this case, I set the flag to follow the called rule’s conditions so you can add conditions to the called rule to prevent it from running even though the Alarm Clock Rule template called it based on the DateTime Item.

So, for example you could create a DateTime Item and populate it with a DateTime to start. Tag it with the “totd” tag. Then install the To Today rule template and create a rule from that which will move that DateTime Item’s to today’s date. Then create a rule that does something you want at that DateTime. But you only want it to happen on weekdays so add a Condition that only evaluates to true on weekdays based on ephemeris. Now install and create a rule from the Alarm Clock Rule instance to use the DateTime Item as the trigger.

The only code you’ve had to write is the what happens when the “alarm” goes off. The condition lets you control when the alarm goes off regardless of the state of the DateTime Item. Everything else was done for you. All you had to do was install the template and instantiate and configure the rule with the Item to use.

I’ve several rule templates that work on this general model. For example there is a template that looks at a Group of Items and calls another rule with the list of those Items that match a given condition (e.g. >= threshold). So all you as the user needs to do is focus on what to do when one or more of the Items matches the conditions. Actually detecting that case is handled for you. Here too, you can set a condition to further refine when the rule can run (e.g. only run if someone is home).

Conditions are a very powerful way to control and manage when rules execute, especially in cases where one installed a rule template that calls another one of your rule but one wants to take into account other stuff too that isn’t a part of the rule template.

1 Like

Thanks @rlkoshak , I used now scripts and it works really fine.