Complex condition linking

conditions
advanced-apilio
Complex condition linking
0

#1

Apilio lets you combine your conditions in two different ways: using the simple (or AND) way or a more complex alternative that lets you combine AND and OR operators. These logic operators are akin to a connector that lets you combine different conditions in a smart way. By combining both operators in a complex link, you can create nested conditions that will make your automations much more sophisticated and customised to your needs.

:bulb: Remember that a condition in Apilio can only be true or false. If you want to know more about how these work, this article will help.

We will first see how these operators work, review the simple and complex condition and then give you some ideas for bringing your Apilio setups to the next level!

How the logic operators AND and OR work

Operator names, such as AND and OR are usually written capitalised.

How AND works

The AND operator will check if all of the conditions are true and, if they are, return true.

For example:

If I’m home AND my partner is home, then turn on the heater to comfort temperature.

  • If I’m home but my partner is not, the heater will not turn on to comfort temperature
  • The same will happen if my partner is home but I’m not.
  • Only if both of us are home (both those conditions are true), then the heater will be turned on to that temperature.

How OR works

The OR operator will check if any of the conditions are true and, in that case, return true.

If we continue with the previous example, maybe I want to turn on the heater to comfort temperature if either I’m home, or my partner is home, or if both of us are home. This is the perfect situation to use the OR operator! Our previous automation would now be the following:

If I’m home OR my partner is home, then turn on the heater to comfort temperature.

  • If I’m home but my partner is not, the heater will turn on to comfort temperature
  • The same will happen if my partner is home but I’m not.
  • And also if both of us are home (both those conditions are true), then the heater will be turned on to that temperature.

This is perfect for keeping both of us toasty warm!

How to implement simple and complex conditions in Apilio

In general, the syntax that Apilio uses is similar to the one used in Microsoft Excel products (you can read Microsoft Help articles for AND, OR).

Simple “AND” Condition Linking

Let’s imagine we have setup three conditions: to check if is nighttime, and if Abbie and Bob are, respectively, home. When I create a new logicblock I will see all the available conditions I can choose from, and I can select as condition linking either Simple or Complex.

Conditions are linked together in Logicblocks. In most scenarios, the easy “AND”-linking of conditions is the way to go – all conditions must be true in order to get a positive result. This can be translated into a sentence like:

IF it’s nighttime AND Bob is home AND Abbie is home THEN turn on the heater to comfort temperature

When all of these are true, then the actions for a positive result will run and we will switch on the lights. But this means that Bob will freeze when Abbie is not home at night as the heater will not switch on! And the same will happen when Abbie is alone. Fortunately, we can fix this with some condition linking.

Complex Condition Linking

As we have just seen, the simple AND might not be enough to create your, here’s when the complex condition becomes super useful!

With Apilio’s complex condition linking you can:

  • Use the OR operator instead of, or in combination with, the AND operator
  • Create nested expressions

For example, you could write:

  • AND(condition_1,condition_2,condition3). This is equivalent to the simple condition linking.
  • OR(condition_1,condition_2,condition3) will return true if any of the 3 conditions is true
  • AND(condition_1,OR(condition_2,condition_3)) will return true if condition 1 and either 2 or 3 (or both!) are true.

To improve the earlier example, you can automate a scenario like this:

IF it’s nighttime AND (Bob OR Abbie are home) THEN turn on the heater to comfort temperature

You can switch to Complex Condition Linking by selecting the option on any Logicblock. Here’s what it looks like in Apilio:

As you can see, our complex condition is using both AND and OR operators:

AND(it_is_nighttime,OR(is_bob_home,is_abbie_home))

Make sure that you check the box for any relevant condition you’d like to use from your listed conditions.

Apilio will parse your condition linking statement on save and will give you an error message in case it cannot match a condition name from your expression with a linked condition!

Example with complex nested conditions

This is a great example from one of our users:

OR(alarm_status_enabled,AND(OR(all_away,alarm_is_scheduled),alarm_status_not_disabled))

Let’s imagine that we have a logicblock that is our home security system, and that if evaluated positively, it will run a series of actions such as locking doors, rolling down blinds, turning on a radio, enabling a movement sensor in the backyard, and so on.

Let’s unpack what this nested condition does:

This logicblock will run my actions to activate all my home security systems either if my alarm is enabled or in the three following situations:

  • The whole family is away and the alarm is not disabled
  • The alarm is scheduled to be on and the alarm is not disabled
  • The whole family is away and the alarm is scheduled to be on and the alarm is not currently disabled

With this complex condition, Apilio takes care of enabling all the home security system so the family can be certain that even a forgetful member of the family leaves the house empty without enabling the alarm, the security systems will still be enabled.


Evaluating logicblocks and triggering actions with IFTTT
Conditions with multiple value for IFTTT
How to choose your variable, trigger and actions
Nested if condition
#2

Hello,
I don’t understand why my condition is refused :
AND(door_is_open,OR(AND(it_is_night_morning,it_is_morning),AND(it_is_night_evening,it_is_evening)))
this one works :
OR(AND(it_is_night_morning,it_is_morning),AND(it_is_night_evening,it_is_evening))

IFTT set a variable current_time (string) each hour, it looks like “October 31, 2019 at 06:01AM”
I have set conditions with this variable : it_is_mornig when current_time contains AM, it_is_night_morning when current time contains 00: OR 01: … OR 07:

I have found an other way to do that :
OR(AND(door_is_open,it_is_night_morning,it_is_morning),AND(door_is_open,it_is_night_evening,it_is_evening))
but i still don’t see my mistake …


#3

Hey Martin,
we discovered a bug in the parsing of the complex condition statements thanks to another user’s tip (Complex conditions).
So there’s a known issue, probably related to the number of levels of nested statements.
Theoretically, there shouldn’t be a limitation, but reality says there is a problem :-/


#4

The same happened to me just now. Worked around with multiple logic blocks with identical event triggers as Martin did, but it would be nice to be able to do more with one evaluation and not to have to worry about multiple triggers. If you want to share your complex evaluation code with the community, I’m sure we’d be happy to help.


#5

@Martin @thutch77 just wanted to let you know this has been resolved in yesterday’s release so it would be good if you could confirm things are working now. Thanks for bearing with us! DanT


#6

Hey all,
unfortunately I was sending the positive news a bit early. We did ship an updated that solved some reported cases, but @Martin’s example didn’t make it my automated tests and when trying it online, it still did not validate.
Sorry for that - I will continue to improve the code and report back.


#7

Hey there,
we deployed a new version today that fixes the issues reported. And while at it, we also increased the length restriction by 25% to 150 characters.
The fix turned out to require more time because we had to move away from parsing the string with a regular expression to a custom subroutine.
We are testing the parsing with over 20 complex condition strings automatically and hope we covered all corner cases. Please let us know if you discover any remaining issues.