By: Tony Stone                Email:aotomic1@gmail.com

Example of Nested IFTTT Logic:

Nest Temperature Control Based on the Location of Multiple Individuals

When I purchased a Nest thermostat I set it up to change the temperature if my iphone entered or exited an area. This worked fine until the house got a little too warm for my wife in the summer while I was away.  I needed to develop a means of automating temperature control based on one or both of us being in or out of the area.

After a lot of trial and error over Memorial Day weekend I was able to accomplish this using a Nest Thermostat, IFTTT, iOS Location, Gmail, and Google Sheets.  I am publishing this because I was not able to find anything else like it and it is a template I plan to reuse it for other recipes that will require nested IF statements.

Steps:

1. Set IFTTT up on my and my wife’s phones
2. I created IFTTT recipes on each of our phones to send an email to each of our email addresses (we both use gmail) when we enter or exit a location.  Example HERE.
3. Set my wife’s gmail up to filter those messages and forward them to my email.
4. In my gmail I set up  filter labels labels for each of our entering and exiting emails.  Hers are labeled “annaloc” and mine are labeled “tonyloc”
5. In my IFTTT account I set up two recipes to write to a single Google Sheet named “locationtrigger” - one to capture emails labeled “annaloc” and the second to capture emails labeled “tonyloc”.  Example HERE.

The resulting Google Sheet entries look like this and are in a tab named “data”:

1. I created a second tab in the spreadsheet named “state” to analyze the data.  It looks like this:

1. Cell A2 finds the last recorded status for Anna:

=INDEX(data!A1:D5000,(SUMPRODUCT(MAX((data!A:A="annaloc")*(ROW(data!A:A))))),4)

1. Cell B2 finds the last recorded status for Tony:

=INDEX(data!A1:D5000,(SUMPRODUCT(MAX((data!A:A="tonyloc")*(ROW(data!A:A))))),4)

1. Cell C2 counts the number of “entered” status states (# of people currently inside the area):

=COUNTIF(A2:B2,"*entered*")

1. Cell D2 translates Cell C2 into a simple “is anyone home - yes or no”:

=IF(C2>0,9999999999999,1111111111111)

(continued)

1. I wrote a Google Apps Script to send an email to trigger@recipe.ifttt.com and CC me if the value of cell D2 changes (Credit: The directions I followed are  HERE) .  The beauty of this script is it will only email me if the state changes from nobody at home to somebody at home or vice versa.  People can come and go, but if the state never changes I won’t get an email.
1. I clicked the menu Tools > Scripts > Script editor. Then entered the following code:

var value = ss.getSheetByName("state").getRange("D2").getValue();

if( value > 0 ) {

var last = PropertiesService.getScriptProperties().getProperty("last");

value = value.toString();

if( value != last ) {

MailApp.sendEmail('trigger@recipe.ifttt.com', 'Nest Home Status #'+value+'\n\n',

'1="not home", 9="is home": '+value+'\n\n', {

cc: 'tony.stone@gmail.com'

});

PropertiesService.getScriptProperties().setProperty("last", value)

}

}

}

1. Save it (menu File > Save) with any name. Then click the menu Triggers > Current script's triggers.
2. Click the blue link and set up the trigger to be time-driven and run every minute.

(NOTE: I tried setting up a onChange or onEdit trigger, but since IFTTT updates the spreadsheet via API those triggers won’t function.)

1. I set up two new IFTTT recipes to trigger when trigger@recipe.ifttt.com receives a new email with #9999999999999 or #1111111111111 in the subject.  For all 1’s I adjust the temperature for energy saving.  For all 9’s I adjust the temperature for comfort.

-The End

Notes:

• There is always a 7-15 minute delay between entering the zone and when the temperature changes take effect.  If the zone is sufficiently large enough this should not be a problem.  I will be looking into ways to speed up parts of the process. UPDATE:  Once my wife upgraded to the iPhone 6s from the 5s the delay reduced significantly - it is now no more than 2-3 minutes.  I have a iPhone 6 and experience a similar 2-3 minute delay.

• This method can be easily extended as long as steps 1-3 can be duplicated for each new person.

• My wife and I have different temperature tolerances - I can tolerate colder than her and she can tolerate warmer than me.  To drive greater efficiency I will be tweaking this to make it slightly cooler in the winter when I am the only one home and slightly warmer in the summer when she is the only one home.

Document Changes:

06/01/15:

First Published

11/27/15:

Updated Google script to replace deprecated function

Updated delay notes to highlight differences observed between iPhone 5s and iPhone 6s trigger lag for IFTTT