3 from collections import deque
5 import paho.mqtt.client as mqtt
14 print(("\t" * indent) + msg)
17 if x is None or y is None:
26 self.running_averages = {}
29 self.now = time.time()
30 tm = time.localtime(self.now)
31 self.year = tm.tm_year
32 self.month = tm.tm_mon
34 self.hour = tm.tm_hour
36 self.wday = tm.tm_wday
37 debug("[{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d} wday={}]".format(
38 self.year, self.month, self.day,
39 self.hour, self.min, tm.tm_sec,
43 def get_sensor(self, key):
44 topic = "burrow/" + key
45 if topic in self.attrs:
46 s = self.attrs[topic].split(" ")
47 if len(s) >= 2 and int(s[1]) < self.now - 120:
48 debug("< {} EXPIRED".format(key))
51 debug("< {} = {}".format(key, s[0]))
54 debug("< {} UNDEFINED".format(key))
57 def set(self, key, val):
59 topic = "burrow/" + key
60 debug("> {} = {}".format(topic, val))
61 mq.publish(topic, val, qos=1, retain=True)
63 def auto_enabled(self, key):
64 topic = "burrow/auto/" + key
65 if topic in self.attrs:
66 return self.attrs[topic] != '0'
70 def hysteresis(self, key, value, low, high):
71 old_state = self.hyst_state.get(key, 0)
84 self.hyst_state[key] = new_state
87 def update_average(self, key, window_seconds):
88 if key not in self.running_averages:
89 self.running_averages[key] = (deque(), 0, 0, None)
90 (history, sum, count, avg) = self.running_averages[key]
92 while len(history) > 0 and history[0][0] <= self.now - window_seconds:
93 if history[0][1] is not None:
98 curr = self.get_sensor(key)
99 history.append((self.now, curr))
104 if count > len(history) // 2:
109 self.running_averages[key] = (history, sum, count, avg)
111 debug("= avg NONE ({} samples, {} non-null)".format(len(history), count))
113 debug("= avg {:.6} ({} samples, {} non-null)".format(avg, len(history), count))
114 self.set("avg/" + key, "{:.6} {}".format(avg, int(self.now)))
116 def get_sensor_avg(self, key):
117 val = self.running_averages[key][3]
119 debug("< {} = avg NONE".format(key))
121 debug("< {} = avg {:.6}".format(key, val))
126 def on_connect(mq, userdata, flags, rc):
127 mq.subscribe("burrow/#")
129 def on_message(mq, userdata, msg):
131 # debug("Message {}: {}".format(msg.topic, msg.payload))
132 st.attrs[msg.topic] = msg.payload.decode('utf-8')
136 lt = st.get_sensor("temp/loft")
137 lt_high = st.hysteresis('lt_high', lt, 29, 30)
138 lt_mid = st.hysteresis('lt_mid', lt, 24, 25)
142 if st.hour in range(10, 20):
147 if st.hour in range(8, 22):
148 if st.min % 30 in range(0, 5):
154 # FIXME: Disabled for now
156 st.set("loft/fan", fs)
160 if st.hour in range(7, 24):
164 st.set("loft/circulation", c)
168 tii = st.get_sensor_avg('air/inside-intake')
169 tie = st.get_sensor_avg('air/inside-exhaust')
170 toi = st.get_sensor_avg('air/outside-intake')
171 tmix = st.get_sensor_avg('air/mixed')
172 house_warm = st.hysteresis('house_warm', tii, 23.5, 24.5)
173 house_hot = st.hysteresis('house_hot', tii, 24.5, 25)
175 # Is AC currently on (mixed air is significantly colder than inside exhaust)?
176 if tie is None or tmix is None:
179 ac_off = st.hysteresis('ac_off', tmix, tie - 5, tie - 4)
181 # XXX: Temporarily disabled
186 # Do we want to bypass the heat exchanger?
187 outside_warmer = st.hysteresis('outside_warmer', diff(toi, tii), -0.5, 0.5)
188 if (house_warm > 0) and (outside_warmer > 0) or \
189 (house_warm < 0) and (outside_warmer < 0):
190 st.set('air/bypass', 0)
192 st.set('air/bypass', 1)
194 # Is mixed air colder than air from the inside?
195 mixed_warmer = st.hysteresis('mixed_warmer', diff(tmix, tii), -1, 0)
197 # Do we want to boost heat exchanger fan?
198 if ac_off < 0 or (house_hot > 0 and mixed_warmer < 0):
199 st.set('air/exchanger-fan', 255)
201 st.set('air/exchanger-fan', 0)
203 debug("Air: house_warm={} house_hot={} ac_off={} outside_warmer={} mixed_warmer={}".format(house_warm, house_hot, ac_off, outside_warmer, mixed_warmer))
205 opts, args = getopt.gnu_getopt(sys.argv[1:], "", ["debug"])
212 mq.on_connect = on_connect
213 mq.on_message = on_message
214 mq.will_set("status/auto", "dead", retain=True)
215 mq.connect("burrow-mqtt")
216 mq.publish("status/auto", "ok", retain=True)
219 # Heuristic delay to get all attributes from MQTT
223 ('loft-fan', auto_loft_fan),
233 st.update_average('air/outside-intake', 60)
234 st.update_average('air/inside-intake', 60)
235 st.update_average('air/inside-exhaust', 60)
236 st.update_average('air/mixed', 60)
239 for name, func in checks:
240 if st.auto_enabled(name):
246 debug("{} DISABLED".format(name))