-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsauna-controller.yaml
More file actions
1715 lines (1460 loc) · 52.7 KB
/
Copy pathsauna-controller.yaml
File metadata and controls
1715 lines (1460 loc) · 52.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ================================
# Substitutions / build-time constants
# ================================
substitutions:
devicename: "Sauna Controller"
internalname: "sauna-controller"
power_of_sauna_oven: "6" # Total installed power (kW), informational only
mqtt_host: !secret mqtt_host
mqtt_port: !secret mqtt_port
mqtt_username: !secret mqtt_username
mqtt_password: !secret mqtt_password
mqtt_topic_base: "sauna/${internalname}"
# Contactor-friendly time-base for windowed control
contactor_window_s: "180" # Window length (s) used to approximate PID power with ON/OFF
contactor_min_on_s: "45" # Minimum ON time (s) inside a window
contactor_min_off_s: "45" # Minimum OFF time (s) inside a window
# ================================
# ESPHome core
# ================================
esphome:
name: "${internalname}"
friendly_name: "${devicename}"
min_version: 2025.7.5
name_add_mac_suffix: false
# On boot: force climate OFF. Outputs are kept OFF until sensors are valid.
on_boot:
priority: 200
then:
- logger.log: "Boot: forcing sauna climate OFF"
- switch.turn_off: sauna_contactor_hw
- output.turn_off: destrat_output
- output.turn_off: light_output
- output.turn_off: amp_output
- climate.control:
id: sauna_climate
mode: 'OFF'
- lambda: |-
id(boost_active) = false;
id(session_start) = 0;
id(boost_start) = 0;
# ================================
# MCU / framework
# ================================
esp32:
board: esp32-s3-devkitc-1
variant: esp32s3
framework:
type: esp-idf
# PSRAM configuration (ESP32-S3-WROOM-1-N16R8)
psram:
mode: octal
speed: 80MHz
# Logging
logger:
level: WARN
logs:
sensor: ERROR
text_sensor: ERROR
climate: ERROR
binary_sensor: ERROR
# Home Assistant API
api:
encryption:
key: !secret api_key
reboot_timeout: 0s
# OTA updates
ota:
- platform: esphome
id: my_ota
password: !secret ota_password
# Wi-Fi / networking
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: 192.168.1.36
gateway: 192.168.1.1
subnet: 255.255.255.0
dns1: 8.8.8.8
dns2: 8.8.4.4
ap:
ssid: "Sauna Fallback Hotspot"
password: "JS8pluyHIW5q"
reboot_timeout: 0s
power_save_mode: none
# Captive portal for fallback AP
captive_portal:
# ================================
# Message encoding
# ================================
json:
mqtt:
broker: ${mqtt_host}
port: ${mqtt_port}
username: ${mqtt_username}
password: ${mqtt_password}
certificate_authority: !secret mqtt_ca_cert
topic_prefix: ${mqtt_topic_base}
discovery: false
log_topic:
topic: ${mqtt_topic_base}/log
level: WARN
birth_message:
topic: ${mqtt_topic_base}/state
payload: online
will_message:
topic: ${mqtt_topic_base}/state
payload: offline
on_json_message:
- topic: ${mqtt_topic_base}/cmd
then:
- lambda: |-
int cmd_id = x["cmd_id"] | 0;
const char *type = x["type"] | "";
JsonObjectConst payload = x["payload"].as<JsonObjectConst>();
if (cmd_id > 0 && cmd_id <= id(last_ack_id)) {
// Ignore duplicates when QoS retries or reconnect delivery occurs.
return;
}
if (strcmp(type, "set_climate") == 0) {
auto call = id(sauna_climate).make_call();
if (!payload["mode"].isNull()) {
const char *mode = payload["mode"] | "OFF";
if (strcmp(mode, "HEAT") == 0) {
call.set_mode(climate::CLIMATE_MODE_HEAT);
} else {
call.set_mode(climate::CLIMATE_MODE_OFF);
}
}
if (!payload["setpoint_c"].isNull()) {
call.set_target_temperature((float) payload["setpoint_c"].as<float>());
}
call.perform();
} else if (strcmp(type, "set_fan") == 0) {
bool destrat = payload["destrat"] | false;
if (destrat) id(fan_destrat).turn_on();
else id(fan_destrat).turn_off();
} else if (strcmp(type, "set_light") == 0) {
bool on = payload["on"] | false;
auto lcall = on ? id(sauna_light).turn_on() : id(sauna_light).turn_off();
lcall.perform();
} else if (strcmp(type, "set_amp") == 0) {
bool on = payload["on"] | false;
if (on) id(sauna_amp).turn_on();
else id(sauna_amp).turn_off();
} else if (strcmp(type, "set_number") == 0) {
const char *name = payload["name"] | "";
if (payload["value"].isNull()) {
ESP_LOGW("mqtt", "set_number without value");
return;
}
float value = payload["value"].as<float>();
if (strcmp(name, "boost_window") == 0) {
auto call = id(boost_window).make_call();
call.set_value(value);
call.perform();
} else if (strcmp(name, "boost_exit") == 0) {
auto call = id(boost_exit).make_call();
call.set_value(value);
call.perform();
} else if (strcmp(name, "max_session_min") == 0) {
auto call = id(max_session_min).make_call();
call.set_value(value);
call.perform();
} else if (strcmp(name, "fan_dt_sp") == 0) {
auto call = id(fan_dt_sp).make_call();
call.set_value(value);
call.perform();
} else {
ESP_LOGW("mqtt", "Unknown set_number target: %s", name);
return;
}
} else if (strcmp(type, "set_select") == 0) {
const char *name = payload["name"] | "";
const char *option = payload["option"] | "";
if (strcmp(name, "control_temp_source") == 0) {
auto call = id(control_temp_source).make_call();
call.set_option(option);
call.perform();
} else {
ESP_LOGW("mqtt", "Unknown set_select target: %s", name);
return;
}
} else if (strcmp(type, "press_button") == 0) {
const char *name = payload["name"] | "";
if (strcmp(name, "restart") == 0) {
id(restart_controller_btn).press();
} else if (strcmp(name, "reset_energy") == 0) {
id(reset_energy_btn).press();
} else if (strcmp(name, "reset_runtime") == 0) {
id(reset_runtime_btn).press();
} else if (strcmp(name, "reset_eta") == 0) {
id(reset_eta_btn).press();
} else if (strcmp(name, "reset_thermal") == 0) {
id(reset_thermal_btn).press();
} else {
ESP_LOGW("mqtt", "Unknown press_button target: %s", name);
return;
}
} else {
ESP_LOGW("mqtt", "Unknown command type: %s", type);
return;
}
if (cmd_id > id(last_ack_id)) {
id(last_ack_id) = cmd_id;
}
ESP_LOGI("mqtt", "Command applied (id=%d, type=%s)", cmd_id, type);
- script.execute: publish_telemetry
# ================================
# 1-Wire buses (separate GPIOs)
# ================================
one_wire:
- platform: gpio
pin: GPIO4
id: ow_top
- platform: gpio
pin: GPIO5
id: ow_head
- platform: gpio
pin: GPIO6
id: ow_under
- platform: gpio
pin: GPIO7
id: ow_outdoor
# ================================
# Outputs (GPIO / PWM)
# ================================
output:
- platform: gpio
id: contactor_gpio
pin: GPIO15
inverted: true
- platform: gpio
id: destrat_output
pin: GPIO16
inverted: true
- platform: gpio
id: light_output
pin: GPIO17
inverted: true
- platform: gpio
id: amp_output
pin: GPIO18
inverted: true
- platform: template
id: pid_heat_output
type: float
write_action:
- lambda: |-
// Store PID duty request (0..1). Windowed contactor logic uses this.
id(pid_duty) = state;
# ================================
# Sensors
# ================================
sensor:
# Primary measurements
- platform: dallas_temp
one_wire_id: ow_top
id: t_top
name: "Sauna Temp Top"
unit_of_measurement: "°C"
accuracy_decimals: 1
device_class: temperature
state_class: measurement
update_interval: 2s
- platform: dallas_temp
one_wire_id: ow_head
id: t_head
name: "Sauna Temp Head"
unit_of_measurement: "°C"
accuracy_decimals: 1
device_class: temperature
state_class: measurement
update_interval: 2s
- platform: dallas_temp
one_wire_id: ow_under
id: t_under
name: "Sauna Temp Under Bench"
unit_of_measurement: "°C"
accuracy_decimals: 1
device_class: temperature
state_class: measurement
update_interval: 2s
- platform: dallas_temp
one_wire_id: ow_outdoor
id: t_outdoor
name: "Sauna Temp Outdoor"
unit_of_measurement: "°C"
accuracy_decimals: 1
device_class: temperature
state_class: measurement
update_interval: 10s
# Derived temperatures
- platform: template
id: t_control
name: "Sauna Control Temp"
unit_of_measurement: "°C"
accuracy_decimals: 1
update_interval: 1s
lambda: |-
std::string mode = id(control_temp_source).current_option().c_str();
if (mode.empty()) mode = "Average (Top+Head)";
float top = id(t_top).state;
float head = id(t_head).state;
if (mode == "Top") {
if (isnan(top)) return NAN;
return top;
}
if (mode == "Head") {
if (isnan(head)) return NAN;
return head;
}
// Default: Average (Top+Head)
if (isnan(top) || isnan(head)) return NAN;
return (top + head) / 2.0f;
- platform: template
id: d_strat
name: "Sauna Stratification Delta"
unit_of_measurement: "°C"
accuracy_decimals: 1
update_interval: 1s
lambda: |-
if (isnan(id(t_top).state) || isnan(id(t_under).state))
return NAN;
return id(t_top).state - id(t_under).state;
# Control diagnostics
- platform: template
name: "Sauna PID Duty"
entity_category: diagnostic
unit_of_measurement: "%"
accuracy_decimals: 1
update_interval: 5s
lambda: |-
return id(pid_duty) * 100.0f;
# Energy estimation (no physical power meter)
- platform: template
id: sauna_power_w
name: "Sauna Power"
unit_of_measurement: "W"
device_class: power
state_class: measurement
accuracy_decimals: 0
update_interval: 1s
lambda: |-
return id(contactor_on) ? (float)(${power_of_sauna_oven}) * 1000.0f : 0.0f;
- platform: integration
name: "Sauna Energy (Wh)"
id: sauna_energy_wh
entity_category: diagnostic
sensor: sauna_power_w
time_unit: h
unit_of_measurement: "Wh"
device_class: energy
state_class: total_increasing
accuracy_decimals: 1
- platform: template
name: "Sauna Energy"
id: sauna_energy_kwh
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
accuracy_decimals: 3
update_interval: 5s
lambda: |-
return id(sauna_energy_wh).state / 1000.0f;
# Runtime counters
- platform: template
name: "Sauna Oven Runtime"
id: oven_runtime_h
unit_of_measurement: "h"
device_class: duration
state_class: total_increasing
accuracy_decimals: 2
update_interval: 10s
lambda: |-
return id(oven_runtime_s) / 3600.0f;
- platform: template
name: "Sauna Fan Runtime"
id: fan_runtime_h
unit_of_measurement: "h"
device_class: duration
state_class: total_increasing
accuracy_decimals: 2
update_interval: 10s
lambda: |-
return id(fan_runtime_s) / 3600.0f;
- platform: template
name: "Sauna Light Runtime"
id: light_runtime_h
unit_of_measurement: "h"
device_class: duration
state_class: total_increasing
accuracy_decimals: 2
update_interval: 10s
lambda: |-
return id(light_runtime_s) / 3600.0f;
- platform: template
name: "Sauna Amplifier Runtime"
id: amp_runtime_h
unit_of_measurement: "h"
device_class: duration
state_class: total_increasing
accuracy_decimals: 2
update_interval: 10s
lambda: |-
return id(amp_runtime_s) / 3600.0f;
# ETA model (auto-calibrated)
- platform: template
name: "ETA Model A (auto)"
id: eta_A_sensor
entity_category: diagnostic
unit_of_measurement: "°C/min"
accuracy_decimals: 3
update_interval: 10s
lambda: |-
return id(eta_A_est);
- platform: template
name: "ETA Model B (auto)"
id: eta_B_sensor
entity_category: diagnostic
unit_of_measurement: "°C/min/°C"
accuracy_decimals: 4
update_interval: 10s
lambda: |-
return id(eta_B_est);
- platform: template
name: "Sauna ETA to Setpoint (auto)"
id: sauna_eta_min
unit_of_measurement: "min"
accuracy_decimals: 0
update_interval: 10s
lambda: |-
if (id(sauna_climate).mode != climate::CLIMATE_MODE_HEAT) return NAN;
if (isnan(id(t_control).state) || isnan(id(t_outdoor).state)) return NAN;
float sp = id(sauna_climate).target_temperature;
float t = id(t_control).state;
float Tout = id(t_outdoor).state;
float remaining = sp - t;
if (remaining <= 0.0f) return 0.0f;
float rate = id(eta_A_est) + id(eta_B_est) * Tout;
if (rate < 0.05f) return NAN;
float eta = remaining / rate;
if (eta > 300.0f) eta = 300.0f;
return eta;
- platform: template
name: "ETA Samples"
id: eta_samples
entity_category: diagnostic
unit_of_measurement: "samples"
accuracy_decimals: 0
update_interval: 10s
lambda: |-
return (float) id(eta_n);
- platform: template
name: "ETA Fit RMSE"
id: eta_rmse
entity_category: diagnostic
unit_of_measurement: "°C/min"
accuracy_decimals: 3
update_interval: 10s
lambda: |-
uint32_t n_u = id(eta_n);
if (n_u < 30) return NAN;
float n = (float)n_u;
float Sx = id(eta_Sx);
float Sy = id(eta_Sy);
float Sxx = id(eta_Sxx);
float Sxy = id(eta_Sxy);
float Syy = id(eta_Syy);
float den = (n * Sxx - Sx * Sx);
if (fabsf(den) < 1e-6f) return NAN;
// Least-squares A,B
float B = (n * Sxy - Sx * Sy) / den;
float A = (Sy - B * Sx) / n;
// SSE = Σ(y^2) - 2AΣy - 2BΣxy + A^2 n + 2AB Σx + B^2 Σ(x^2)
float SSE = Syy
- 2.0f * A * Sy
- 2.0f * B * Sxy
+ (A * A) * n
+ 2.0f * A * B * Sx
+ (B * B) * Sxx;
if (SSE < 0.0f) SSE = 0.0f;
float dof = n - 2.0f;
if (dof <= 1.0f) return NAN;
float rmse = sqrtf(SSE / dof);
return rmse;
- platform: template
name: "ETA Confidence"
id: eta_confidence
entity_category: diagnostic
unit_of_measurement: "%"
accuracy_decimals: 0
update_interval: 10s
lambda: |-
uint32_t n_u = id(eta_n);
if (n_u < 10) return 0;
// Component 1: data sufficiency (0..1), saturates around 300 samples
float n = (float)n_u;
float data_score = n / 300.0f;
if (data_score > 1.0f) data_score = 1.0f;
// Component 2: fit quality from RMSE (0..1)
float rmse = id(eta_rmse).state;
if (isnan(rmse)) rmse = 1.0f;
// Map RMSE to score: <=0.10 °C/min => great, >=0.50 => poor
float fit_score;
if (rmse <= 0.10f) fit_score = 1.0f;
else if (rmse >= 0.50f) fit_score = 0.0f;
else fit_score = 1.0f - (rmse - 0.10f) / (0.50f - 0.10f);
float conf = (0.55f * data_score + 0.45f * fit_score) * 100.0f;
if (conf < 0.0f) conf = 0.0f;
if (conf > 100.0f) conf = 100.0f;
return conf;
- platform: template
name: "ETA Predicted Heat Rate"
id: eta_rate_pred
entity_category: diagnostic
unit_of_measurement: "°C/min"
accuracy_decimals: 2
update_interval: 10s
lambda: |-
if (isnan(id(t_outdoor).state)) return NAN;
float Tout = id(t_outdoor).state;
float rate = id(eta_A_est) + id(eta_B_est) * Tout;
// clamp to sane limits
if (rate < 0.05f) rate = 0.05f;
if (rate > 5.00f) rate = 5.00f;
return rate;
- platform: template
name: "Sauna ETA to Setpoint (Predicted)"
id: sauna_eta_min_pred
unit_of_measurement: "min"
accuracy_decimals: 0
update_interval: 10s
lambda: |-
if (isnan(id(t_control).state) || isnan(id(t_outdoor).state)) return NAN;
float sp = id(sauna_climate).target_temperature; // setpoint from HA even if climate is OFF
float t = id(t_control).state;
float Tout = id(t_outdoor).state;
float remaining = sp - t;
if (remaining <= 0.0f) return 0.0f;
float rate = id(eta_A_est) + id(eta_B_est) * Tout;
if (rate < 0.05f) return NAN;
float eta = remaining / rate;
if (eta > 300.0f) eta = 300.0f;
return eta;
- platform: template
name: "ETA Learning Active"
entity_category: diagnostic
update_interval: 2s
lambda: |-
return (id(sauna_climate).mode == climate::CLIMATE_MODE_HEAT &&
!isnan(id(t_control).state) &&
!isnan(id(t_outdoor).state) &&
id(contactor_on)) ? 1 : 0;
# Cabin thermal model (online identification)
- platform: template
name: "Thermal Model Samples"
entity_category: diagnostic
unit_of_measurement: "samples"
accuracy_decimals: 0
update_interval: 10s
lambda: |-
return (float)id(th_n);
- platform: template
name: "Sauna UA (Loss Coefficient)"
entity_category: diagnostic
unit_of_measurement: "W/°C"
accuracy_decimals: 1
update_interval: 10s
lambda: |-
// UA = -c/a
if (id(th_a) <= 0.0) return NAN;
double UA = (-id(th_c)) / id(th_a);
if (UA < 0.0) return NAN;
return (float)UA;
- platform: template
name: "Sauna C (Thermal Capacity)"
entity_category: diagnostic
unit_of_measurement: "kJ/°C"
accuracy_decimals: 1
update_interval: 10s
lambda: |-
// C = 1/a (J/°C) -> kJ/°C
if (id(th_a) <= 0.0) return NAN;
double Cj = 1.0 / id(th_a);
if (Cj <= 0.0) return NAN;
return (float)(Cj / 1000.0);
- platform: template
name: "Sauna Tau (Time Constant)"
entity_category: diagnostic
unit_of_measurement: "min"
accuracy_decimals: 1
update_interval: 10s
lambda: |-
// tau = C/UA = -1/c (sec) -> min
if (id(th_c) >= -1e-9) return NAN; // c must be negative
double tau_s = -1.0 / id(th_c);
return (float)(tau_s / 60.0);
# Estimated heat losses (from UA and ΔT)
- platform: template
name: "Sauna Heat Loss"
id: sauna_heat_loss_w
unit_of_measurement: "W"
device_class: power
state_class: measurement
accuracy_decimals: 0
update_interval: 10s
lambda: |-
if (isnan(id(t_control).state) || isnan(id(t_outdoor).state)) return NAN;
if (id(th_a) <= 0.0) return NAN;
// UA = -c/a [W/°C]
double UA = (-id(th_c)) / id(th_a);
if (UA < 0.0) return NAN;
double dT = (double)(id(t_control).state - id(t_outdoor).state); // °C
if (dT < 0.0) dT = 0.0;
double Ploss = UA * dT; // W
if (Ploss < 0.0) return NAN;
return (float)Ploss;
- platform: template
name: "Sauna Heat Loss (kW)"
id: sauna_heat_loss_kw
unit_of_measurement: "kW"
device_class: power
state_class: measurement
accuracy_decimals: 2
update_interval: 10s
lambda: |-
if (isnan(id(sauna_heat_loss_w).state)) return NAN;
return id(sauna_heat_loss_w).state / 1000.0f;
- platform: template
name: "Sauna Heating Headroom (kW)"
id: sauna_heating_headroom_kw
unit_of_measurement: "kW"
device_class: power
state_class: measurement
accuracy_decimals: 2
update_interval: 10s
lambda: |-
if (isnan(id(sauna_heat_loss_w).state)) return NAN;
float headroom_w = (float)(${power_of_sauna_oven}) * 1000.0f - id(sauna_heat_loss_w).state;
return headroom_w / 1000.0f;
- platform: template
name: "Sauna Control Temp Valid"
id: control_temp_valid
entity_category: diagnostic
accuracy_decimals: 0
update_interval: 2s
lambda: |-
std::string mode = id(control_temp_source).current_option().c_str();
if (mode.empty()) mode = "Average (Top+Head)";
float top = id(t_top).state;
float head = id(t_head).state;
if (mode == "Top") return isnan(top) ? 0.0f : 1.0f;
if (mode == "Head") return isnan(head) ? 0.0f : 1.0f;
return (isnan(top) || isnan(head)) ? 0.0f : 1.0f;
binary_sensor:
- platform: template
name: "Sauna Contactor Active"
id: sauna_contactor_active
device_class: power
lambda: |-
return id(contactor_on);
- platform: template
name: "Sauna Sensor Fault"
id: sauna_sensor_fault
device_class: problem
entity_category: diagnostic
lambda: |-
return id(sensor_fault_active);
text_sensor:
- platform: template
name: "Sauna Control Temp Source (Active)"
id: control_temp_source_active
entity_category: diagnostic
update_interval: 5s
lambda: |-
std::string mode = id(control_temp_source).current_option().c_str();
if (mode.empty()) mode = "Average (Top+Head)";
return mode;
# ================================
# User parameters (HA: entity_category=config)
# ================================
number:
- platform: template
name: "Sauna Boost Window (°C)"
id: boost_window
entity_category: config
min_value: 5
max_value: 30
step: 1
initial_value: 15
optimistic: true
restore_value: true
- platform: template
name: "Sauna Boost Exit (°C)"
id: boost_exit
entity_category: config
min_value: 2
max_value: 15
step: 1
initial_value: 5
optimistic: true
restore_value: true
- platform: template
name: "Sauna Max Session (min)"
id: max_session_min
entity_category: config
min_value: 30
max_value: 240
step: 5
initial_value: 180
optimistic: true
- platform: template
name: "Sauna Fan dT Setpoint (°C)"
id: fan_dt_sp
entity_category: config
min_value: 1
max_value: 250
step: 0.5
initial_value: 50
optimistic: true
restore_value: true
select:
- platform: template
name: "Sauna Control Temp Source"
id: control_temp_source
entity_category: config
optimistic: true
restore_value: true
options:
- "Average (Top+Head)"
- "Top"
- "Head"
initial_option: "Average (Top+Head)"
# Switch entities
switch:
# Expose the heater contactor as a HA switch (drives the GPIO output above)
- platform: output
name: "Sauna Contactor"
id: sauna_contactor_hw
output: contactor_gpio
restore_mode: ALWAYS_OFF
internal: true
- platform: output
name: "Sauna Destratification Fan"
id: fan_destrat
output: destrat_output
restore_mode: ALWAYS_OFF
- platform: output
name: "Sauna Amplifier Power"
id: sauna_amp
output: amp_output
restore_mode: ALWAYS_OFF
# Light entity
light:
- platform: binary
id: sauna_light
name: "Sauna Light"
output: light_output
restore_mode: ALWAYS_OFF
# Maintenance / reset buttons (HA: entity_category=diagnostic)
button:
- platform: restart
id: restart_controller_btn
name: "Sauna Restart"
- platform: template
id: reset_energy_btn
name: "Reset Sauna Energy"
entity_category: diagnostic
on_press:
- sensor.integration.reset: sauna_energy_wh
- platform: template
id: reset_runtime_btn
name: "Reset Sauna Runtime Counters"
entity_category: diagnostic
on_press:
- lambda: |-
id(oven_runtime_s) = 0;
id(fan_runtime_s) = 0;
id(light_runtime_s) = 0;
id(amp_runtime_s) = 0;
- platform: template
id: reset_eta_btn
name: "Reset ETA Calibration"
entity_category: diagnostic
on_press:
- lambda: |-
id(eta_n) = 0;
id(eta_Sx) = 0.0f;
id(eta_Sy) = 0.0f;
id(eta_Sxx) = 0.0f;
id(eta_Sxy) = 0.0f;
id(eta_Syy) = 0.0f;
id(eta_A_est) = 1.20f;
id(eta_B_est) = 0.010f;
id(eta_last_ms) = 0;
id(eta_last_t) = NAN;
- platform: template
id: reset_thermal_btn
name: "Reset Thermal Model"
entity_category: diagnostic
on_press:
- lambda: |-
id(th_n) = 0;
id(th_S11) = 0; id(th_S22) = 0; id(th_S12) = 0;
id(th_T1) = 0; id(th_T2) = 0;
id(th_a) = 0; id(th_c) = 0;
id(th_last_ms) = 0;
id(th_last_tin) = NAN;
# ================================
# Internal state / model parameters
# ================================
globals:
# MQTT command ACK tracking
- id: last_ack_id
type: int
restore_value: yes
initial_value: "0"
- id: boost_active
type: bool
initial_value: "false"
- id: session_start
type: uint32_t
initial_value: "0"
- id: boost_start
type: uint32_t
initial_value: "0"
# PID duty request (0..1)
- id: pid_duty
type: float
initial_value: "0.0"
# Windowed control state
- id: window_start_ms
type: uint32_t
initial_value: "0"
- id: window_on_ms
type: uint32_t
initial_value: "0"
- id: contactor_on
type: bool
initial_value: "false"
- id: force_climate_off
type: bool
initial_value: "false"
- id: oven_runtime_s
type: uint32_t
restore_value: yes
initial_value: "0"
- id: fan_runtime_s
type: uint32_t
restore_value: yes
initial_value: "0"
- id: light_runtime_s
type: uint32_t
restore_value: yes
initial_value: "0"
- id: amp_runtime_s
type: uint32_t
restore_value: yes