-
Notifications
You must be signed in to change notification settings - Fork 1
/
wemoPlug-comed-old2.py
executable file
·220 lines (192 loc) · 6.2 KB
/
wemoPlug-comed-old2.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
#!/usr/bin/env python3
import sys
import time
import math
import pywemo
import random
import datetime
import comedlib
import commonlib
CL = commonlib.CommonLib()
### TODO
# add initial comment to log file
# keep track of length of time charged or off
#badHours = [5, 6, 17, 18]
#badHours = [17, 18]
badHours = []
chargingCutoffPrice = 3.99
#wemoIpAddress = "192.168.2.165" #insight1
wemoIpAddress = "192.168.2.166" #insight2
#always start charge below this value
lower_bound = 2.8
#always stop charge above this value
upper_bound = 10.2
#wemoIpAddress = "192.168.2.168" #plug6
class ComedSmartWemoPlug(object):
def __init__(self):
self.depth = 0
self.connectToWemo()
self.comlib = comedlib.ComedLib()
self.comlib.msg = False
return
#======================================
def connectToWemo(self):
self.address = wemoIpAddress
self.port = pywemo.ouimeaux_device.probe_wemo(self.address)
#self.wemoUrl = 'http://%s:%i/setup.xml' % (self.address, self.port)
self.wemoUrl = pywemo.setup_url_for_address(self.address)
self.device = pywemo.discovery.device_from_description(self.wemoUrl)
#======================================
def getCurrentComedRate(self):
return self.comlib.getCurrentComedRate()
#======================================
def getMedianComedRate(self):
median,std = self.comlib.getMedianComedRate()
return median
#======================================
def getReasonableCutOff(self):
return self.comlib.getReasonableCutOff()
#======================================
def getPredictedRate(self):
return self.comlib.getPredictedRate()
#======================================
def enable(self):
self.depth += 1
if self.device.get_state() == 0:
mystr = "turning ON wemo plug, start charging"
print(CL.colorString(mystr, "green"))
self.device.on()
time.sleep(15)
self.writeToLogFile("begin charging")
else:
#print "charging already active"
pass
time.sleep(5)
if self.device.get_state() in (1, 8):
self.depth = 0
return
print(f"state = {self.device.get_state()}")
print(CL.colorString("ERROR: starting charging", "red"))
if self.depth > 10:
sys.exit(1)
else:
self.enable()
return
#======================================
def disable(self):
self.depth += 1
if self.device.get_state() in (1, 8):
mystr = f"turning OFF wemo plug, stop charging (state = {self.device.get_state()})"
print(CL.colorString(mystr, "red"))
self.device.off()
time.sleep(5)
self.writeToLogFile("stop charging")
else:
#print "charging already disabled"
pass
# get state of device
if self.device.get_state() == 0:
self.depth = 0
return
print(f"state = {self.device.get_state()}")
print(CL.colorString("ERROR: turning off charging", "red"))
time.sleep(0.2 * self.depth)
if self.depth > 10:
sys.exit(1)
else:
self.disable()
return
#======================================
def writeToLogFile(self, msg):
f = open("car_charging-wemo_log.csv", "a")
f.write("%d\t%s\t%s\n"%(time.time(), time.asctime(), msg))
f.close()
#======================================
if __name__ == '__main__':
count = 0
last_hour = -2
refreshTime = 180
wemoplug = ComedSmartWemoPlug()
while(True):
if count > 0:
factor = (math.sqrt(random.random()) + math.sqrt(random.random()))/1.414
sleepTime = refreshTime*factor
#print "Sleep %d seconds"%(sleepTime)
time.sleep(sleepTime)
count += 1
now = datetime.datetime.now()
hour = now.hour
if hour != last_hour:
print('============== new hour ==')
last_hour = hour
timestr = "%02d:%02d"%(now.hour, now.minute)
### always enable before 5AM
#if hour < 5:
# wemoplug.enable()
# continue
### special condition where it is assumed to be a high price
if hour in badHours:
if now.minute < 20:
mystr = "charging disabled, bad hour, sleep until %d:20"%(hour)
print(CL.colorString(mystr, "red"))
wemoplug.disable()
minutesToSleep = 20 - now.minute - 2
sleepTime = minutesToSleep*60
if sleepTime > 0:
time.sleep(sleepTime)
continue
else:
#print "good hour %d"%(hour)
pass
### get the comed rate
current_rate = wemoplug.getCurrentComedRate()
predict_rate = wemoplug.getPredictedRate()
median = wemoplug.getMedianComedRate()
cutoff = wemoplug.getReasonableCutOff()
### more strict cutoff
#cutoff = (cutoff + 2.0 * median) / 3.0
if cutoff < lower_bound:
cutoff = lower_bound
#elif cutoff > upper_bound:
# cutoff = upper_bound
### less strict cutoff
#cutoff += 1.0
### more strict cutoff
cutoff -= 0.5
#print("Adjusted cutoff = %.2f ( %.1f | %.1f )"%(cutoff, chargingCutoffPrice, reasonableCutoff))
#print(rate)
#print(median)
#print(cutoff)
#print(now.minute)
if predict_rate > 2.0*cutoff and now.minute > 20:
mystr = "%s: charging LONG disable over double price per kWh ( %.2f | %.2f | cutoff = %.2f )"%(timestr, current_rate, predict_rate, cutoff)
print(CL.colorString(mystr, "red"))
wemoplug.disable()
#print "wait out the rest of the hour"
minutesToSleep = 60 - now.minute - 2
sleepTime = minutesToSleep*60
if sleepTime > 0:
time.sleep(sleepTime)
continue
buffer_rate = 0.5
if predict_rate < lower_bound:
mystr = "%s: charging enabled ( %.2f c/kWh | %.2f c/kWh | cutoff = %.2f c/kWh )"%(timestr, current_rate, predict_rate, cutoff)
print(CL.colorString(mystr, "green"))
wemoplug.enable()
elif predict_rate > float(cutoff) + buffer_rate:
mystr = "%s: charging disabled ( %.2f c/kWh | %.2f c/kWh | cutoff = %.2f c/kWh )"%(timestr, current_rate, predict_rate, cutoff)
print(CL.colorString(mystr, "red"))
wemoplug.disable()
continue
elif predict_rate > upper_bound:
mystr = "%s: charging disabled ( %.2f c/kWh | %.2f c/kWh | upper_bound = %.2f c/kWh )"%(timestr, current_rate, predict_rate, upper_bound)
print(CL.colorString(mystr, "red"))
wemoplug.disable()
continue
elif predict_rate < float(cutoff) - buffer_rate:
mystr = "%s: charging enabled ( %.2f c/kWh | %.2f c/kWh | cutoff = %.2f c/kWh )"%(timestr, current_rate, predict_rate, cutoff)
print(CL.colorString(mystr, "green"))
wemoplug.enable()
else:
mystr = "%s: charging unchanged ( %.2f c/kWh | %.2f c/kWh | cutoff = %.2f c/kWh )"%(timestr, current_rate, predict_rate, cutoff)
print(CL.colorString(mystr, "brown"))