Check "But only if" conditions with OR?


is it possible to have openHAB check two conditions in a rule with OR? As far as I understood they are checked with AND per standard, but I don’t see a way to change that. Shouldn’t an option to switch between AND and OR be very benificial to many rules? I guess there are workarounds though…

You can do that with a script condition, using ECMA, DSL, or Blockly.

I believe that the developers kept the base rules simple, under the theory that it’s better to use scripts for complex conditions. This preserves the simple, beginner-friendly interface while enabling power users to do whatever they want with conditions.

1 Like

Thanks, I will try that!

I added a DSL rule/script, which might not be optimal, but seems to work:

((now.getHour() * 60 + now.getMinute()) > 345 && (now.getHour() * 60 + now.getMinute()) < 540) || ((now.getHour() * 60 + now.getMinute()) > 1050 && (now.getHour() * 60 + now.getMinute()) < 1320)

Might be better to add a variable for (now.getHour() * 60 + now.getMinute(), but not sure if that’s possible in those scripts.

As far as I’m aware, anything you can put between the then and end of a DSL rule can go into a script. OH just treats it like DSL. If you think it’s better, try it and find out. Worst-case scenario is that you revert back to this code.

I’m not much of a programmer, so I try to keep my if conditions short and easy for me to comprehend in the future. As such, I generally use unbound items as conditional checks. In your case, I would have an item with cron-trigger rules that turn it on and off at the prescribed times, and then I’d just check if the proxy item is ON.

I use this in most of my rules, which start by checking if my “System mode” proxy item is set to home/away/sleeping/etc. to determine if they should take action.