-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweather_hubitat.php
283 lines (229 loc) · 9.42 KB
/
weather_hubitat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
// (c)2021/2023 by The Master lagmrs.com
//
// pull temp from hubitat
//
//
// you should already have maker api installed on the hub
// you will need a token the device no of the maker and the temp device
// consult maker api for token. No hub device driver is needed for this script
//
// This script replaces a perl and batch file in favor of PHP
//
//
// place in /etc/asterisk/local
// wget https://raw.githubusercontent.com/tmastersmart/gmrs_live/main/weather_hubitat.php
//
//
// The timezone in PHP is not setup properly in hamvoip
// so you need to set your timezone.
// // chrontab -e add the following for time on the hr between 6am and 11pm
// 00 7-23 * * * php /etc/asterisk/local/weather_hubitat.php >> /tmp/time.txt
define('TIMEZONE', 'America/Chicago');
date_default_timezone_set(TIMEZONE);
$zone = ltrim(exec('timedatectl | grep "Time zone"'));//testing get timezone from hub
$node="2955"; // Your node number
$level = 3 ;// 1 temp only 2=temp,cond 3= temp,cond,wind humi rain
$phpVersion= phpversion();
$ver= "v1.1";
$time= date('H:i');
$date = date('m-d-Y');
//$WBdatum = gmdate('m-d-Y [H:i:s]');
$datum = date('m-d-Y H:i:s');
// HUB SETTINGS
$hub="192.168.0.13";// Hubitat
$maker="282"; // Device # for the maker API
$token = "0ba48ca1-f585-41b7-bfd4-2f9b7a134ed5";// access token from maker API
$device ="662"; // device no for the temp sensor wet=rain
$zipcode="71432";$fc = "f";// accuweather.com forcast zipcode
$sitename = "Hubitat";
$time_zone = date_default_timezone_get();
print " =============================================
";
print " Hubitat local temp $ver
";
print " PHP Time zone:$time_zone PHP:$phpVersion
";
print " PI $zone
";
print " =============================================
";
// poll the hubs sensors
$datum = date('m-d-Y H:i:s');
print "$datum Polling HUB:$hub
";
$pos1="";$temp="";$cond1="";$cond2="";$cond3="";
$html = file_get_contents("http://$hub/apps/api/$maker/devices/$device?access_token=$token");
$pos1 = strpos($html, 'temperature');
if ($pos1){$test = substr($html, ($pos1),50);$Lpos = strpos($test, 'currentValue');$Rpos = strpos($test, 'dataType');$the_temp= substr($test, $Lpos+14,$Rpos-$Lpos-16);}
$pos1 = strpos($html, 'wet');$wet="";
if ($pos1){$test = substr($html, ($pos1),50);$Lpos = strpos($test, 'currentValue');$Rpos = strpos($test, 'dataType');$wet= substr($test, $Lpos+14,$Rpos-$Lpos-16);}
$file="/tmp/hubatat.xml";
if(file_exists($file)){unlink($file);}
$fileOUT = fopen($file,'w');flock ($fileOUT, LOCK_EX );fwrite ($fileOUT,$html);flock ($fileOUT, LOCK_UN );fclose ($fileOUT);
$file="/tmp/temperature.txt";
if(file_exists($file)){unlink($file);}
$fileOUT = fopen($file,'w');flock ($fileOUT, LOCK_EX );fwrite ($fileOUT,$the_temp);flock ($fileOUT, LOCK_UN );fclose ($fileOUT);
$datum = date('m-d-Y H:i:s');
print "$datum $sitename Temp:$the_temp
";
if ($level >1){
// Poll acuweather for the current conditions. Ignore the temp its not local
// http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${FAHRENHEIT}\&locCode\=$1
$file="/tmp/accuweather.xml"; $cond1="";$cond2="";$cond3="";
if(file_exists($file)){unlink($file);}
$html = file_get_contents("http://rss.accuweather.com/rss/liveweather_rss.asp?metric=$fc&locCode=$zipcode");
$fileOUT = fopen($file,'w');flock ($fileOUT, LOCK_EX );fwrite ($fileOUT,"$html");flock ($fileOUT, LOCK_UN );fclose ($fileOUT);
$fileIN= file($file);
foreach($fileIN as $line){
$line = str_replace("\r", "", $line);
$line = str_replace("\n", "", $line);
$pos1 = strpos($line, 'Currently');
$pos2 = strpos($line, ':');
$len = strlen($line);
if (!$cond1){
if ($pos1){
$test = substr($line, $pos2+2,40);
$pos2 = strpos($test, ':');
$temp = substr($test, 0, $pos2);
$datum = date('m-d-Y H:i:s');
$cond1=strtolower($temp);
continue ;
}
}
if (!$cond2){
if ($pos1){
$test = substr($line, $pos2+2,40);
$pos2 = strpos($test, ':');
$temp = substr($test, 0, $pos2);
$datum = date('m-d-Y H:i:s');
$cond2=strtolower($temp);
continue ;
}
}
if (!$cond3){
if ($pos1){
$test = substr($line, $pos2+2,40);
$pos2 = strpos($test, ':');
$temp = substr($test, 0, $pos2);
$datum = date('m-d-Y H:i:s');
$cond3=strtolower($temp);
continue ;
}
}
} // end loop
$file="/tmp/conditions.txt";$fileOUT = fopen($file,'w');flock ($fileOUT, LOCK_EX );fwrite ($fileOUT,"$the_temp F / $cond1 $cond2 $cond3");flock ($fileOUT, LOCK_UN );fclose ($fileOUT);
$vpath="/var/lib/asterisk/sounds";
$cond="/tmp/conditions.gsm";$file=$cond;
if(file_exists($file)){unlink($file);} // Mostly Cloudy
$fileOUT = fopen($file,'wb');flock ($fileOUT, LOCK_EX ); $cmd="";
$u = explode(" ","$cond1 ");
if ($cond1){
check_name ($u[0]);
check_name ($u[1]);
//check_name ($u[2]);
}
if ($cond2){
$u = explode(" ",$cond2);
check_name ($u[0]);
check_name ($u[1]);
}
if ($cond3){
$u = explode(" ",$cond3);
check_name ($u[0]);
check_name ($u[1]);
}
flock ($fileOUT, LOCK_UN );fclose ($fileOUT);
// End of condictions ==============================================================================================
$datum = date('m-d-Y H:i:s');
print "$datum conditions: ($cond1 $cond2 $cond3)
";
} // end level 2
// end conditions
$hour = date('H');
$day = date('l');
$hr = date('h');
$min = date('i');
$oh=false;make_number ($hr);$theHR = $file1; $theHR2 = $file2;
if ($min == 0 ){$theMin="$vpath/digits/oclock.gsm";$theMin2="";}
else {$oh=true;make_number ($min);$theMin = $file1;$theMin2=$file2;}
$datum = date('m-d-Y H:i:s');
print "$datum Local Time $hr:$min
";
$silence1 = "$vpath/silence/1.gsm";
$silence2 = "$vpath/silence/2.gsm";
$condition = "/tmp/condition.gsm";
$currentTime = "/tmp/current-time.gsm";
$file=$currentTime; $cmd="";
if(file_exists($file)){unlink($file);}
$fileOUT = fopen($file,'wb');flock ($fileOUT, LOCK_EX );
$fileIN = file_get_contents ($silence1);file_put_contents ($file,$fileIN, FILE_APPEND);$cmd="$cmd $silence2";
$status ="";
if ($hour < 12 ) {$status = "good-morning"; check_name ($status);}
if ($hour >= 12) {$status = "good-afternoon";check_name ($status);}
if ($hour >= 18) {$status = "good-evening"; ;check_name ($status);}
$datum = date('m-d-Y H:i:s');
check_name ("the-time-is");
$oh=false;make_number ($hr);$theHR = $file1; $theHR2 = $file2;
if($theHR){$fileIN = file_get_contents ($theHR);file_put_contents ($file,$fileIN, FILE_APPEND);$cmd="$cmd $theHR";}
if($theHR2){$fileIN = file_get_contents ($theHR2);file_put_contents ($file,$fileIN, FILE_APPEND);$cmd="$cmd $theHR2";}
if ($min == 0 ){check_name ("oclock");$theMin="";$theMin2="";}
else {$oh=true;make_number ($min);$theMin = $file1;$theMin2=$file2;
if ($theMin != ""){$fileIN = file_get_contents ($theMin);file_put_contents($file,$fileIN, FILE_APPEND); $cmd="$cmd $theMin"; }
if ($theMin2 != "") { $fileIN = file_get_contents ($theMin2);file_put_contents ($file,$fileIN, FILE_APPEND);$cmd="$cmd $theMin2";}
}
if ($hour < 12 ){$pm="am";$fileIN = file_get_contents ("$vpath/digits/a-m.gsm");file_put_contents ($file,$fileIN, FILE_APPEND);}
if ($hour >= 12){$pm="pm";$fileIN = file_get_contents ("$vpath/digits/p-m.gsm");file_put_contents ($file,$fileIN, FILE_APPEND);}
print "$datum $status Time is $hr:$min $pm
";
$fileIN = file_get_contents ($silence2);file_put_contents ($file,$fileIN, FILE_APPEND);
// Weather
check_name ("weather");
check_name ("conditions");
$fileIN = file_get_contents ($cond);file_put_contents ($file,$fileIN, FILE_APPEND);$cmd="$cmd $cond";
check_name ("temperature");
$oh=false;make_number ($the_temp);
if($file0){$fileIN = file_get_contents ($file0);file_put_contents($file,$fileIN, FILE_APPEND);}
if($file1){$fileIN = file_get_contents ($file1);file_put_contents ($file,$fileIN, FILE_APPEND);}
if($file2){$fileIN = file_get_contents ($file2);file_put_contents ($file,$fileIN, FILE_APPEND);}
if($file3){$fileIN = file_get_contents ($file3);file_put_contents ($file,$fileIN, FILE_APPEND);}
check_name ("degrees");
flock ($fileOUT, LOCK_UN );fclose ($fileOUT);
$datum = date('m-d-Y H:i:s');
print "$datum Playing file to NODE:$node $currentTime
";
$status= exec("sudo asterisk -rx 'rpt localplay $node /tmp/current-time'",$output,$return_var);
if(!$status){$status="OK";}
print "$datum finished $status $return_var
";
print " =============================================
";
function make_number ($in){
global $vpath,$file0,$file1,$file2,$file3,$negative,$oh;
// Speak all possible numbers
// PHP Number matrix
$file0 = "";$file1 = "";$file2 = "";$file3 = "";$negative="";
if ($in <0 ){$negative = "$vpath/digits/minus.gsm";}
$in = abs($in);
$in = round($in);
if ($oh){if ($in<10) { $file1 = "$vpath/digits/oh.gsm";}}
if ($in >= 100){ $file3 = "$vpath/digits/hundred.gsm"; $in = ($in -100); }
if ($in>=20 and $in<30 ){$file1 = "$vpath/digits/20.gsm";$in=$in-20;}
if ($in>=30 and $in<40 ){$file1 = "$vpath/digits/30.gsm";$in=$in-30;}
if ($in>=40 and $in<50 ){$file1 = "$vpath/digits/40.gsm";$in=$in-40;}
if ($in>=50 and $in<60 ){$file1 = "$vpath/digits/50.gsm";$in=$in-50;}
if ($in>=60 and $in<70 ){$file1 = "$vpath/digits/60.gsm";$in=$in-60;}
if ($in>=70 and $in<80 ){$file1 = "$vpath/digits/70.gsm";$in=$in-70;}
if ($in>=80 and $in<90 ){$file1 = "$vpath/digits/80.gsm";$in=$in-80;}
if ($in>=90 and $in<100 ){$file1 = "$vpath/digits/90.gsm";$in=$in-90;}
if ($in >=1 and $in<20 ){$file2 = "$vpath/digits/$in.gsm";}
}
function check_name ($in){
global $vpath,$file1,$file;
$file1="";
$fileSound= "$vpath/$in.gsm";
if (file_exists($fileSound)){
$fileIN = file_get_contents ($fileSound);file_put_contents ($file,$fileIN, FILE_APPEND);
}
}
?>