Kill local globals and just use climate
authorDavid Woodhouse <dwmw@amazon.co.uk>
Thu, 3 Oct 2024 19:48:49 +0000 (20:48 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Thu, 3 Oct 2024 19:48:49 +0000 (20:48 +0100)
bathroomfloor.yaml

index 1a015a6847bd2b8f2bc5aabe2d1e985ded304b47..69da041fb1ea902a87add933b5624f3d44946e80 100644 (file)
@@ -51,26 +51,14 @@ wifi:
       bssid: !secret wndr3800_bssid
       priority: 1
 
-globals:
-  - id: relay_stable_time
-    type: time_t
-    initial_value: '0'
-
-  - id: temp_setpoint
-    type: float
-    initial_value: '21.0'
-    restore_value: true
-
-  - id: ufh_manual_delay
-    type: int
-    initial_value: '1800'
-    restore_value: true
-
-  - id: ufh_auto_delay
-    type: int
-    initial_value: '120'
-    restore_value: true
-
+script:
+  - id: back_to_auto
+    mode: restart
+    then:
+      - delay: 30 min
+      - climate.control:
+          id: ntc_climate
+          mode: HEAT
 
 time:
   - platform: sntp
@@ -100,7 +88,8 @@ mqtt:
       - delay: 2s # Too soon and the first messages don't get through!
       - lambda: |-
          id(tell_domo_nvalue)->execute(${domo_ufh}, id(ufh_relay).state);
-         id(tell_domo_nsvalues)->execute(${domo_thresh}, 2, std::to_string(id(temp_setpoint)));
+         id(tell_domo_svalue)->execute(${domo_thresh},
+                                       std::to_string(id(ntc_climate).target_temperature));
 
   on_disconnect:
     then:
@@ -121,23 +110,26 @@ mqtt:
                   } else {
                     id(ufh_relay).turn_off();
                   }
-                  // Delay for longer if turned on manually
-                  id(relay_stable_time) = ::time(NULL) + id(ufh_manual_delay);
+                  {
+                  auto offcall = id(ntc_climate).make_call();
+                  offcall.set_mode("OFF");
+                  offcall.perform();
+                  }
+                  id(back_to_auto).execute();
                   break;
 
                 case ${domo_thresh}: /* Temperature setpoint */
-                  id(temp_setpoint) = x["svalue1"].as<float>();
+                  double temp = x["svalue1"].as<double>();
                   auto call = id(ntc_climate).make_call();
                   call.set_mode("HEAT");
-                  call.set_target_temperature(id(temp_setpoint));
+                  call.set_target_temperature(temp);
                   call.perform();
-                  ESP_LOGD("mqtt", "Got temp_setpoint %f", id(temp_setpoint));
+                  ESP_LOGD("mqtt", "Got temp_setpoint %f", temp);
                   break;
             }
 
 light:
   - platform: status_led
-    internal: true
     pin: GPIO23
     id: blue_led
     restore_mode: ALWAYS_OFF
@@ -157,12 +149,10 @@ switch:
       then:
         - lambda: |-
             id(tell_domo_nvalue)->execute(${domo_ufh}, 1);
-            id(relay_stable_time) = ::time(NULL) + id(ufh_auto_delay);
     on_turn_off:
       then:
         - lambda: |-
             id(tell_domo_nvalue)->execute(${domo_ufh}, 0);
-            id(relay_stable_time) = ::time(NULL) + id(ufh_auto_delay + 30);
 
 sensor:
   - platform: ntc
@@ -232,15 +222,14 @@ climate:
     idle_action:
       - switch.turn_off: ufh_relay
     on_control:
-      - lambda: ESP_LOGI("CLIMATE", "on_control");
-    on_state:
       - lambda: |-
-         if (id(ntc_climate).target_temperature != id(temp_setpoint)) {
-           ESP_LOGI("UFH", "Tell domo target changed from %f °C to %f °C",
-                    id(temp_setpoint), id(ntc_climate).target_temperature);
-           id(temp_setpoint) = id(ntc_climate).target_temperature;
-           id(tell_domo_nsvalues)->execute(${domo_thresh}, 2, std::to_string(id(temp_setpoint)));
-         }
+           auto temp = x.get_target_temperature();
+           if (temp && temp.value() != id(ntc_climate).target_temperature) {
+             ESP_LOGI("CLIMATE", "on_control, set target %f to %f",
+                               id(ntc_climate).target_temperature, temp.value());
+             id(tell_domo_svalue)->execute(${domo_thresh},
+                                         std::to_string(temp.value()));
+           }
 
 
 interval: