-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest_odr.py
517 lines (451 loc) · 17.1 KB
/
nest_odr.py
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# nest_odr.py -- a python interface to the Nest thermometer to provide
# outdoor reset override to boost boiler output to allow faster recovery from
# nightly temperature setbacks.
# ------------------------------------------------------------------------------
#
# Copyright 2016 Brent L. Brock and HoloScene Wildlife Services LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------
# Author: Brent L. Brock
# ---------------------------------------------------------------------------
#
# Note: nest-odr requires python-nest developed by Jason Kölker, https://github.com/jkoelker/python-nest/
import os, sys, time, datetime, threading, nest
from tendo import singleton
from nest_extras import get_napi, get_parameters, data_log, setup_log_handlers
from nest import utils as nest_utils
try:
import RPi.GPIO as GPIO
except:
pass
# -------------------- Classes ---------------------------
# --------------------------------------------------------
class thermostat(object):
def __init__(self, name):
self.name = name
self.active = False
self.h_state = None
self.stage = 0
self.new = False
self.t_start = None
self.thresh_1 = Thresh_Stage_1
def setactive(self, active):
self.active = active
return self.active
def setstage(self, stage):
self.stage = stage
if self.stage > 0 and self.t_start == None:
self.t_start = datetime.datetime.now()
return self.stage, self.t_start
def seth_state(self, h_state):
self.h_state = h_state
return self.h_state
def set_new(self, new):
self.new = new
return self.new
def set_thresh_1(self, thresh):
self.thresh_1 = thresh
return self.thresh_1
# -------------------- Functions -------------------------
# --------------------------------------------------------
# Track how long communication with Nest server is down
def check_connection(i):
max_time = 15
if i <= max_time - 1 and Stage > 0:
my_handler.setFormatter(log_formatter)
print('--Unable to connect to Nest server for ' + str(i) + ' minutes. Will retry in 1 minute.--')
my_handler.setFormatter(log_formatter)
try:
app_log.warning('--Unable to connect to Nest server for ' + str(i) + ' minutes. Will retry in 1 minute.--')
except:
pass
i = i + 1
time.sleep(60)
get_napi(username, password)
if not structure == None:
print('--Nest server connection succeeded--')
try:
app_log.info('\t--Nest server connection succeeded--')
except:
pass
i = 0
elif i > max_time - 1:
con_err()
sys.exit()
else:
print('--Unable to connect to Nest server. Will try again on next cycle.--')
my_handler.setFormatter(blnk_formatter)
try:
app_log.info('')
my_handler.setFormatter(log_formatter)
app_log.warning('--Unable to connect to Nest server. Will try again on next cycle.--')
except:
pass
sys.exit()
i = i + 1
return (i)
# Reset ODR and exit program if connection to Nest Server cannot be made during boost cycle
def con_err():
print('--Unable to connect to Nest server for ' + str(max_time) + ' minutes. Resetting ODR.--')
try:
my_handler.setFormatter(blnk_formatter)
app_log.error('')
my_handler.setFormatter(log_formatter)
app_log.error('--Unable to connect to Nest server for ' + str(max_time) + ' minutes. Resetting ODR.--')
except:
pass
Restore_ODR()
sys.exit()
return
# Set GPIO pins to output
def GPIO_out(pins):
print 'Running GPIO_out...'
try:
GPIO.setmode(GPIO.BOARD)
for p in pins:
GPIO.setup(p, GPIO.OUT)
GPIO.output(p, False)
print 'Pin ' + str(p) + ' set to OUT'
except:
try:
app_log.error('*** GPIO pins could not be set to OUT ***')
except:
pass
def set_GPIO(pin, state):
try:
print ('Trying to set GPIO pin, ' + str(pin) + ' to ' + str(state))
GPIO.output(pin,state)
except:
try:
app_log.error('*** GPIO pin: ' + str(pin) + ' could not be set to ' + str(state) + ' ***')
except:
pass
# Make sure GPIO pin states match boost stage
def GPIO_check(target_states):
cur_pin_state = []
try:
for p in pins:
if s:
s = True
elif not s:
s = False
cur_pin_state.append(s)
print 'Pin: ' + str(p) + ' is ' + str(s)
GPIO_out(pins)
print 'GPIO check successful'
except:
if not cur_pin_state == target_states:
try:
app_log.error('*** Current GPIO pin states do not match target pin states - Potential hardware malfunction ***')
except:
pass
def make_therm_objects():
global therm_list
therm_list = [thermostat(device.where) for device in structure.devices]
return therm_list
def get_target_stage():
level_list = []
for t in therm_list:
level_list.append(t.stage)
target_stage = int(max(level_list))
sum_stage = int(sum(level_list))
return target_stage, sum_stage
# Measures the temperature differential between the current room temperature and the target setpoint. If room temperature is
# <= 1.25 degrees F of the target temperature for any thermomemter, then relay_trigger is set to 1. HVAC-Stat is also checked to make
# sure a thermostat is actually calling for heat as an additional check.
def ODR_override():
# Make sure connection with Nest server was established before continuing.
Away = structure.away
for device in structure.devices:
T_name = device.where
T_room = nest_utils.c_to_f(device.temperature)
if Away:
T_target = nest_utils.c_to_f(device.away_temperature[0])
try:
app_log.info('-- Away mode enabled --')
except:
pass
print('-- Away mode enabled --')
else:
T_target = nest_utils.c_to_f(device.target)
H_stat = device.hvac_heater_state
T_diff = T_room - T_target
T_outside = nest_utils.c_to_f(structure.weather.current.temperature)
for th in therm_list:
if th.name == T_name:
t = th
t.seth_state(H_stat)
# Check if thermostat was calling for recovery in previous loop and sets threashold temp to -0.75F to
# maintain boost cycle until call for heat on device is satisfied.
if t.active:
t.set_thresh_1(-0.75)
if float(T_diff) < float(Thresh_Stage_3) and H_stat == True:
t.setstage(3)
elif float(T_diff) < float(Thresh_Stage_2) and H_stat == True:
t.setstage(2)
elif float(T_diff) < float(t.thresh_1) and H_stat == True:
t.setstage(1)
else:
t.setstage(0)
if t.stage > 0:
t.setactive(True)
else:
t.setactive(False)
## print 'Thermostat : %s' % T_name
## print 'Away Status : %s' % str(Away)
## print 'T_room : %s' % str(T_room)
## print 'T_target : %s' % str(T_target)
## print 'HVAC State : %s' % str(H_stat)
## print 'T_diff : %s' % str(T_diff)
## print 'T_thresh : %s' % str(t.thresh_1)
## print 'Device List : %s' % dev_list
## print '\n'
update_status(t)
# Write data to log file if option is set
if log_data:
d_log = data_log(structure, Stage, log_dir, max_log_size)
#d_log = data_log(napi, Stage, log_dir, max_log_size)
if d_log[1]:
try:
app_log.warning(d_log[0])
except:
pass
print d_log[0]
if get_target_stage()[0] > 0:
rchk = threading.Timer(delay_rechk, main)
rchk.start()
else:
rchk = None
if dev_list:
Increment_Temp()
# Log initaiaton of new boost cycle
def update_status(t):
## Report new boost initiated
if t.active and Stage == 0 and not t.name in dev_list:
t.set_new(True)
my_handler.setFormatter(log_formatter)
try:
app_log.info('******** [ODR Override Initiated] ********')
except:
pass
## print('\n******** [ODR override initiated] ********')
## print('\tRequesting_Thermostat : %s' % t.name)
GPIO_out(pins)
dev_list.append(t.name)
# Add devices calling for boost to list and remove devices no longer calling for boost.
if t.active and t.h_state == True and not t.name in dev_list:
t.set_new(True)
dev_list.append(t.name)
try:
app_log.info('\t-- Device: ' + t.name + ' is requesting a new boost call and has been added to the queue')
except:
pass
## print('Device: ' + t.name + ' is requesting a new boost call and has been added to the queue.')
report_data(dev_list)
elif (t.stage == 0 or not t.h_state) and t.name in dev_list:
if not t.h_state:
try:
app_log.info('\t-- Nest call for heat on Device: ' + t.name + ' has ended. Removing ' + t.name + ' from the boost queue')
except:
pass
## print('\tNest call for heat on Device: ' + t.name + ' has ended. Removing ' + t.name + ' from the boost queue')
T_delta = datetime.datetime.now() - t.t_start
m, s = divmod(T_delta.total_seconds(), 60)
h, m = divmod(m, 60)
disp = "%d:%02d:%02d" % (h, m, s)
try:
app_log.info('\tBoost call for Device: ' + t.name + ' ended after ' + disp + ' H:MM:SS')
except:
pass
## print('*** Boost call for Device: ' + t.name + ' ended after ' + disp + ' H:MM:SS ***')
t.setactive(False)
dev_list.remove(t.name)
if not dev_list:
print('\nDevice list is empty...\n')
Restore_ODR()
else:
try:
app_log.info('\tBoost call continues for Device(s): ' + '[%s]' % ', '.join(map(str, dev_list)))
except:
pass
print('*** Boost call continues for Device(s): [%s]' % ', '.join(map(str, dev_list)) + ' ***')
# Controls GPIO pins 13,16, & 18 on raspberry pi.
#
# Water Temp PIN13 PIN16 PIN18
# Stage1 On Off Off
# Stage2 On On Off
# Stage3 On On On
# Adjust boost stage according to active calls
def Increment_Temp():
i_s = 0
global Stage
level_list = []
level, sum_stage = get_target_stage()
# Increase boost stage if cumulative boost call is large
if sum_stage > 5:
level = 3
# Set GPIO pins to target boost level
if Stage <> level:
if level >= 1:
set_GPIO(13, True) # 22K Resistance
else:
set_GPIO(13, False)
if level >= 2:
set_GPIO(16, True) # 22K + 47K = 69K Resistance
else:
set_GPIO(16, False)
if level == 3:
set_GPIO(18,True) # Infinite Resistance (circuit open)
else:
set_GPIO(18,False)
# Log output
if not level == Stage:
if level > Stage and Stage > 0:
try:
app_log.info('\t-- Calling thermostat queue status has changed. Increasing boost to Stage ' + str(level) + ' --')
except:
pass
## print('\t-- Calling thermostat status has changed. Increasing boost to Stage ' + str(level) + ' --')
elif level < Stage:
try:
app_log.info('\t-- Calling thermostat queue status has changed. Reducing boost to Stage ' + str(level) + ' --')
except:
pass
## print('\t-- Calling thermostat status has changed. Reducing boost to Stage ' + str(level) + ' --')
try:
app_log.info('\t-- Stage ' + str(level) + ' Boost engaged --')
except:
pass
## print('--Stage ' + str(level) + ' Boost engaged--')
report_data(dev_list)
Stage = level
# Send thermostat and weather data to log file
def report_data(thermostats):
Away = structure.away
devices = structure.devices
Time_s = structure.weather.current.datetime.strftime('%H:%M:%S')
for device in devices:
try:
Thermostat = device.where
if Thermostat in thermostats:
T_room = nest_utils.c_to_f(device.temperature)
if Away:
T_target = nest_utils.c_to_f(device.away_temperature[0])
app_log.info('-- Away mode enabled --')
## print('-- Away mode enabled --')
else:
T_target = nest_utils.c_to_f(device.target)
H_stat = device.hvac_heater_state
T_diff = T_room - T_target
T_outside = nest_utils.c_to_f(structure.weather.current.temperature)
app_log.info('\tSample Time : %s' % Time_s)
app_log.info('\tRequesting_Thermostat : %s' % Thermostat)
app_log.info('\tTemp_Differential : %s' % str(T_diff))
app_log.info('\tTemp_Room : %s' % str(T_room))
app_log.info('\tTemp_Target : %s' % str(T_target))
app_log.info('\tTemp_Outside : %s' % str(T_outside))
my_handler.setFormatter(blnk_formatter)
app_log.info('')
my_handler.setFormatter(log_formatter)
except:
pass
# Kill timers, cleanup pins, and exit program
def Restore_ODR():
try:
rchk.cancel()
print('recheck cancelled')
except:
pass
try:
GPIO.cleanup()
except:
pass
try:
T_delta = datetime.datetime.now() - T_start
m, s = divmod(T_delta.total_seconds(), 60)
h, m = divmod(m, 60)
disp = "%d:%02d:%02d" % (h, m, s)
app_log.info('\t-- ODR Override Cycle Complete --')
app_log.info('\t-- Total duration: ' + disp + ' H:MM:SS --')
my_handler.setFormatter(blnk_formatter)
app_log.info('')
print('**** ODR Override Cycle Complete ***** \n**** Total duration: ' + disp + ' H:MM:SS ***\n')
except:
pass
sys.exit()
def main():
global i
global structure
structure = get_napi(username, password)[0]
while structure == None:
i = check_connection(i)
if not therm_list:
make_therm_objects()
print('Thermostat objects created...')
ODR_override()
# ---------------------- PROGRAM STARTS HERE---------------------------- #
# sys.exit(-1) if other instance is running
me = singleton.SingleInstance()
# define variables
global structure
global therm_list
global dev_list
global data
relay_trigger = False
Stage = 0
dev_list = []
therm_list = []
T_start = datetime.datetime.now()
i = 0
pins = [13,16,18]
# Import configuration parameters
p = get_parameters()
for key,val in p.items():
exec(key + '=val')
# print ("key: " + str(key) + ", val: " + str(val))
# set up log file
# Wrap all logging operation in try statements to prevent lost connection to remote logging directory
# from causing the program to crash.
try:
log_object = setup_log_handlers(log_dir)
app_log = log_object[0]
my_handler = log_object[1]
log_formatter = log_object[2]
blnk_formatter = log_object[3]
if log_data:
from nest_extras import data_log
from subproccess import call
except:
pass
main()
# Set target humidity according to outside temperature
if set_hum:
from nest_extras import target_humidity
hum_value = target_humidity(structure, max_hum)
## print 'Outside Temp : %s' % str(nest_utils.c_to_f(structure.weather.current.temperature))
## print 'Outside Humidity: %s' % str(structure.weather.current.humidity)
## print 'Humidity Target : %s' % str(hum_value)
# Remove duplicate lines from data log without changing sort order
if log_data:
try:
dfile = log_dir + 'nest_data.log'
tmpfile = log_dir + 'temp.log'
call("awk '!x[$0]++' '%s' > '%s' && mv '%s' '%s'" % (dfile, tmpfile, tmpfile, dfile), shell=True)
except:
pass