Complex condition linking

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 work

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

How the logic operator 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 the logic operator 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 the logic operator XOR works

This has been implemented following Microsoftā€™s specification for XOR: https://support.microsoft.com/en-us/office/xor-function-1548d4c2-5e47-4f77-9a92-0533bba14f37

The operator XOR:

  • results in TRUE when the number of TRUE conditions is odd
  • results in FALSE when the number of TRUE conditions is even

So if you have three conditions (odd number) and all of them are true, XOR returns true. But if you only have two conditions (even number), then XOR returns false.

How the logic operator NOR works

NOR results in TRUE if all the conditions are false.

NOR(i_m_home, robin_is_home) returns true when nobody is at home.

NOR is just the opposite of the basic operator OR:

NOR() = NOT(OR())

How the logic operator NAND works

NAND results in TRUE if, and only if, at least one of the conditions is false.

NAND(watered_yesterday, rain_yesterday) will be true if either:

  • the plants were watered yesterday and it didnā€™t rain
  • it rained but the plantā€™s didnā€™t get watered

NAND is just the opposite of the basic operator AND:

NAND = NOT(AND())

But I really want the NOT operator

Good news is NOR and NAND work exactly like NOT() if only used with one element. So if you apply NOR to one condition, youā€™ll get the opposite, making these equivalent to NOT.

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, XOR).

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.

4 Likes

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 ā€¦

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 :-/

1 Like

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.

1 Like

@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

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.

1 Like

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.

1 Like