From 925863f3459386cfeaa1424df7796a7b436a5109 Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Sat, 28 May 2022 12:25:24 +0200
Subject: [PATCH] add selects for max/min/hysteresis remove globals, use only
 selects

---
 propbox.yaml | 84 +++++++++++++++++++++++++++++++++-------------------
 1 file changed, 53 insertions(+), 31 deletions(-)

diff --git a/propbox.yaml b/propbox.yaml
index a0c92b5..d26d688 100644
--- a/propbox.yaml
+++ b/propbox.yaml
@@ -19,13 +19,6 @@ substitutions:
   schedule_bloom_on: "0 * * * * *"
   schedule_bloom_off: "30 * * * * *"
 
-  temperature_max: "27"
-  temperature_min: "21"
-  temperature_hysteresis: "2"
-  humidity_max: "70"
-  humidity_min: "40"
-  humidity_hysteresis: "5"
-
 <<: !include common.yaml
 
 esp32:
@@ -44,12 +37,6 @@ http_request:
   useragent: esphome/${devicename}
   timeout: 10s
 
-globals:
-  - id: schedule
-    type: int
-    restore_value: true
-    initial_value: '0'
-
 text_sensor:
   !include include/text_sensors.yaml
 
@@ -96,19 +83,54 @@ sensor:
 
 select:
   - platform: template
-    id: select_schedule
+    id: schedule
     name: "Light Schedule"
-    options:
-      - "Disabled"
-      - "Grow"
-      - "Bloom"
+    options: ["Disabled", "Grow", "Bloom"]
+    initial_option: "Disabled"
+    optimistic: true
+    restore_value: true
+  - platform: template
+    id: temperature_max
+    name: "Temperature: Fan On"
+    options: ["21", "22", "23", "24", "25", "26", "27", "28", "29"]
+    initial_option: "27"
+    optimistic: true
+    restore_value: true
+  - platform: template
+    id: temperature_hysteresis
+    name: "Temperature: Fan Hysteresis"
+    options: ["1", "2", "3", "4", "5"]
+    initial_option: "2"
+    optimistic: true
+    restore_value: true
+  - platform: template
+    id: temperature_min
+    name: "Temperature: Fan Off"
+    options: ["19", "20", "21", "22", "23", "24", "25"]
+    initial_option: "21"
+    optimistic: true
+    restore_value: true
+  - platform: template
+    id: humidity_max
+    name: "Humidity: Fan On"
+    options: ["40", "45", "50", "55", "60", "65", "70", "75"]
+    initial_option: "70"
     optimistic: true
     restore_value: true
-    on_value: 
-      then:
-        - lambda: |-
-            auto index = id(select_schedule).active_index();
-            id(schedule) = index.value();
+  - platform: template
+    id: humidity_hysteresis
+    name: "Humidity: Fan Hysteresis"
+    options: ["5", "10", "15"]
+    initial_option: "5"
+    optimistic: true
+    restore_value: true
+  - platform: template
+    id: humidity_min
+    name: "Humidity: Fan Off"
+    options: ["20", "25", "30", "35", "40", "45", "50"]
+    initial_option: "40"
+    optimistic: true
+    restore_value: true            
 
 switch:
   - platform: restart
@@ -151,28 +173,28 @@ time:
       - cron: ${schedule_grow_on}
         then:
           - lambda: |-
-              if (id(schedule) == 1) {
+              if (id(schedule).state == "Grow") {
                 id(propbox_light).turn_on();
                 id(propbox_light).publish_state(true);
               }
       - cron: ${schedule_grow_off}
         then:
           - lambda: |-
-              if (id(schedule) == 1) {
+              if (id(schedule).state == "Grow") {
                 id(propbox_light).turn_off();
                 id(propbox_light).publish_state(false);
               }
       - cron: ${schedule_bloom_on}
         then:
           - lambda: |-
-              if (id(schedule) == 2) {
+              if (id(schedule).state == "Bloom") {
                 id(propbox_light).turn_on();
                 id(propbox_light).publish_state(true);
               }
       - cron: ${schedule_bloom_off}
         then:
           - lambda: |-
-              if (id(schedule) == 2) {
+              if (id(schedule).state == "Bloom") {
                 id(propbox_light).turn_off();
                 id(propbox_light).publish_state(false);
               }
@@ -184,8 +206,8 @@ script:
           // switch fan on when temperature/humidity is above the limit, unless humidity/temperature is below the limit
           if (
             !id(propbox_fan).state &&
-            (id(temperature).state >= ${temperature_max} && id(humidity).state    >= ${humidity_min}) ||
-            (id(humidity).state    >= ${humidity_max}    && id(temperature).state >= ${temperature_min})
+            (id(temperature).state >= atoi(id(temperature_max).state.c_str()) && id(humidity).state    >= atoi(id(humidity_min).state.c_str())) ||
+            (id(humidity).state    >= atoi(id(humidity_max).state.c_str())    && id(temperature).state >= atoi(id(temperature_min).state.c_str()))
           ) {
             id(propbox_fan).turn_on();
             id(propbox_fan).publish_state(true);
@@ -194,8 +216,8 @@ script:
           // switch fan off when temperature and humidity are below the limits minus hysteresis
           else if (
             id(propbox_fan).state &&
-            id(temperature).state <= ${temperature_max} - ${temperature_hysteresis} && 
-            id(humidity).state    <= ${humidity_max}    - ${humidity_hysteresis}
+            id(temperature).state <= atoi(id(temperature_max).state.c_str()) - atoi(id(temperature_hysteresis).state.c_str()) && 
+            id(humidity).state    <= atoi(id(humidity_max).state.c_str())    - atoi(id(humidity_hysteresis).state.c_str())
           ) {
             id(propbox_fan).turn_off();
             id(propbox_fan).publish_state(false);
-- 
GitLab