]> www.infradead.org Git - users/dwmw2/esp32-pool.git/commitdiff
Fix default setting of pump
authorDavid Woodhouse <dwmw@amazon.co.uk>
Wed, 6 Sep 2023 13:15:58 +0000 (14:15 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Wed, 6 Sep 2023 13:21:05 +0000 (14:21 +0100)
Make a script which turns the pool pump on or off depending on whether
the time is between 7am and 7pm. Run it at 7am and 7pm.

Also run it on time sync, and on startup, to ensure that the pump is
running if it's supposed to be after a reboot.

The 'force' parameter ensures that we don't keep flipping it on at every
time sync; only at startup and at the regular 7am/7pm switches. So if
it's been overridden manually it should stay that way without surprises.

pool.yaml

index ffb3cadd5c596d28487c96b699fde46bc5909f20..d6d3ce044fd2529465c82848df6ed949830432f5 100644 (file)
--- a/pool.yaml
+++ b/pool.yaml
 
 esphome:
   name: pool
+  on_boot:
+    then:
+      - script.execute:
+          id: pump_default
+          force: false
 
 esp32:
   board: esp32-gateway
@@ -89,6 +94,25 @@ script:
         last_change = now;
         id(valve_output).toggle();
 
+    # As soon as we know what the time is (and at 7am and 7pm), decide if the pump should be on.
+  - id: pump_default
+    parameters:
+      force: bool # Set it again even if it was already set since boot
+    then:
+      lambda: |-
+        static bool done = false;
+        if (force || !done) {
+           auto t = id(sntp_time).now();
+           if (t.is_valid()) {
+              ESP_LOGD("pump_default", "Setting pump switch at %dh", t.hour);
+              if (t.hour >= 7 && t.hour < 19)
+                id(pool_pump).turn_on();
+              else
+                id(pool_pump).turn_off();
+           }
+           done = true;
+        }
+
 globals:
   - id: solar_in_fails # Number of minutes without a reading.
     type: int
@@ -139,15 +163,16 @@ time:
         # Turn pool pump on at 7am and off at 7pm
       - seconds: 0
         minutes: 0
-        hours: 7
+        hours: 7,19
         then:
-          - switch.turn_on: pool_pump
-      - seconds: 0
-        minutes: 0
-        hours: 19
-        then:
-          - switch.turn_off: pool_pump
-
+          script.execute:
+            id: pump_default
+            force: true
+    on_time_sync:
+      then:
+        script.execute:
+          id: pump_default
+          force: false
 
 ethernet:
   type: LAN8720