By @mhawksey

Using IFTTT with Google Apps Script for delayed THAT triggers and more

IFTTT is a great free service which hooks into a long list of other applications and devices (currently 570 in total), which lets you create basic ‘if this then that’ type recipes, called applets. For example I have applets for:

If new feed item from http://feeds.feedburner.com/MASHe, then add row to spreadsheet in my Google Drive

and

If current condition changes to snow, then post a tweet to @mhawksey

Sometimes IFTTT’s basic logic isn’t enough and recently I was trying to setup a ‘if this then that for x minutes’ or ‘if this then that after x minutes’. Thankfully IFTTT has a Webhooks service which can be used as a this or that, so in other words ‘if this url is called then …’ or ‘if this then call that url’. In this post I’m going to show you how you can use Google Apps Script with IFTTT to do more than this and that. As part of this I’m going to show you how I got my Wemo Smart Switch to turn on for a defined number of minutes via Google Assistant.

If you are not familiar with Google Apps Script this is part of the G Suite product family and lives in Google Drive. Like IFTTT Google Apps Script is a free service and provides easy ways to automate tasks across Google products and third party services.

For this project I’m using the Google Assistant service on IFTTT which allows you to create voice commands. As well as commands with a simple phrase you can also create triggers which take a number or text input. In this project I created a:

‘If I say “Switch on the garden lights for # minutes”, then make a web request’

Before we set this up we also need to create two IFTTT Webhook applets, one to turn the lights on the other to turn the lights off. These will be called by the Google Apps Script project we create.

Setting up the webhooks

Assuming you’ve already got an IFTTT account and connected the webhooks service you should be create an applet searching for the ‘webhooks’ service:

Using the ‘recieve a web request’ option enter an event name, in my project I used garden_lights_on and setup my Wemo switch for the garden lights turn on. Next create a similar webhook service with the event name garden_lights_off this time to turn the switch off.

To use these webhooks we need to get the service urls. The easiest way I found to do this was to visit the Webhooks settings page and copy the url on the page into a new browser tab:

This will show you the format the url request to trigger an event:

Setting up Google Apps Script

If you visit https://script.google.com you should be able to start a new script project. In your project copy/paste the following code replacing where prompted for the urls for triggers for turning the switch on and off:

function doGet(e) {
  var mins = parseInt(e.parameters.mins)
  UrlFetchApp.fetch('YOUR_IFTTT_TRIGGER_TO_TURN_LIGHTS_ON');
  // e.g. UrlFetchApp.fetch('https://maker.ifttt.com/trigger/garden_lights_on/with/key/pjz5ahl5chT7SWrn3YElO');
  ScriptApp.newTrigger('lightsOff')
           .timeBased()
           .after(mins*60*1000) // after(durationMilliseconds)
           .create();
}

function lightsOff(){
  UrlFetchApp.fetch('YOUR_IFTTT_TRIGGER_TO_TURN_LIGHTS_OFF');
  // e.g. UrlFetchApp.fetch('https://maker.ifttt.com/trigger/garden_lights_off/with/key/pjz5ahl5chT7SWrn3YElO');
}

In the code the doGet(e) function is how we setup a web app in Google Apps Script. The e.parameters lets us access querystring parameters, in this case we will extract a mins value as an integer. When the web app url for this script project is requested it in turn makes a HTTP request to our IFTTT webhook using the build-in UrlFetchApp service. Next the script sets up a time based trigger to run the other lightsOff() function, which is created to run after a set duration in milliseconds. In the lightsOff() function it again uses UrlFetchApp service to call our other IFTTT webhook to switch off the lights.

Before we can use this script we need to deploy the script as a web app. This can be done from the script editor window using the Publish > Deploy as web app. In the ‘Deploy as web app’ dialog enter a project version name and set the app to be accessed by ‘anyone, even anonymously’ and click ‘Deploy’ (more information on deploying as web apps).

When you click deploy you’ll be prompted for permissions for the script to run which will then give you a current web app URL.

Finally…

The final step is to use the deployed Google Apps Script web app URL in the Google Assistant applet in IFTTT. To do this create a new applet searching for the Google Assistant service. Using ‘Say a phrase with a number’ service enter your phrase, in my case ‘switch on the garden lights for # minutes’. For the that search for the webhook service and select ‘make a web request’. In the request URL use the Google Apps Script web app url created in the previous section adding ?mins={{NumberField}}, which lets us pass the number of minutes (mins) to our script.

So there you have it using Google Apps Script for time based triggers in IFTTT. Hopefully this will also inspire you to exploring how Google Apps Script can be used in your other applets.

Exit mobile version