-
Notifications
You must be signed in to change notification settings - Fork 0
/
AC.sh
executable file
·303 lines (256 loc) · 7.98 KB
/
AC.sh
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
#host=192.168.114.200
subnet=6
host=192.168.$subnet.1
sl=10
temp=30
if [ ! -z "$1" ]
then
temp=$1
fi
DEFAULT="\033[0m"
BOLD="\033[1m"
DIM="\033[2m"
UNDERLINE="\033[4m"
BLINK="\033[5m"
REVERSE="\033[7m"
HIDDEN="\033[8m"
BOLD_C="\033[22m"
UNDERLINE_C="\033[24m"
BLINK_C="\033[25m"
REVERSE_C="\033[27m"
COLOR_C="\033[29m"
BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
BLACK_B="\033[40m"
RED_B="\033[41m"
GREEN_B="\033[42m"
YELLOW_B="\033[43m"
BLUE_B="\033[44m"
MAGENTA_B="\033[45m"
CYAN_B="\033[46m"
WHITE_B="\033[47m"
echo -e $GREEN"Will keep T=$temp °C$DEFAULT"
isnum() { awk -v a="$1" 'BEGIN {print (a == a + 0)}'; }
#echo test of number $(isnum "23.6")
get_temperature_from_wttrin() {
curl --max-time 15 wttr.in | head -4 | tail -1 > t
bytes=`stat -c %s t`
if [ $bytes -gt 0 ]
then
local current_temperature=$(more t | sed 's/\x1B\[[0-9;]\+[A-Za-z]//g' | cut -f3 -d"." | tr -d " " | cut -f1 -d"(" ) #cut -c1-2)
echo "Going from weather: $current_temperature"
wttrin_temperature=$current_temperature
fi
}
get_temperature_from_zigbee() {
# try to get tempearture from HomeAssistant Logs
local filetemperature="/home/homeassistant/.homeassistant/temperature_desk.csv"
local lastlog=`stat -c %Y $filetemperature`
local now=`date +%s`
let age=$now-$lastlog
echo "File age is $age seconds"
if [ $age -lt 2400 ] #40 minutes
then
local current_temperature=$(tail -1 /home/homeassistant/.homeassistant/temperature_desk.csv | cut -f2 -d",")
echo "Temperature from $filetemperature : $current_temperature"
else
echo "file is too old (age grater than 2400 seconds)"
fi
#outout
local res=$(isnum "$current_temperature")
if [ "$res" == "1" ] ; then
echo -e $current_temperature 'is a number (Zigbee)'
zigbee_temperature=$current_temperature
else
echoi -e $current_temperature 'is not a number (Zigbee)'
fi
}
get_temperature_from_callup() {
#trying to read call-up log
local lastlog=`stat -c %Y /home/pi/temperature.log`
local now=`date +%s`
let age=$now-$lastlog
echo "File age is $age seconds"
if [ $age -lt 2400 ] #40 minutes
then
local current_temperature=`less /home/pi/temperature.log | grep Appending | tail -1 | cut -f2 -d":" | cut -f2 -d","` # | cut -f1 -d"."`
echo "Temperature from callup: $current_temperature ($age seconds ago)"
fi
local res=$(isnum "$current_temperature")
if [ "$res" == "1" ] ; then
echo $current_temperature 'is a number (Call-up)'
callup_temperature=$current_temperature
else
echo $current_temperature 'is not a number (Call-up)'
fi
}
get_temperature_from_wifi() {
echo "try to get temperature from WLAN direct connection to the ESP8266"
~/rasp_scripts/connect_wlanX.sh -i wlan0 -s $subnet
# -q quiet
# -c nb of pings to perform
ping -q -c5 $host > /dev/null
if [ "$?" -eq "0" ]
then
echo "Host is reachable, sleeping $sl"
sleep $sl
local url="$host"/last_temp.csv
echo "URL $url"
wget -d --tries=4 --read-timeout=3 -O "last" "$host"/last_temp.csv
#curl -L -C - -o "last" $url #"$host"/last_temp.csv
#lynx -source $url > last
local current_temperature=$(cat last | cut -f2 -d"," | cut -f1 -d".")
echo "Temperature from $host : $current_temperature"
wifi_temperature=$current_temperature
fi
}
if [ -n "$debug" ]; then
get_temperature_from_wttrin
if [ -n "$wttrin_temperature" ]; then
echo -e $GREEN "wttr.in available: "$wttrin_temperature $DEFAULT
else
echo -e $RED "wttr.in not available" $DEFAULT
fi
fi
if [ -n "$debug" ]; then
get_temperature_from_callup
if [ -n "$callup_temperature" ]; then
echo -e $GREEN "Callup available: "$callup_temperature $DEFAULT
else
echo -e $RED "Callup not available" $DEFAULT
fi
fi
if [ -n "$debug" ]; then
get_temperature_from_zigbee
if [ -n "$zigbee_temperature" ]; then
echo -e $GREEN "Zigbee available: "$zigbee_temperature $DEFAULT
else
echo -e $RED "Zigbee not available" $DEFAULT
fi
fi
if [ -n "$debug" ]; then
#get_temperature_from_wifi
if [ -n "$wifi_temperature" ]; then
echo -e $GREEN "Wifi available: "$wifi_temperature $DEFAULT
else
echo -e $RED "Wifi not available" $DEFAULT
fi
fi
while [ 1 -ge 0 ]
do
get_temperature_from_zigbee
if [ -n "$zigbee_temperature" ]; then
echo -e $GREEN "Zigbee available: "$zigbee_temperature $DEFAULT
reading=$zigbee_temperature
else
echo -e $RED "Zigbee not available" $DEFAULT
get_temperature_from_callup
if [ -n "$callup_temperature" ]; then
echo -e $GREEN "Callup available: "$callup_temperature $DEFAULT
reading=$callup_temperature
else
echo -e $RED "Callup not available" $DEFAULT
get_temperature_from_wttrin
if [ -n "$wttrin_temperature" ]; then
echo -e $GREEN "wttr.in available: "$wttrin_temperature $DEFAULT
reading=$wttrin_temperature
else
echo -e $RED "wttr.in not available" $DEFAULT
echo "No reading available ... assuming temperature is 30 degrees"
reading=30
fi
fi
fi
current_temperature=$reading
echo "The current temperature is $current_temperature °C"
if (( $(bc -l <<<"$current_temperature >= $temp") )); then
#if [ $current_temperature -ge $temp ]
#then
echo "Need to switch ON" # if temperature > threshold then switch on
irsend SEND_ONCE achaier on
fi
if (( $(bc -l <<<"$current_temperature < $temp") )); then
#if [ $current_temperature -lt $temp ]
#then
echo "Need to switch OFF" # if temperature < threshold then switch off
irsend SEND_ONCE achaier off
fi
sleep 600 # sleep 10 minutes
done
if [ 0 -ge 1 ]; then
while [ 1 -ge 0 ]
do
# try to get tempearture from HomeAssistant Logs
filetemperature="/home/homeassistant/.homeassistant/temperature_desk.csv"
lastlog=`stat -c %Y $filetemperature`
now=`date +%s`
let age=$now-$lastlog
echo "File age is $age seconds"
if [ $age -lt 2400 ] #40 minutes
then
current_temperature=$(tail -1 /home/homeassistant/.homeassistant/temperature_desk.csv | cut -f2 -d",")
echo "Temperature from $filetemperature : $current_temperature"
else
echo "try to get temperature from WLAN direct connection to the ESP8266"
~/rasp_scripts/connect_wlanX.sh -i wlan0 -s $subnet
# -q quiet
# -c nb of pings to perform
ping -q -c5 $host > /dev/null
if [ $? -eq 0 ]
then
echo "Host is reachable, sleeping $sl"
sleep $sl
url="$host"/last_temp.csv
echo "URL $url"
wget -d --tries=4 --read-timeout=3 -O "last" "$host"/last_temp.csv
#curl -L -C - -o "last" $url #"$host"/last_temp.csv
#lynx -source $url > last
current_temperature=$(cat last | cut -f2 -d"," | cut -f1 -d".")
echo "Temperature from $host : $current_temperature"
else
#trying to read call-up log
lastlog=`stat -c %Y /home/pi/temperature.log`
now=`date +%s`
let age=$now-$lastlog
echo "File age is $age seconds"
if [ $age -lt 2400 ] #40 minutes
then
current_temperature=`less /home/pi/temperature.log | grep Appending | tail -1 | cut -f2 -d":" | cut -f2 -d","` # | cut -f1 -d"."`
echo "Temperature from callup: $current_temperature ($age seconds ago)"
else
curl --max-time 15 wttr.in | head -4 | tail -1 > t
bytes=`stat -c %s t`
if [ $bytesize -gt 0 ]
then
current_temperature=$(more t | sed 's/\x1B\[[0-9;]\+[A-Za-z]//g' | cut -f3 -d"." | tr -d " " | cut -c1-2)
echo "Going from weather: $current_temperature"
else
echo "No reading available ... assuming temperature is 30 degrees"
current_temperature=30
fi
fi
fi
fi
echo "The current temperature is $current_temperature °C"
if (( $(bc -l <<<"$current_temperature >= $temp") )); then
#if [ $current_temperature -ge $temp ]
#then
echo "Need to switch ON" # if temperature > threshold then switch on
irsend SEND_ONCE achaier on
fi
if (( $(bc -l <<<"$current_temperature < $temp") )); then
#if [ $current_temperature -lt $temp ]
#then
echo "Need to switch OFF" # if temperature < threshold then switch off
irsend SEND_ONCE achaier off
fi
sleep 600 # sleep 10 minutes
done
fi