Close

How to get CA ETo data with Electric Imp

luke-benoLuke Beno wrote 10/11/2015 at 04:27 • 2 min read • Like

Writing this quick post for @Reinier van der Lee and his #Vinduino, a wine grower's water saving project project.

He is starting to use Electric Imp to connect his Vineyard and one of the tasks it tor retrieve ETo data. I recommended Electric Imp because it's Agent is really good at doing REST API requests and parsing the results. IMO this would be much much more difficult and resource intensive in Arduino. So here we go.

Step 1) You must get your API key from CIMIS, so register at this page.

Step 2) Check your email and do as requested by CIMIS.

Step 3) Login to CIMIS at this page.

Step 4) click on the "Account" link in the upper right corner as shown below

Step 5) Scroll down to the "Application Key (optional)" Section. Follow instructions in image below.

Ok now you have a AppKey, here's the code for the Electric Imp Agent:

function whatWasYestedaysDateAgain() {
  // Get Epoch time in seconds
  local now_GMT = time()
  // Correct for Pacific Time Zone (UTC-8)
  local now_PST = now_GMT - (8*60*60)
  // Subtract 1 day
  local yesterday_PST = now_PST - (24*60*60)
  // Convert epoch to a date object
  local yday = date(yesterday_PST)
  // Extract the year, month and day from date object
  // Remember for some lame reason months are listed 0-11 so add 1
  return yday.year.tostring()+"-"+(yday.month+1).tostring()+"-"+yday.day.tostring()
}

function getETo(zip) {
  local yesterday = whatWasYestedaysDateAgain()
  local apiKey = "Paste AppKey Here"
  local url = "http://et.water.ca.gov/api/data?appKey="+apiKey+"&targets="+zip+"&startDate="+yesterday+"&endDate="+yesterday
  local request = http.get(url, {});
  return http.jsondecode(request.sendsync().body)["Data"]["Providers"][0]["Records"][0]["DayAsceEto"]["Value"].tofloat()
}

server.log(getETo("92592"))

Paste your AppKey into the second line of the "getETo" function and you are good to go, the getETo function will return a float value of yesterdays ETo for the specified zipcode.

More info on the REST API can be found here: http://et.water.ca.gov/Rest/Index

Like

Discussions

Reinier van der Lee wrote 10/11/2015 at 06:37 point

Wow, Luke, you didn't waste time on this one. Thanks a bunch!

  Are you sure? yes | no

Luke Beno wrote 10/11/2015 at 22:54 point

yeah I've discovered that if I don't do things while they are fresh in my mind, they may not get done!  Want to make sure that you have a good first experience with the imp too

  Are you sure? yes | no