Skip to content

Instantly share code, notes, and snippets.

@walquis
Created September 20, 2020 00:22
Show Gist options
  • Save walquis/c0c1b97c66c56f2d2390b47a0446b751 to your computer and use it in GitHub Desktop.
Save walquis/c0c1b97c66c56f2d2390b47a0446b751 to your computer and use it in GitHub Desktop.
Simple open_weather implementation - used to collect temperature data for Nest stats website until sep 2019 when Lennox system installed
require 'httparty'
def outside_temp_f zip
# Only read the temp every ten minutes, or if no current-temperature-file established.
#
curr_temperature_fname = 'current-temperature-file'
if File.exists? curr_temperature_fname
age_in_seconds = DateTime.now.to_time.to_i - File.stat(curr_temperature_fname).mtime.to_time.to_i
# Is file less than ten minutes old?
if age_in_seconds < 600
return File.read(curr_temperature_fname).strip.to_f
end
end
uri = 'http://api.openweathermap.org/data/2.5/weather'
appid = ENV['OPEN_WEATHER_APPID']
response = HTTParty.get "#{uri}?APPID=#{appid}&zip=#{zip}"
# Kelvin to Fahrenheit ...
temp_f = (JSON.parse(response.body)['main']['temp'] * 1.8) - 459.67
File.open(curr_temperature_fname, 'w') do |f|
puts "#{DateTime.now} - initializing or updating current-temperature-file with '#{temp_f}'..."
f.write temp_f.to_s
end
temp_f.to_f
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment