]> mj.ucw.cz Git - home-hw.git/blobdiff - auto/burrow-auto
Auto: Meditation mode turned off
[home-hw.git] / auto / burrow-auto
index 077022dca6b61e8059fd80512536bc3f86402ac6..b876223dae335efe839a193bb07c71600fa0d8db 100755 (executable)
@@ -2,16 +2,35 @@
 
 from collections import deque
 import getopt
+import os
 import paho.mqtt.client as mqtt
 import sys
 import time
 
 debug_mode = False
+log_name = '/run/burrow-auto'
+log_file = None
 indent = 0
 
 def debug(msg):
     if debug_mode:
         print(("\t" * indent) + msg, file=sys.stderr)
+    elif log_file is not None:
+        print(("\t" * indent) + msg, file=log_file)
+
+def debug_open():
+    if not debug_mode:
+        global log_file
+        log_file = open(log_name + '.new', 'w')
+
+def debug_close():
+    if debug_mode:
+        debug("=" * 80)
+    else:
+        global log_file
+        log_file.close()
+        log_file = None
+        os.rename(log_name + '.new', log_name)
 
 def diff(x, y):
     if x is None or y is None:
@@ -132,26 +151,33 @@ st = State()
 
 def auto_loft_fan():
     global st
-    lt = st.get_sensor("temp/loft")
-    lt_high = st.hysteresis('lt_high', lt, 29, 30)
-    lt_mid = st.hysteresis('lt_mid', lt, 24, 25)
-    if lt_high > 0:
+    lt = st.get_sensor_avg("temp/loft")
+    out = st.get_sensor_avg('air/outside-intake')
+
+    if False and st.hour in range(21, 24) or st.hour in range(6, 10):
+        fs = 0
+    elif lt is None or out is None:
+        fs = 0
+    elif st.hysteresis('lf_out_cold', out, 5, 6) < 0:
+        fs = 0
+    elif st.hysteresis('lf_out_cool', out, 14, 15) < 0:
+        if st.min in range(10, 15):
+            fs = 2
+        else:
+            fs = 0
+    elif ac_is_on() > 0:
         fs = 3
-    elif lt_mid > 0:
-        if st.hour in range(10, 20):
+    elif st.hysteresis('lf_loft_hot', lt, 25, 26) > 0:
+        if st.hysteresis('lf_loft_hotter_than_out', lt, out - 1, out + 1) > 0:
             fs = 3
         else:
             fs = 1
     else:
-        if st.hour in range(8, 22):
-            if st.min % 30 in range(0, 5):
-                fs = 3
-            else:
-                fs = 0
+        if st.min in range(10, 15):
+            fs = 2
         else:
             fs = 0
-    # FIXME: Disabled for now
-    fs = 0
+
     st.set("loft/fan", fs)
 
 
@@ -214,14 +240,18 @@ def auto_air():
 
     debug("Air: house_warm={} house_hot={} ac_on={} outside_warmer={} mixed_warmer={}".format(house_warm, house_hot, ac_on, outside_warmer, mixed_warmer))
 
+    if ac_on != 0:
+        st.set("air/ac-on", "{} {}".format(1 if ac_on > 0 else 0, int(st.now)))
+    else:
+        st.set("air/ac-on", "")
+
 
 def auto_aircon():
     global st
     tii = st.get_sensor_avg('air/inside-intake')
     tie = st.get_sensor_avg('air/inside-exhaust')
-    # house_hot = st.hysteresis('ac_house_hot', tii, 24.5, 25)
-    house_hot = st.hysteresis('ac_house_hot', tii, 23, 24)
-    outside_hot = st.hysteresis('ac_outside_hot', tie, 20, 21)
+    house_hot = st.hysteresis('ac_house_hot', tii, 23.5, 24)
+    outside_hot = st.hysteresis('ac_outside_hot', tie, 24, 25)
     ac_on = ac_is_on()
 
     if house_hot > 0 and outside_hot > 0:
@@ -231,9 +261,10 @@ def auto_aircon():
 
     if not hasattr(st, 'last_ac_change'):
         st.last_ac_change = st.now
+    need_wait = st.last_ac_change + 300 - st.now    # FIXME: Increase
 
-    if st.last_ac_change >= st.now - 600:
-        action = "wait"
+    if need_wait > 0:
+        action = f"wait({need_wait:.0f})"
     elif ac_on == 0:
         action = "need-data"
     elif ac_on != want_ac:
@@ -279,6 +310,7 @@ checks = [
 ]
 
 while True:
+    debug_open()
     st.update()
 
     debug("averages")
@@ -298,5 +330,6 @@ while True:
             indent -= 1
         else:
             debug("{} DISABLED".format(name))
-    debug("=" * 80)
+
+    debug_close()
     time.sleep(10)