My Solution to Cancel Irrigation if it has Rained Today

Posted on
Sat Apr 18, 2015 11:29 am
notsleepy offline
User avatar
Posts: 12
Joined: Nov 30, 2014

My Solution to Cancel Irrigation if it has Rained Today

Update: Just use the Wunderground plugin. Works perfectly. FACEPALM

This simple solution uses the free WUnderground API to cancel regularly scheduled irrigation (mine uses EZFlora) if either are true:
a. It's currently raining in my zip code
b. It has already rained more than 1/4 inch in my zip code today

  1. Register and signup for a free API key from Wunderground choosing the free 'Developer' plan: http://www.wunderground.com/weather/api/d/pricing.html
  2. Create a directory in the root of your hard drive:
    Code: Select all
    /ruby
  3. Copy the ruby code below, edit the config section noted by 'CHANGE THIS', and save the file to that newly created directory like so
    Code: Select all
    /ruby/weather.rb
  4. Add a cron entry to run that script every 5 minutes:
    Code: Select all
    */5 * * * * ruby /ruby/weather.rb
  5. Create the variables shown in the attached screenshot, and add the conditions shown to your irrigation schedule

I've tested it and both variables properly worked!

Note: the following can be used to add the cron entry from terminal:
Code: Select all
env EDITOR=nano crontab -e

then just paste the line from step 4 above.

_____________________________________________

Here is the ruby code:

Code: Select all
require 'net/http'
require 'logger'
require 'json'

########## CHANGE THESE ################
wunderground_key = 'ENTER YOUR KEY HERE'
indigo_username = 'ENTER YOUR INDIGO USERNAME HERE'
indigo_password = 'ENTER YOUR INDIGO PASSWORD HERE'
zipcode = "ENTER YOUR ZIP CODE HERE"
log_filepath = '/Users/tony/weather.log'  # You'll need to change /Users/tony to whatever is the directory for your Mac user
########################################
`touch #{log_filepath}`   # touch file if it doesn't exist
logger = Logger.new(log_filepath, 'monthly')

# get current conditions
conditions_url = "http://api.wunderground.com/api/#{wunderground_key}/geolookup/conditions/q/#{zipcode}.json"

url = URI.parse(conditions_url)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}

json_string = res.body
puts json_string
parsed_json = JSON.parse(json_string)


# retrieve data we want from parsed json
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
inches_of_rain_today = parsed_json['current_observation']['precip_today_in']
weather = parsed_json['current_observation']['weather'].downcase
currently_raining = 'FALSE'
if weather.include?("rain")
  currently_raining = "TRUE"
end

# update indigo variables
`curl --digest -u #{indigo_username}:#{indigo_password} -X PUT -d value=#{inches_of_rain_today} http://127.0.0.1:8176/variables/inches_rained_today`
`curl --digest -u #{indigo_username}:#{indigo_password} -X PUT -d value=#{currently_raining} http://127.0.0.1:8176/variables/currently_raining`

# log data
logstring = "Current temperature in #{location} is: #{temp_f} and it has rained #{inches_of_rain_today} today\n"
print logstring
logger.info(logstring)
Attachments
b.jpeg
b.jpeg (383.32 KiB) Viewed 4061 times
Last edited by notsleepy on Sat Apr 18, 2015 1:25 pm, edited 1 time in total.

Posted on
Sat Apr 18, 2015 12:01 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: My Solution to Cancel Irrigation if it has Rained Today

I can see you use Ruby scripts elsewhere but is there any reason you don't just use the Wunderground plugin for this?

Posted on
Sat Apr 18, 2015 12:14 pm
notsleepy offline
User avatar
Posts: 12
Joined: Nov 30, 2014

Re: My Solution to Cancel Irrigation if it has Rained Today

Reason: I didn't realize it was that easy. :shock:

Hahahhahaa! Next time I'll browse the plugins first.

Posted on
Sat Apr 18, 2015 12:23 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: My Solution to Cancel Irrigation if it has Rained Today

:-)

Posted on
Sat Apr 18, 2015 12:25 pm
notsleepy offline
User avatar
Posts: 12
Joined: Nov 30, 2014

Re: My Solution to Cancel Irrigation if it has Rained Today

Yes thats very nice actually. Ok carry on. :)
Attachments
12.jpeg
12.jpeg (72.89 KiB) Viewed 4001 times

Posted on
Sat Apr 18, 2015 12:27 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: My Solution to Cancel Irrigation if it has Rained Today

Maybe you WERE sleepy at the time you came up with it. ;-)

Posted on
Sat Apr 18, 2015 12:43 pm
notsleepy offline
User avatar
Posts: 12
Joined: Nov 30, 2014

Re: My Solution to Cancel Irrigation if it has Rained Today

I was very much awake but had a 1 year old screaming at me for much of the journey for the right solution

Posted on
Sat Apr 18, 2015 3:00 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: My Solution to Cancel Irrigation if it has Rained Today

Glad you're finding the plugin helpful!

Good luck,
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests

cron