Created
March 23, 2018 14:17
-
-
Save Radiotechniman/019be9126ab1af1f2bcaba3b6023fc93 to your computer and use it in GitHub Desktop.
Simple php script to be used with homey which allows it to do something when it rains or will rain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//set the mm of rain you consider it's raining | |
$rainthreshold_minutes = "0.1" ; | |
$rainthreshold_hours = "0.5" ; | |
// according to buienradar there are 5 levels of rain per hour (mm/h) | |
// 0-2 | |
// 2-5 | |
// 5-10 | |
// 10-100 | |
// 100+ | |
// these are used in the "severity level" | |
/// | |
//change below if you know what you are doing | |
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); | |
header("Cache-Control: post-check=0, pre-check=0", false); | |
header("Pragma: no-cache"); | |
if(!isset($_GET['lat'])){ | |
$lat = "51.244418" ; | |
}else{ | |
$lat = $_GET['lat'] ; | |
} | |
if(!isset($_GET['lon'])){ | |
$lon = "-5.235900" ; | |
}else{ | |
$lon = $_GET['lon'] ; | |
} | |
echo '{' ; | |
echo '"location" : {' ; | |
echo '"lat" : "'.$lat.'", ' ; | |
echo '"lon" : "'.$lon.'", '; | |
echo '"http" : "https://www.buienradar.nl/nederland/neerslag/zoom/3uurs?lat='.$lat.'&lon='.$lon.'" },' ; | |
$json = file_get_contents("https://gpsgadget.buienradar.nl/data/raintext?lat=".$lat."&lon=".$lon) ; | |
$json = urlencode($json) ; | |
$data = explode("%0D%0A",$json) ; | |
$thishour=$nexthour=$ever = 0; | |
$rainbyminutes = $bool_rainbyminutes = '' ; | |
$first_rain = "ik heb geen idee" ; | |
$rain_stops = "ik heb geen idee" ; | |
$bool_rainnow = $bool_rainthis_hour = $bool_rainnext_hour = 'false' ; | |
$rain = array(); | |
for($i=0;$i<(count($data)-1);$i++){ | |
$tmp = urldecode($data[$i]) ; | |
$tmp2 = explode("|",$tmp) ; | |
$neerslg = neerslag($tmp2[0]); | |
$bool_neerslg = "false" ; | |
// check if it will rain now | |
if($i==0){ | |
if($neerslg>=$rainthreshold_minutes){ | |
$bool_rainnow = 'true' ; | |
$bool_neerslg = 'true' ; | |
$first_rain = 'het regent nu.' ; } | |
} | |
else{ | |
if($neerslg>=$rainthreshold_minutes){ | |
$bool_neerslg = 'true' ; | |
if($first_rain == "ik heb geen idee"){ | |
$first_rain = 'het gaat over '.($i*5).' minuten regenen.' ; | |
} | |
} | |
} | |
$rain[] = $neerslg ; | |
if($i<12){ //add the items 0 to 11 together to calculate the total rain for this hour | |
$thishour+= ($neerslg*10)/10 ; | |
$ever += ($neerslg*10)/10 ;} | |
else{ //same for the rest to calculate next hour | |
$nexthour+= ($neerslg*10)/10 ; | |
$ever += ($neerslg*10)/10 ; } | |
$rainbyminutes .= '"'.($i*5).'" : "'.$neerslg.'",' ; | |
$bool_rainbyminutes .= '"'.($i*5).'" : '.$bool_neerslg.',' ; | |
} | |
$rainbyminutes = rtrim($rainbyminutes, ",") ; | |
$bool_rainbyminutes = rtrim($bool_rainbyminutes, ",") ; | |
if($thishour>=$rainthreshold_hours){ | |
$bool_rainthis_hour = 'true' ; | |
} | |
if($nexthour>=$rainthreshold_hours){ | |
$bool_rainnext_hour = 'true' ; | |
} | |
// print_r($rain) ; | |
//find when it will be dry, first find first occurence of rain | |
$regen_time = '' ; | |
if($ever>0){ | |
for($i=0;$i<count($rain);$i++){ | |
$regen = $rain[$i]; | |
// if found stop | |
if($regen>0.0){ | |
$regen_time = $i ; | |
break; } | |
} | |
//now find the first occurence after the above one | |
for($i=$regen_time;$i<count($rain);$i++){ | |
$regen_stop = $rain[$i]; | |
if($regen_stop==0.0){ | |
$rain_stops = "het stopt over ".($i*5)." minuten met regenen." ; | |
break; } | |
else{ | |
$rain_stops = "het duurt minimaal ".($i*5)." minuten, maar dit kan veranderen." ;} | |
} | |
} | |
echo '"bool" : {' ; | |
echo '"rainnow" : '.$bool_rainnow.','; | |
echo '"rainthis_hour" : '.$bool_rainthis_hour.','; | |
echo '"rainnext_hour" : '.$bool_rainnext_hour.','; | |
echo '"rainbyminutes" : {' ; | |
echo ''.$bool_rainbyminutes.'}'; | |
echo '},' ; | |
echo '"rainby" : {' ; | |
echo '"this_hour" : "'.$thishour.'", '; | |
echo '"next_hour" : "'.$nexthour.'", '; | |
echo '"minutes" : {' ; | |
echo ''.$rainbyminutes.'}' ; | |
echo '},' ; | |
echo '"severitylevel" : { ' ; | |
echo '"this_hour" : "'.calc_rainlevel($thishour).'", '; | |
echo '"next_hour" : "'.calc_rainlevel($nexthour).'" '; | |
echo '},' ; | |
echo '"homeyquestions" : { ' ; | |
echo '"whenstart" : "'.$first_rain.'", '; | |
echo '"whenstop" : "'.$rain_stops.'" '; | |
//echo '"whenstop_heavy" : "ik heb geen idee", '; | |
//echo '"whendry_30" : "ik heb geen idee", '; | |
//echo '"whenheavyrain" : "ik heb geen idee" '; | |
echo '}}' ; | |
function neerslag($input){ | |
$tmp = (($input-109)/32) ; | |
$tmp = pow(10, $tmp) ; | |
$tmp = round($tmp,1) ; | |
if($tmp == "0" ){ | |
return "0.0" ; } | |
else{ | |
return $tmp; | |
} | |
} | |
function calc_rainlevel($input){ | |
if(($input>=0 && $input< 2 )){ | |
return 0 ; } | |
elseif(($input>=2 && $input< 5 )) { | |
return 1 ; } | |
elseif(($input>=5 && $input< 10 )) { | |
return 2 ; } | |
elseif(($input>=10 && $input<= 100 )) { | |
return 3 ; } | |
else{ | |
return 4 ; } | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment