From: David Woodhouse Date: Wed, 6 Sep 2023 13:15:58 +0000 (+0100) Subject: Fix default setting of pump X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8b7728ddbd1c8a024205b82f05b3cb81a747b83d;p=users%2Fdwmw2%2Fesp32-pool.git Fix default setting of pump 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. --- diff --git a/pool.yaml b/pool.yaml index ffb3cad..d6d3ce0 100644 --- a/pool.yaml +++ b/pool.yaml @@ -11,6 +11,11 @@ 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