3 from collections import deque
6 import paho.mqtt.client as mqtt
11 log_name = '/run/burrow-auto'
17 print(("\t" * indent) + msg, file=sys.stderr)
18 elif log_file is not None:
19 print(("\t" * indent) + msg, file=log_file)
24 log_file = open(log_name + '.new', 'w')
33 os.rename(log_name + '.new', log_name)
36 if x is None or y is None:
45 self.running_averages = {}
48 self.now = time.time()
49 tm = time.localtime(self.now)
50 self.year = tm.tm_year
51 self.month = tm.tm_mon
53 self.hour = tm.tm_hour
55 self.wday = tm.tm_wday
56 debug("[{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d} wday={}]".format(
57 self.year, self.month, self.day,
58 self.hour, self.min, tm.tm_sec,
62 def get_sensor(self, key):
63 topic = "burrow/" + key
64 if topic in self.attrs:
65 s = self.attrs[topic].split(" ")
66 if len(s) >= 2 and int(s[1]) < self.now - 120:
67 debug("< {} EXPIRED".format(key))
70 debug("< {} = {}".format(key, s[0]))
73 debug("< {} UNDEFINED".format(key))
76 def set(self, key, val):
78 topic = "burrow/" + key
79 debug("> {} = {}".format(topic, val))
80 mq.publish(topic, val, qos=1, retain=True)
82 def send(self, key, val):
84 topic = "burrow/" + key
85 debug("> {} := {}".format(topic, val))
86 mq.publish(topic, val, qos=1, retain=False)
88 def auto_enabled(self, key):
89 topic = "burrow/auto/" + key
90 if topic in self.attrs:
91 return self.attrs[topic] != '0'
95 def hysteresis(self, key, value, low, high):
96 old_state = self.hyst_state.get(key, 0)
109 self.hyst_state[key] = new_state
112 def update_average(self, key, window_seconds):
113 if key not in self.running_averages:
114 self.running_averages[key] = (deque(), 0, 0, None)
115 (history, sum, count, avg) = self.running_averages[key]
117 while len(history) > 0 and history[0][0] <= self.now - window_seconds:
118 if history[0][1] is not None:
123 curr = self.get_sensor(key)
124 history.append((self.now, curr))
129 if count > len(history) // 2:
134 self.running_averages[key] = (history, sum, count, avg)
136 debug("= avg NONE ({} samples, {} non-null)".format(len(history), count))
138 debug("= avg {:.6} ({} samples, {} non-null)".format(avg, len(history), count))
139 self.set("avg/" + key, "{:.6} {}".format(avg, int(self.now)))
141 def get_sensor_avg(self, key):
142 val = self.running_averages[key][3]
144 debug("< {} = avg NONE".format(key))
146 debug("< {} = avg {:.6}".format(key, val))
154 lt = st.get_sensor_avg("temp/loft")
155 out = st.get_sensor_avg('air/outside-intake')
157 if lt is None or out is None:
159 elif st.hysteresis('lf_out_cold', out, 5, 6) < 0:
161 elif st.hysteresis('lf_out_cool', out, 14, 15) < 0:
162 if st.min in range(10, 15):
168 elif st.hysteresis('lf_loft_hot', lt, 25, 26) > 0:
169 if st.hysteresis('lf_loft_hotter_than_out', lt, out - 1, out + 1) > 0:
174 if st.min in range(10, 15):
179 st.set("loft/fan", fs)
184 if st.hour in range(7, 24):
188 st.set("loft/circulation", c)
192 # Heuristics to tell if AC is currently on
194 tie = st.get_sensor_avg('air/inside-exhaust')
195 tmix = st.get_sensor_avg('air/mixed')
196 if tie is None or tmix is None:
199 # Mixed air is significantly colder than inside exhaust
200 ac_on = -st.hysteresis('ac_off', tmix, tie - 5, tie - 4)
202 # FIXME: It might also mean that the loft is cold
209 tii = st.get_sensor_avg('air/inside-intake')
210 tie = st.get_sensor_avg('air/inside-exhaust')
211 toi = st.get_sensor_avg('air/outside-intake')
212 tmix = st.get_sensor_avg('air/mixed')
213 house_warm = st.hysteresis('house_warm', tii, 23.5, 24.5)
214 house_hot = st.hysteresis('house_hot', tii, 24.5, 25)
217 # XXX: Temporarily disabled
222 # Do we want to bypass the heat exchanger?
223 outside_warmer = st.hysteresis('outside_warmer', diff(toi, tii), -0.5, 0.5)
224 if (house_warm > 0) and (outside_warmer > 0) or \
225 (house_warm < 0) and (outside_warmer < 0):
226 st.set('air/bypass', 0)
228 st.set('air/bypass', 1)
230 # Is mixed air colder than air from the inside?
231 mixed_warmer = st.hysteresis('mixed_warmer', diff(tmix, tii), -1, 0)
233 # Do we want to boost heat exchanger fan?
234 if ac_on > 0 or (house_hot > 0 and mixed_warmer < 0):
235 st.set('air/exchanger-fan', 255)
237 st.set('air/exchanger-fan', 0)
239 debug("Air: house_warm={} house_hot={} ac_on={} outside_warmer={} mixed_warmer={}".format(house_warm, house_hot, ac_on, outside_warmer, mixed_warmer))
242 st.set("air/ac-on", "{} {}".format(1 if ac_on > 0 else 0, int(st.now)))
244 st.set("air/ac-on", "")
249 tii = st.get_sensor_avg('air/inside-intake')
250 tie = st.get_sensor_avg('air/inside-exhaust')
251 house_hot = st.hysteresis('ac_house_hot', tii, 23.5, 24)
252 outside_hot = st.hysteresis('ac_outside_hot', tie, 23, 24)
255 if house_hot > 0 and outside_hot > 0:
260 if not hasattr(st, 'last_ac_change'):
261 st.last_ac_change = st.now
262 need_wait = st.last_ac_change + 300 - st.now # FIXME: Increase
265 action = f"wait({need_wait:.0f})"
268 elif ac_on != want_ac:
270 st.send('air/aircon-remote', 'p')
271 st.last_ac_change = st.now
275 debug("AC: house_hot={} outside_hot={} ac_on={} want_ac={} action={}".format(house_hot, outside_hot, ac_on, want_ac, action))
278 def on_connect(mq, userdata, flags, rc):
279 mq.subscribe("burrow/#")
281 def on_message(mq, userdata, msg):
283 # debug("Message {}: {}".format(msg.topic, msg.payload))
284 st.attrs[msg.topic] = msg.payload.decode('utf-8')
286 opts, args = getopt.gnu_getopt(sys.argv[1:], "", ["debug"])
293 mq.on_connect = on_connect
294 mq.on_message = on_message
295 mq.will_set("status/auto", "dead", retain=True)
296 mq.connect("burrow-mqtt")
297 mq.publish("status/auto", "ok", retain=True)
300 # Heuristic delay to get all attributes from MQTT
304 ('loft-fan', auto_loft_fan),
307 ('aircon', auto_aircon),
316 st.update_average('air/outside-intake', 180)
317 st.update_average('air/inside-intake', 180)
318 st.update_average('air/inside-exhaust', 180)
319 st.update_average('air/mixed', 180)
320 st.update_average('temp/loft', 180)
323 for name, func in checks:
324 if st.auto_enabled(name):
330 debug("{} DISABLED".format(name))