4 import paho.mqtt.client as mqtt
13 print(("\t" * indent) + msg)
16 if x is None or y is None:
25 self.running_averages = {}
28 self.now = time.time()
29 tm = time.localtime(self.now)
30 self.year = tm.tm_year
31 self.month = tm.tm_mon
33 self.hour = tm.tm_hour
35 self.wday = tm.tm_wday
36 debug("[{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d} wday={}]".format(
37 self.year, self.month, self.day,
38 self.hour, self.min, tm.tm_sec,
42 def get_sensor(self, key):
43 topic = "burrow/" + key
44 if topic in self.attrs:
45 s = self.attrs[topic].split(" ")
46 if len(s) >= 2 and int(s[1]) < self.now - 120:
47 debug("< {} EXPIRED".format(key))
50 debug("< {} = {}".format(key, s[0]))
53 debug("< {} UNDEFINED".format(key))
56 def set(self, key, val):
58 topic = "burrow/" + key
59 debug("> {} = {}".format(topic, val))
60 mq.publish(topic, val, qos=1, retain=True)
62 def auto_enabled(self, key):
63 topic = "burrow/auto/" + key
64 if topic in self.attrs:
65 return self.attrs[topic] != '0'
69 def hysteresis(self, key, value, low, high):
70 if key in self.hyst_state:
71 old_state = self.hyst_state[key]
86 self.hyst_state[key] = new_state
89 def update_average(self, key, window_seconds):
90 if key not in self.running_averages:
91 self.running_averages[key] = ([], 0, 0, None)
92 (history, sum, count, avg) = self.running_averages[key]
94 while len(history) > 0 and history[0][0] <= self.now - window_seconds:
95 if history[0][1] is not None:
100 curr = self.get_sensor(key)
101 history.append((self.now, curr))
106 if count > len(history) // 2:
111 self.running_averages[key] = (history, sum, count, avg)
112 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]
118 debug("< {} = avg {:.6}".format(key, val))
123 def on_connect(mq, userdata, flags, rc):
124 mq.subscribe("burrow/#")
126 def on_message(mq, userdata, msg):
128 # debug("Message {}: {}".format(msg.topic, msg.payload))
129 st.attrs[msg.topic] = msg.payload.decode('utf-8')
133 lt = st.get_sensor("temp/loft")
134 lt_high = st.hysteresis('lt_high', lt, 29, 30)
135 lt_mid = st.hysteresis('lt_mid', lt, 24, 25)
139 if st.hour in range(10, 20):
144 if st.hour in range(8, 22):
145 if st.min % 30 in range(0, 5):
151 # FIXME: Disabled for now
153 st.set("loft/fan", fs)
157 if st.hour in range(20, 22):
161 st.set("loft/circulation", c)
165 tii = st.get_sensor_avg('air/inside-intake')
166 tie = st.get_sensor_avg('air/inside-exhaust')
167 toi = st.get_sensor_avg('air/outside-intake')
168 tmix = st.get_sensor_avg('air/mixed')
169 house_warm = st.hysteresis('house_warm', tii, 23.5, 24.5)
170 house_hot = st.hysteresis('house_hot', tii, 24.5, 25)
172 # Is AC currently on (mixed air is significantly colder than inside exhaust)?
173 if tie is None or tmix is None:
176 ac_off = st.hysteresis('ac_off', tmix, tie - 5, tie - 4)
178 # Do we want to bypass the heat exchanger?
179 outside_warmer = st.hysteresis('outside_warmer', diff(toi, tii), -0.5, 0.5)
180 if (house_warm > 0) and (outside_warmer > 0) or \
181 (house_warm < 0) and (outside_warmer < 0):
182 st.set('air/bypass', 0)
184 st.set('air/bypass', 1)
186 # Is mixed air colder than air from the inside?
187 mixed_warmer = st.hysteresis('mixed_warmer', diff(tmix, tii), -1, 0)
189 # Do we want to boost heat exchanger fan?
190 if ac_off < 0 or (house_hot > 0 and mixed_warmer < 0):
191 st.set('air/exchanger-fan', 255)
193 st.set('air/exchanger-fan', 0)
195 debug("Air: house_warm={} house_hot={} ac_off={} outside_warmer={} mixed_warmer={}".format(house_warm, house_hot, ac_off, outside_warmer, mixed_warmer))
197 opts, args = getopt.gnu_getopt(sys.argv[1:], "", ["debug"])
204 mq.on_connect = on_connect
205 mq.on_message = on_message
206 mq.will_set("status/auto", "dead", retain=True)
207 mq.connect("127.0.0.1")
208 mq.publish("status/auto", "ok", retain=True)
211 # Heuristic delay to get all attributes from MQTT
215 ('loft-fan', auto_loft_fan),
225 st.update_average('air/outside-intake', 60)
226 st.update_average('air/inside-intake', 60)
227 st.update_average('air/inside-exhaust', 60)
228 st.update_average('air/mixed', 60)
231 for name, func in checks:
232 if st.auto_enabled(name):
238 debug("{} DISABLED".format(name))