Skip to content

Instantly share code, notes, and snippets.

@krkeegan
Created August 5, 2014 22:37
Show Gist options
  • Save krkeegan/d2cdfa2988299b1fc300 to your computer and use it in GitHub Desktop.
Save krkeegan/d2cdfa2988299b1fc300 to your computer and use it in GitHub Desktop.
MisterHouse Weather Underground Sample Excerpt
# Category = Interfaces
#@ Adds weather items to the web interface
use XML::Simple;
$p_w_forecast = new Process_Item qq[get_url "http://api.wunderground.com/api/<API>/forecast/q/KVNY.xml" "$config_parms{data_dir}/web/forecast.html"];
$p_w_conditions = new Process_Item qq[get_url "http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCASHERM6" "$config_parms{data_dir}/web/conditions.html"];
$p_w_forecast_hourly_wu = new Process_Item qq[get_url "http://api.wunderground.com/api/<API>/hourly/q/KVNY.xml" "$config_parms{data_dir}/web/hourly_wu.html"];
if ($Reload){
&get_forecast;
&get_conditions;
}
sub get_forecast{
start $p_w_forecast;
}
sub get_hourly_wu{
start $p_w_forecast_hourly_wu;
}
if (done_now $p_w_forecast){
my $content = file_read("$config_parms{data_dir}/web/forecast.html");
$content =~ s/^\s+//; # Solves: Unable to recognise encoding warning
my $data = $xml->XMLin($content);
#loop through forecast Days
my $forecasts = $data->{forecast}->{simpleforecast}->{forecastdays}->{forecastday};
foreach my $f (@{$forecasts}){
my $high = $f->{high}->{fahrenheit};
my $low = $f->{low}->{fahrenheit};
my $conditions = $f->{conditions};
my $precip = $f->{pop};
my $date = $f->{date}->{weekday_short};
my $period = $f->{period};
my $item = "w_" . $period . "_forecast";
my $object = &get_object_by_name($item);
$object->set("$conditions $high/$low $precip\% Rain");
$object->{label} = $period ."-".$date;
}
}
if (done_now $p_w_forecast_hourly_wu || done_now $p_w_forecast_hourly_nws){
my $content = file_read("$config_parms{data_dir}/web/hourly_wu.html");
$content =~ s/^\s+//; # Solves: Unable to recognise encoding warning
my $data = $xml->XMLin($content);
#Intialize array of forecasts
my @forecast_array;
#loop through forecast hours
my $forecasts = $data->{hourly_forecast}->{forecast};
foreach my $f (@{$forecasts}){
my $hour = $f->{FCTTIME}->{hour}; #Use for inside MH
my $epoch = $f->{FCTTIME}->{epoch} * 1000; #use for posting to nimbits
my $temp = $f->{temp}->{english};
if ($epoch <= ((time *1000)+86400000)){ #only push 24 hours of data
push(@forecast_array, {"d"=>$temp, "t"=>$epoch});
$hourly_forecast{$hour} = $temp;
}
}
}
sub get_conditions{
start $p_w_conditions;
}
if (done_now $p_w_conditions){
#Get Condition Line Only
run_voice_cmd 'Get the Internet weather conditions';
my $content = file_read("$config_parms{data_dir}/web/conditions.html");
$content =~ s/^\s+//; # Solves: Unable to recognise encoding warning
my $data = $xml->XMLin($content);
$main::Weather{TempOutdoor} = $data->{temp_f};
$main::Weather{WindAvgDir} = $data->{wind_degrees};
$main::Weather{WindAvgSpeed} = $data->{wind_mph};
$main::Weather{WindGustSpeed} = $data->{wind_gust_mph};
$main::Weather{HumidOutdoor} = $data->{relative_humidity};
$main::Weather{RainRate} = $data->{precip_1hr_in};
$main::Weather{RainTotal} = $data->{precip_today_in};
$main::Weather{BaromSea} = $data->{pressure_in};
$main::Weather{DewOutdoor} = $data->{dewpoint_f};
$main::Weather{Conditions} = ' ';
Weather_Common::weather_updated;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment