Google Spreadsheet Apps Script API Examples

A HUGE thank you to Gene in the forums here for pointing me straight. First off, I know just enough to do up these working examples. I’m very familiar with Google Sheets and Apps Scripts, but know very little about APIs and how to make these API calls to Rachio. After tons of Googling, I was able to get a response back from my Rachio and things went quick after that. I work best with examples and figured I’d post mine here so maybe someone else can benefit.

If you have a better way to do this or some more examples I haven’t done up yet, PLEASE add a comment.

This assumes you have a working knowledge of Google Apps Scripts. Simply paste these into a blank script and work your way down the list. You’ll need the info you get in the first few to run the ones at the bottom. To get the info that’s returned, go to View / Logs. Some of the info is kind of buried, but easy to pick it out.

function GetPersonID() {
var APIKey = “INSERT_YOUR_API_KEY”;
var url = “https://api.rach.io/1/public/person/info”;
var headers = {
“contentType”: “application/json”,
“headers”:{“Authorization”: "Bearer " + APIKey}
};

var response = UrlFetchApp.fetch(url, headers);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

function GetDeviceID() {
var APIKey = “INSERT_YOUR_API_KEY”;
var PersonID = “INSERT_YOUR_PERSON_ID”
var url = “https://api.rach.io/1/public/person/” + PersonID;
var headers = {
“contentType”: “application/json”,
“headers”:{“Authorization”: "Bearer " + APIKey}
};

var response = UrlFetchApp.fetch(url, headers);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

function GetZones() {
//HERE YOU’RE LOOKING FOR THE ZONE ID’S. IT SEEMED LIKE I ONLY GOT ZONES IN MY MAIN SCHEDULE?
var APIKey = “INSERT_YOUR_API_KEY”;
var DeviceID = “INSERT_YOUR_DEVICE_ID”
var url = “https://api.rach.io/1/public/device/”+DeviceID;
var headers = {
“contentType”: “application/json”,
“headers”:{“Authorization”: "Bearer " + APIKey}
};

var response = UrlFetchApp.fetch(url, headers);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

function ZoneInfo() {
//THIS WILL GIVE YOU MORE DETAILED INFO ABOUT THE ZONE AND CONFIRM WHAT ZONE EACH ID IS POINTED TO
var APIKey = “INSERT_YOUR_API_KEY”;
var ZoneID = “INSERT_A_ZONE_ID”
var url = “https://api.rach.io/1/public/zone/”+ZoneID;
var headers = {
“contentType”: “application/json”,
“headers”:{“Authorization”: "Bearer " + APIKey}
};

var response = UrlFetchApp.fetch(url, headers);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

function ZoneStart() {
var APIKey = “INSERT_YOUR_API_KEY”;
var ZoneID = “INSERT_A_ZONE_ID”
// Make a POST request with a JSON payload.
var data = {
‘id’: ZoneID,
‘duration’: 60 // value in seconds; required
};
var options = {
‘method’ : ‘put’,
‘contentType’: ‘application/json’,
‘headers’:{‘Authorization’: 'Bearer ’ + APIKey},
// Convert the JavaScript object to a JSON string.
‘payload’ : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(‘https://api.rach.io/1/public/zone/start’, options);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

function ZoneStartMultiple() {
//I WANTED THE ABILITY TO CYCLE SO I NEEDED TO CALL EXTRA ZONES THAT AREN’T ACTUALLY USED. I JUST SEND A DURATION OF 0 AND IT SEEMS TO WORK
var APIKey = “INSERT_YOUR_API_KEY”;
var ZoneID1 = “INSERT_A_ZONE_ID”
var ZoneID2 = “INSERT_A_ZONE_ID”
var ZoneID3 = “INSERT_A_ZONE_ID”
var ZoneID4 = “INSERT_A_ZONE_ID”
var ZoneID5 = “INSERT_A_ZONE_ID”
var ZoneID6 = “INSERT_A_ZONE_ID”
var ZoneID7 = “INSERT_A_ZONE_ID”
var ZoneID8 = “INSERT_A_ZONE_ID”
// Make a POST request with a JSON payload.
var data = { “zones” : [
{
‘id’: ZoneID1,
‘duration’: 30,
‘sortOrder’ : 0
},
{
‘id’: ZoneID2,
‘duration’: 0,
‘sortOrder’ : 1
},
{
‘id’: ZoneID3,
‘duration’: 0,
‘sortOrder’ : 2
},
{
‘id’: ZoneID4,
‘duration’: 0,
‘sortOrder’ : 3
},
{
‘id’: ZoneID5,
‘duration’: 0,
‘sortOrder’ : 4
},
{
‘id’: ZoneID6,
‘duration’: 0,
‘sortOrder’ : 5
},
{
‘id’: ZoneID7,
‘duration’: 0,
‘sortOrder’ : 6
},
{
‘id’: ZoneID8,
‘duration’: 0,
‘sortOrder’ : 7
},
{
‘id’: ZoneID1,
‘duration’: 0,
‘sortOrder’ : 9
},
{
‘id’: ZoneID2,
‘duration’: 0,
‘sortOrder’ : 10
},
{
‘id’: ZoneID3,
‘duration’: 0,
‘sortOrder’ : 11
},
{
‘id’: ZoneID4,
‘duration’: 0,
‘sortOrder’ : 12
},
{
‘id’: ZoneID5,
‘duration’: 0,
‘sortOrder’ : 13
},
{
‘id’: ZoneID6,
‘duration’: 0,
‘sortOrder’ : 14
},
{
‘id’: ZoneID7,
‘duration’: 0,
‘sortOrder’ : 15
},
{
‘id’: ZoneID8,
‘duration’: 0,
‘sortOrder’ : 16
}
]};
var options = {
‘method’ : ‘put’,
‘contentType’: ‘application/json’,
‘headers’:{‘Authorization’: 'Bearer ’ + APIKey},
// Convert the JavaScript object to a JSON string.
‘payload’ : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(‘https://api.rach.io/1/public/zone/start_multiple’, options);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

1 Like

Provide more information on why this script is useful and how you are using it. Thanks!

1 Like

Looks he is holding up a json payload that lets him trigger running one or multiple zones from Google sheets.

okay. why would he want to do that? I love Sheets and Scripts, but am not following on this one.

I did a google sheet to run the system as opposed to the built in logic. Too many limitations in the built in system.

I’m considering something similar to build a watering schedule that is time based and waters several times per day but for very short durations.

The use case is for reseeding my lawn. Maybe build out a schedule in sheets and execute it via Google apps engine.

I don’t know enough about apps engine to know yet. Until yesterday after dinner I had not written a single line of apps engine code.

I wrote this little hack (it’s a bit ugly and too hardcoded) to fetch some data from my two rachios

@mpcluever, may I suggest a couple of things:

  1. Put the script within a code block to make it easier to read, etc. To do this, put three backticks (`) on the line before and the line after the code. It makes it much nicer looking such as (@ParB, you might consider this too):
function GetPersonID() {
  var APIKey = “INSERT_YOUR_API_KEY”;
  var url = “https://api.rach.io/1/public/person/info”;
  var headers = {
    “contentType”: “application/json”,
    “headers”:{“Authorization”: "Bearer " + APIKey}
  };

  var response = UrlFetchApp.fetch(url, headers);
  var data = JSON.parse(response.getContentText());
  Logger.log(data);
}
  1. Instead of all the “INSERT_YOUR_API_KEY” all over, maybe use something like a global variable as outlined at the link below. This might be much less important as they are really examples of how to use App Script.
    https://stackoverflow.com/questions/24721226/how-to-define-global-variable-in-google-apps-script
1 Like

I struggled a bit with the forum reformatting my codeblock. I was looking for something like “< code>” or “< pre>” but didn’t find anything that resembled that. Thanks for the tip.