-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp
More file actions
4014 lines (3611 loc) · 141 KB
/
temp
File metadata and controls
4014 lines (3611 loc) · 141 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:
device_name: "poolcontroller"
friendly_name: "pool"
#wifi domain to use, standard = .local
domain: .local
#domain: !secret domain_iot
#domain: !secret domain_pidri
ssid1_ap: !secret wifi_pidri
ssid1_pw: !secret wifi_pidri_pass
ssid2_ap: !secret wifi_autox
ssid2_pw: !secret wifi_autox_pass
#GPIO definitions
pin_relay1: GPIO25
pin_relay2: GPIO26
pin_relay3: GPIO33
pin_relay4: GPIO32
ledwifi: GPIO04
buzzer: GPIO12
txd0: GPIO01 #UART Tuya Reader (not needed actually)
rxd0: GPIO03 #UART Tuya Reader
txd1: GPIO17 #UART Modbus RS485
rxd1: GPIO16 #UART Modbus RS485
pwmh_hbridge: GPIO13 #s2cs - brought outside to screw header as I/O
spics0: GPIO05
spiclk: GPIO18
spimiso: GPIO19
spimosi: GPIO23
i2csda: GPIO21
i2cscl: GPIO22
i2cint: GPIO02
prog: GPIO00 #Für Flash auf GND ziehen, EN kurz auf GND ziehen danach wieder loslassen (3,3V)
#Input_only GPIOs - All Screw Header
yfdn40: GPIO34
yfs201: GPIO35
phtank_reset: GPIO36
maintenance: GPIO39
#Input/Output GPIO - All Screw Header
phase_hbridge: GPIO14
onewire: GPIO15
inout16: GPIO16
inout27 : GPIO27 #GPIO27 / RF433
#Code Subs
modbusid_freq_inv: modbus_freq_inv
modbusid_heat_pump: modbus_sunway
modbusid_energy_meter: meter_ds100
name_pzem17: Elektrolyse
################Die goldene Regel der Prioritäten###############################
################################################################################
# In ESPHome gilt: Höhere Zahlen werden zuerst ausgeführt.
# 800.0 (WLAN/Netzwerk)
# 600.0 (Core)
# 200.0 (Standard Sensoren)
# 40.0 (UART Bus Default) Die Hardware-Schnittstelle muss bereit sein.
# 40.0 (Modbus Hub Default) Der Controller, der die Kommunikation verwaltet und zeitgleich mit UART startet
# 40.0 (Modbus Sensor Default) Erbt die Priorität des Controllers.
# -15.0 (Dein Ziel für on_boot)
# Für den Poolcontroller folgendes:
# 800.0 (WLAN/Netzwerk)
# 600.0 (Core)
# 200.0 (Standard Sensoren)
# 40.0 (UART Bus) Die Hardware-Schnittstelle muss bereit sein.
# 35.0 (Modbus Hub) Der Controller, der die Kommunikation verwaltet und zeitgleich mit UART startet
# 30.0 (Modbus Sensor) Erbt die Priorität des Controllers.
# 10.0 (Dein Ziel für on_boot)
# Zentrale Verwaltung der Prioritäten
uart_prior: "40.0"
modbushub_prior: "35.0"
modbusctrl_prior: "30.0"
on_boot_prior: "10.0"
#Update frequency und command throttle für Modbus
#command_throttle (Optional, Time): minimum time in between 2 requests to the device. Default is 0ms.
#update_interval (Optional, Time): The interval that the sensors should be checked. Defaults to 60 seconds.
throttle_modbus_heatpump: "200ms"
upd_int_modbus_heatpump: "60s"
throttle_modbus_energy_meter: "5s"
upd_int_modbus_energy_meter: "60s"
# #Poolcontroller-Logics Intial Values
checkIntervalORP: "2 min" #Angabe nach der Zahl ist wichtig!!
checkIntervalPH: "15 min" #Angabe nach der Zahl ist wichtig!!
esphome:
name: "${friendly_name}"
project:
name: "autox86.poolparty"
version: "1.0.1"
on_boot:
priority: ${on_boot_prior}
then:
- script.execute: script_on_boot
esp32:
board: esp-wrover-kit
framework:
#type: arduino
type: esp-idf
external_components:
- source: github://hostcc/esphome-component-dynamic-on-time
# - source: github://autox86/esphome-component-dynamic-on-time
# - source: github://esphome/esphome@2025.7.3
# components: [uart]
# refresh: 1h
# - source: github://esphome/esphome@2023.12.1
# components: [tuya]
# refresh: 1h
status_led:
#LED-Zustand Bedeutung
# Blinken langsam -> WLAN wird gesucht
# Blinken schnell -> Verbindung zu API/MQTT noch nicht fertig
# Dauerlicht AN -> WLAN + API/MQTT OK
# Aus -> Gerät ist aus / GPIO invertiert
pin:
number: ${ledwifi}
inverted: true
output:
- platform: ledc
pin: ${buzzer}
id: rtttl_out
rtttl:
output: rtttl_out
#Must be activated to enter safe mode after 10 failed normal boot
safe_mode:
######################################################################
# Pool
# - Volumen: 45 m³
# - Salzwasserpool, Salzkonzentration Zodiac LM2: 3200–4000 ppm
# - Temperatur Referenz: 25 °C
# - Idealer pH-Wert: 7.2
# Poolpumpe
# - Förderleistung bei 100%: 12 m³/h
# - Frequenz über Inverter steuerbar
# Chlorinator / Elektrolyse
# - Zodiac LM2-40, max 40 g/h Chlor
# - ORP Zielwert: 750 mV
# - Chlorbedarf wird über ORP Δ ermittelt
# PH-Dispenser
# - Maximal pro Zyklus: 100 ml
# - Maximal pro Tag: 300 ml
# - Tankkapazität: 20 L
######################################################################
globals:
- id: glo_flow_meter_min
type: float
initial_value: '200.0' #Minimum was an Durchfluss des Meter herrscht, sonst beendet die Pumpe
- id: glo_flow_pump_min
type: float
initial_value: '200.0' #Minimum was an Durchfluss der Pumpe herrscht, sonst beendet die Pumpe
restore_value: true
- id: glo_pzem_min
type: float
initial_value: '1.5' #Minimum was an Strom fließen muss, sonst beendet die Elektrolyse ()
restore_value: true
- id: glo_chlor_fail_count
type: int
initial_value: '0' # Variable für die Fehlerfall Elektrolyse
restore_value: false
- id: glo_chlor_error_lockout
type: bool
initial_value: 'false' # Variable für die Fehlerfall Elektrolyse
restore_value: false
- id: glo_tuya_toggle_count
type: int
initial_value: '0' # Variable für die Fehlerfall Tuya
restore_value: false
- id: glo_tuya_error_lockout
type: bool
initial_value: 'false' # Variable für die Fehlerfall Tuya
restore_value: false
- id: glo_tuya_last_reset_ms
type: uint32_t
initial_value: '0' # Variable für die Fehlerfall Tuya
restore_value: false
- id: glo_need_salt
type: bool
initial_value: 'false' #brauchen wir Salz?
restore_value: false
- id: glo_low_salt_limit
type: float
initial_value: '7.5' # Bei 7.5A wird ein LowSalt Alarm ausgelöst
restore_value: true
- id: glo_workmode
type: std::string
initial_value: '""' # Workmode in variabel
restore_value: true
- id: glo_frost_start_time
type: uint32_t
initial_value: '0'
- id: glo_frost_active
type: bool
initial_value: 'false' #frostschutz aktiv?
restore_value: false
- id: last_switch_action
type: uint32_t
initial_value: '0' #Used for storing a switch action, (see switch_poolpump)
restore_value: false
- id: glo_restart_allowed
type: bool
initial_value: 'false' #In case there was a restart performed, unexpected or not we indicate if we shall restart the pump
restore_value: false
- id: glo_last_reset_reason
type: std::string
initial_value: '"Initialwert"'
restore_value: false
- id: boot_log_buffer
type: std::string
initial_value: '""'
- id: glo_uart_priority
type: float
initial_value: '40.0' # Priority
restore_value: false
- id: glo_maintenance
type: bool
restore_value: true
initial_value: 'false' #Haben wir den Workmode maintenance?
- id: glo_modbushub_priority
type: float
initial_value: '35.0' # Priority
restore_value: false
- id: glo_modbussensor_priority
type: float
initial_value: '30.0' # Priority
restore_value: false
- id: glo_on_boot_priority
type: float
initial_value: '10.0' # Priority
restore_value: false
- id: glo_pump_active_via_automation
type: bool
restore_value: true
initial_value: 'false' #Wurde die Pumpe via Intervall gestartet?
- id: glo_ph_allowed
type: bool
restore_value: true
initial_value: 'false' #PH Erlaubt? Sicher ist sicher, PH Injektor initial auf false
- id: glo_orp_allowed
type: bool
restore_value: true
initial_value: 'false' #Sicher ist sicher, Chlorinator initial auf false
- id: glo_hp_allowed
type: bool
restore_value: true
initial_value: 'false' #Sicher ist sicher, HeatPump initial auf false
- id: glo_tuya_allowed
type: bool
restore_value: true
initial_value: 'false' #Sicher ist sicher, HeatPump initial auf false
- id: use_flow_dn40
type: bool
restore_value: true
initial_value: 'true' #FlowSensor angeschlossen? true or false
- id: use_flow_s201
type: bool
restore_value: true
initial_value: 'true' #FlowSensor angeschlossen? true or false
- id: chlorinator_polarity
type: bool
initial_value: 'false' #Polarität für die Zelle merken (Umpolung beim Start)
restore_value: true
- id: chlorinator_runtime_seconds
type: int
initial_value: '0' #globale helfer variabel für Chlorproduktion
restore_value: false
- id: chlorinator_remaining_seconds
type: int
initial_value: '0' #globale helfer variabel für Chlorberechnung
restore_value: false
- id: chlor_needed
type: bool
initial_value: 'false' # Chlor nötig --> True / False
- id: chlorinator_max_reached
type: bool
initial_value: 'false' # Chlorinator max Laufzeit erreicht?
- id: chlor_run_time_max_day
type: float
initial_value: '480' # Minuten die der Chlorinator pro Tag maximal erreichen darf änderbar über Nummernfeld
restore_value: true
- id: chlor_run_time_max_cycle
type: float
initial_value: '30' # Minuten die der Chlorinator pro Zyklus maximal erreichen darf änderbar über Nummernfeld
restore_value: true
- id: chlorinator_today_run_time
type: float
initial_value: '0' # Tageslaufzeit in Sekunden (bereits gelaufen)
restore_value: false
- id: max_ph_ml_per_cycle
type: float
initial_value: '300' # ml die der Dispenser pro Zyklus maximal injezieren darf, änderbar über Nummernfeld
restore_value: true
- id: max_ph_ml_per_day
type: float
initial_value: '300' # ml die der Dispenser täglich maximal injezieren darf, änderbar über Nummernfeld
restore_value: true
- id: glo_ph_remaining_seconds
type: int
restore_value: false
initial_value: '0'
- id: glo_orp_max
type: float
initial_value: '800' # Der Maximal Wert für ORP
restore_value: true
- id: glo_orp_min
type: float
initial_value: '600' # Der Minimal Wert für ORP
restore_value: true
- id: glo_orp_target
type: int
initial_value: '750' #globale helfer variabel für Chlorberechnung
restore_value: true
- id: ph_cycles
type: float
initial_value: '1' # Anzahl an PH Laufzyklen pro Tag
- id: ph_locked
type: bool
initial_value: 'false' #Verhindert eine weitere PH Injezierung wenn Grenzwerte erreicht sind
- id: ph_max
type: float
initial_value: '7.8' # Maximaler PH Wert den das Wasser haben darf, änderbar über Nummernfeld
restore_value: true
- id: ph_max_injection_reached
type: bool
initial_value: 'false' # Check: Haben wir die maximale Tagesdosis an PH Minus erreicht?
restore_value: false
- id: ph_min
type: float
initial_value: '6.8' # Maximaler PH Wert den das Wasser haben darf, änderbar über Nummernfeld
restore_value: true
- id: ph_ml_per_second
type: float
initial_value: '0.7388' #ml die der Dispenser pro Sekunde injeziert, änderbar über Nummernfeld
restore_value: true
- id: ph_needed
type: bool
initial_value: 'false' # Check: PH Minus nötig?
- id: glo_ph_target
type: float
initial_value: '7.0' # PH wert der angestrebt wird.
restore_value: true
- id: ph_tank_capacity_ml
type: float
restore_value: true
initial_value: '20000' # Volumen des PH Behälters in ML - änderbar über Nummernfeld
- id: ph_tank_refill
type: bool
initial_value: 'false' # Tank leer, also nachfüllen?
restore_value: true
- id: ph_tank_remaining
type: float
initial_value: '20000' # Verbleibender Tankinhalt
restore_value: true
- id: ph_today_ml
type: float
initial_value: '0' # Tageszähler:PH Minus an dem heutigen Tag injeziert in ml
restore_value: true
- id: phdispenser_today_run_time
type: float
initial_value: '0' # Tgeszähler:PH Dispenser Laufzeit in sekunden
restore_value: false
- id: poolpump_ok
type: bool
initial_value: 'false' # CheckVariabel: Ist Poolpumpenvorlaufzeit erreicht ? True / False
- id: poolvolume
type: float
restore_value: false
initial_value: '45000' #Größe deines Pools in Liter, änderbar über Nummernfeld
- id: poolpump_flow_max
type: float
restore_value: false
initial_value: '12.0' #Durchfluss Poolpumpe bei 100%
- id: PumpPreRunTime
type: float
initial_value: '10' # Vorlaufzeit der Poolpumpe in Minuten angebe, änderbar über Nummernfeld
restore_value: true
- id: poolpump_start_time
type: unsigned long
initial_value: '0' #Rechenhilfe für PumpPreRunTime (Vorlaufzeit)
- id: glo_set_frequency
type: float
initial_value: '100' #globale variabel zum Konvertieren des Betriebsmodus (Select als String) zu einer Zahl
restore_value: true
- id: phdispenser_cycle_time
type: float
initial_value: '0' # Zähler für die Laufzeit des aktuellen PH-Zyklus in Sekunden
restore_value: false
- id: ph_last_cycle_ml
type: float
initial_value: '0' # Menge an PH Minus (ml) im letzten Zyklus, für Tank-Update beim Ausschalten
restore_value: false
- id: glo_timer_start_hour
type: int
initial_value: '9' # Speichert die Stunde des Timers
restore_value: true
- id: glo_timer_start_min
type: int
initial_value: '0' # Speichert die Minute des Timers
restore_value: true
- id: glo_timer_stopp_hour
type: int
initial_value: '9' # Speichert die Stunde des Timers
restore_value: true
- id: glo_timer_stopp_min
type: int
initial_value: '0' # Speichert die Minute des Timers
restore_value: true
- id: glo_tuya_last_seen_ms
type: uint32_t
initial_value: "0" # Zeit seit letztem Kontakt mit Tuya Controller
restore_value: false
- id: glo_tuya_online
type: bool
initial_value: 'false' # Wird über irgendeinen Tuya sensor befüllt
restore_value: false
- id: glo_tuya_uart_seen_recently
type: bool
initial_value: 'false' # Haben wir den Controller kürzlich noch gesehen?
restore_value: false
# https://esphome.io/components/esphome.html#adjusting-flash-writes
preferences:
#setting interval to 5 minutes since this defines writing to flash
#Created a specific save script that is executed for things that need
#to save more quickly.
flash_write_interval: 5min
################################################################################
############################### Intervale #######################################
################################################################################
############################Messungen/Status####################################
###Bei jedem Zyklus werden die Logikfunktionen getriggert und entsprechend Flags
###(globale Variablen) gesetzt. Zudem wird der Status in der Logausgabe angezeigt
################################################################################
interval:
- interval: 7s
then:
- if:
condition:
- time.has_time:
then:
- script.execute: script_tuya_watchdog #Online Status vom W2839
- delay: 50ms
- script.execute: script_check_PumpPreRunTime #Vorlaufzeit der Pumpe erreicht?
- delay: 50ms
- script.execute: script_check_chlor_needed #Überprüfe ob Chlorbedarf
- delay: 50ms
- script.execute: script_check_ph_needed #Überprüfe ob PH Bedarf
- delay: 50ms
- script.execute: script_status #Schreibe Status ins Log
- delay: 50ms
# Anstarten der Pumpe nach einen Restart des ESP32 / Frostschutz
- if:
condition:
- lambda: |-
float temp = id(sensor_outdoor_temp).state;
// Frostschutz aktivieren bei -5 Grad
if (!std::isnan(temp) && temp <= -5.0) {
if (!id(glo_frost_active)) {
id(glo_frost_active) = true;
id(glo_frost_start_time) = millis(); // Startzeit merken
ESP_LOGW("frost", "Frostschutz AKTIVIERT bei %.1f°C", temp);
}
}
// Starten wenn: Timer erlaubt ODER Frostschutz aktiv
return (id(bs_timer_allows_run).state || id(glo_frost_active))
&& id(glo_restart_allowed)
&& (id(inv_running).state != 1)
&& !id(glo_maintenance);
then:
- lambda: |-
if (!id(glo_frost_active)) {
id(glo_pump_active_via_automation) = true;
}
- script.execute: script_pumpstart
################################PH-Minus Interval###############################
###Bei jedem Zyklus wird das ph_locked flag zurückgesetzt um eine erneute
### PH Dosierung zu ermöglichen und die PH Dosierung gestartet
################################################################################
- interval: ${checkIntervalPH}
then:
- if:
condition:
- time.has_time
- lambda: return id(glo_ph_allowed); //falls nicht erlaubt durch Workmode, wird es nicht gestartet
- lambda: return !id(switch_relay2_ph).state;
- lambda: |-
if (id(use_flow_dn40)) {
return (id(flow_pump).state > id(glo_flow_pump_min) && (id(inv_running).state == 1));
}else{
return (id(inv_running).state == 1);
}
then:
- if:
condition:
- lambda: return id(poolpump_ok) && id(ph_needed) && !id(ph_max_injection_reached) && (id(ph_tank_remaining) > 500.0);
then:
- switch.turn_on: switch_phdosierpumpe
- lambda: |-
// 1. Zähler für den neuen Zyklus auf Null setzen
id(phdispenser_cycle_time) = 0;
id(ph_last_cycle_ml) = 0.0f;
// 2. Countdown berechnen (Volles Zyklus-Limit)
if (id(ph_ml_per_second) > 0) {
// Da wir oben genullt haben, ist die Restmenge einfach das volle Limit
id(glo_ph_remaining_seconds) = (int)(id(max_ph_ml_per_cycle) / id(ph_ml_per_second));
} else {
id(glo_ph_remaining_seconds) = 0;
}
ESP_LOGI("PH-Minus Interval", "PH Dosierung gestartet. Geplante Dauer: %d s", id(glo_ph_remaining_seconds));
id(txt_sens_status).publish_state("PH Dosierung läuft...");
id(script_status).execute(); // Damit das Web-UI sofort "Inaktiv" gegen den Countdown tauscht
else:
- lambda: |-
// Bestimme den Grund
std::string reason;
if (!id(poolpump_ok)) {
reason = "IntervalPH - Vorlaufzeitpumpe nicht erreicht";
} else if (!id(ph_needed)) {
reason = "IntervalPH - kein PH Bedarf (PH ok)";
} else if (id(ph_max_injection_reached)) {
reason = "IntervalPH - tägliches PH-Limit erreicht";
} else if (id(ph_tank_remaining) <= 500.0) {
reason = "IntervalPH - PH Tank fast leer --> Auffüllen und Reset drücken!";
id(ph_tank_refill) = true;
} else {
reason = "IntervalPH - Unbekannte Blockade (ggf. glo_ph_allowed)";
}
// Log-Ausgabe
id(txt_sens_status).publish_state(reason);
ESP_LOGW("PH-Minus Interval", "PH Dosierpumpe über Interval Start verhindert weil: %s", reason.c_str());
#########################ORP (Chlorinator) Interval#################################################
##Bei jedem Zyklus werden die Flags geprüft und nach bedarf der Chlorinator eingeschaltet##
##
###########################################################################################
- interval: ${checkIntervalORP}
then:
- if:
condition:
- time.has_time
- lambda: return id(glo_orp_allowed) && !id(glo_chlor_error_lockout); //SPERRE PRÜFEN
- lambda: return !id(switch_chlorinator).state;
- lambda: |-
bool flow_ok = true;
// Prüfe DN40 falls aktiviert
if (id(use_flow_dn40) && id(flow_pump).state <= id(glo_flow_pump_min)) flow_ok = false;
// Prüfe S201 falls aktiviert
if (id(use_flow_s201) && id(flow_meter).state <= id(glo_flow_meter_min)) flow_ok = false;
// Pumpe muss laufen UND (wenn Sensoren aktiv) muss Flow da sein
return (id(inv_running).state == 1) && flow_ok;
then:
- if:
condition:
- lambda: return id(poolpump_ok) && id(chlor_needed) && !id(chlorinator_max_reached);
then:
# Starte Ablauf: Chlorinator starten (mit Berechnung) + Countdown setzen
- lambda: |-
ESP_LOGI("ORP Interval", "Chlorinator über Zeitintervall angefragt.");
#- script.execute: script_chlorinator_by_interval
- switch.turn_on: switch_chlorinator
else:
- lambda: |-
// Bestimme den Grund
std::string reason;
if (!id(poolpump_ok)) {
reason = "IntervalORP - Vorlaufzeitpumpe nicht erreicht";
} else if (!id(chlor_needed)) {
reason = "IntervalORP - kein Chlor Bedarf (ORP ok)";
} else if (id(chlorinator_max_reached)) {
reason = "IntervalORP - tägliche ORP-Laufzeit erreicht";
} else if (id(use_flow_dn40) && id(flow_pump).state <= id(glo_flow_pump_min)) {
reason = "YF-DN40 - kein Durchfluss!";
} else if (id(use_flow_s201) && id(flow_meter).state <= id(glo_flow_meter_min)) {
reason = "YF-S201 - kein Durchfluss!";
} else {
reason = "IntervalORP - Unbekannte Blockade (ggf. glo_ph_allowed)";
}
// Log-Ausgabe
id(txt_sens_status).publish_state(reason);
ESP_LOGW("IntervalORP", "Chlorinator über Interval Start verhindert weil: %s", reason.c_str());
######################Sicherheitsinterval############################
#Das Interval prüft laufend ob der Chlorinator oder PH Dosierpumpe noch läuft
#Fehlerfall: Poolpumpe, Flow Sensor --> aus
#Chlorinator: Maximale Laufzeit (Tag) erreicht? --> aus
#PH: Maximale Menge pro Tag errreicht? --> aus
#Countdown aus Berechnung erreicht? --> aus
####################################################################################
- interval: 7s
then:
- if:
condition:
# Nur ausführen, wenn Chlor oder PH ODER Pumpe läuft - während Pumpe laut timer nicht laufen sollte
# (Wir erweitern die Bedingung: ...oder wenn Frostschutz noch aktiv ist)
- lambda: |-
return (id(switch_chlor_run_gpio).state || id(switch_relay2_ph).state) || !id(bs_timer_allows_run).state || id(glo_frost_active);
then:
- lambda: |-
// --- 0. FROSTSCHUTZ LOGIK ---
if (id(glo_frost_active)) {
float temp = id(sensor_outdoor_temp).state;
// Frostschutz NUR deaktivieren, wenn es wärmer als -2 Grad ist
if (!std::isnan(temp) && temp >= -2.0) {
id(glo_frost_active) = false;
ESP_LOGW("safety-interval", "Frostschutz-Status beendet (Temp: %.1f°C)", temp);
}
}
// --- A. GEMEINSAME SICHERHEIT ---
if (id(inv_running).state != 1 || (id(use_flow_dn40) && id(flow_pump).state <= id(glo_flow_pump_min)) || (id(use_flow_s201) && id(flow_meter).state <= id(glo_flow_meter_min))) {
ESP_LOGW("safety-interval", "Pumpe aus oder kein Durchfluss YF-DN40 / S201 --> STOPP -> Alles aus.");
id(script_stopp_all).execute();
return;
}
// --- B. CHLORINATOR LOGIK ---
if (id(switch_chlor_run_gpio).state) {
id(chlorinator_today_run_time) += 7;
id(chlorinator_remaining_seconds) -= 7;
// Prüfung auf:
// 1. Ziel-ORP erreicht
// 2. Zeit abgelaufen
// 3. Tagesmaximum erreicht
bool orp_reached = (id(orp_measvalue).state >= id(glo_orp_target));
bool daily_max = (id(chlorinator_today_run_time) >= (id(chlor_run_time_max_day) * 60));
bool timer_done = (id(chlorinator_remaining_seconds) <= 0);
if (orp_reached || daily_max || timer_done) {
// Grund ermitteln (const char* ist sicher für ESP-IDF)
const char* reason = "Unbekannt";
//std::string reason = "Unbekannt";
if (orp_reached) {
reason = "Ziel-ORP erreicht = orp_measvalue";
} else if (daily_max) {
reason = "Tagesmaximum erreicht = chlorinator_today_run_time";
} else if (timer_done) {
reason = "Zyklus-Zeit abgelaufen = chlorinator_remaining_seconds";
}
ESP_LOGW("safety-interval", "Chlorinator STOP | Grund: %s", reason);
// Zusatz-Info im Log für die Analyse (Optional)
ESP_LOGD("safety-interval", "Status beim Stop: ORP: %.1f, Heute: %.0fs, Rest: %ds",
id(orp_measvalue).state, id(chlorinator_today_run_time), id(chlorinator_remaining_seconds));
id(switch_chlorinator).turn_off();
id(chlorinator_remaining_seconds) = 0;
}
// --- NEU: Stromfluss-Überwachung im Betrieb
if (!id(switch_chlor_debug).state) {
// Wenn der Strom unter e.g. 1.5A liegt ODER der PZEM gar nicht online ist
if (!id(bs_pzem_online).state || id(pzem_dc_current).state < id(glo_pzem_min)) {
id(glo_chlor_fail_count)++;
if (id(glo_chlor_fail_count) >= 2) {
id(glo_chlor_error_lockout) = true;
id(bs_chlor_lockout).publish_state(true);
ESP_LOGE("chlor_watchdog", "SICHERHEITSSPERRE: Chlorinator Hardware defekt?");
// WICHTIG: Sofort stoppen, damit GPIOs nicht auf "An" bleiben
id(script_stopp_chlor).execute();
}
} else {
// Erfolg: Zähler zurücksetzen
id(glo_chlor_fail_count) = 0;
}
}
}
// --- C. pH-DOSIER LOGIK (Verbesserte Sicherheitsprüfung) ---
if (id(switch_relay2_ph).state) {
float flow_rate = id(ph_ml_per_second);
// 1. Aktuelle Menge berechnen BEVOR wir die Zeit erhöhen
// So prüfen wir, ob der LETZTE Schritt uns über das Limit gebracht hat
float current_cycle_ml = id(phdispenser_cycle_time) * flow_rate;
// 2. Sofort-Prüfung gegen das Limit
if (current_cycle_ml >= id(max_ph_ml_per_cycle)) {
ESP_LOGW("safety-interval", "PH LIMIT STOP! Zyklus-Limit erreicht: %.1f ml", current_cycle_ml);
id(script_stopp_ph).execute();
}
else if (id(ph_today_ml) >= id(max_ph_ml_per_day)) {
ESP_LOGW("safety-interval", "PH LIMIT STOP! Tages-Limit erreicht.");
id(script_stopp_ph).execute();
}
else {
// 3. Nur wenn kein Limit erreicht ist, zählen wir die 7 Sekunden dazu
id(phdispenser_today_run_time) += 7; // Korrigiert auf 7!
id(phdispenser_cycle_time) += 7; // Korrigiert auf 7!
// Wir ziehen die Sekunden ab vom Timer
id(glo_ph_remaining_seconds) -= 7;
if (id(glo_ph_remaining_seconds) < 0) id(glo_ph_remaining_seconds) = 0;
float added_ml = 7.0 * flow_rate; // Korrigiert auf 7!
id(ph_today_ml) += added_ml;
id(ph_tank_remaining) -= added_ml;
id(ph_last_cycle_ml) = id(phdispenser_cycle_time) * flow_rate;
// PH Zielwert Prüfung (Zusätzlicher Stop-Grund)
if (id(ph_measvalue).state <= id(glo_ph_target)) {
ESP_LOGI("safety-interval", "PH Zielwert erreicht: %.2f", id(ph_measvalue).state);
id(script_stopp_ph).execute();
} else {
ESP_LOGD("safety-interval", "PH läuft: %.1f ml im Zyklus", id(ph_last_cycle_ml));
}
}
}
// --- D. ABSCHALT-LOGIK (Cleanup) ---
// Wir prüfen: Läuft die Pumpe gerade "unberechtigt"?
// 1. Timer ist aus
// 2. Frostschutz ist aus
// 3. Chlorinator ist aus
// 4. PH-Pumpe ist aus
// UND: Die Automatisierung denkt eigentlich, sie wäre noch aktiv (Flag)
// ODER die Pumpe läuft physikalisch noch.
bool run_allowed = id(bs_timer_allows_run).state ||
id(glo_frost_active) ||
id(switch_chlor_run_gpio).state ||
id(switch_relay2_ph).state;
if (!run_allowed) {
if (id(inv_running).state == 1 && id(glo_pump_active_via_automation)) {
ESP_LOGW("safety-interval", "Cleanup: Pumpe läuft ohne Grund -> STOP");
// Pumpe ausschalten
id(switch_poolpump).turn_off();
// Status-Flag zurücksetzen
id(glo_pump_active_via_automation) = false;
}
// Wenn sie läuft, aber NICHT von der Automatik gestartet wurde
else if (id(inv_running).state == 1 && !id(glo_pump_active_via_automation)) {
// Statischer Zähler, damit das Log nur alle 5 Minuten (ca. 42 Zyklen à 7s) nervt
static int manual_log_count = 0;
if (manual_log_count++ % 42 == 0) {
ESP_LOGI("safety-interval", "Manueller Modus: Pumpe läuft ohne Automatik-Anforderung.");
}
}
}
// --- E. Timer check - Abschaltung weil außerhalb der Zeit (Sicherheitsintervall Teil) ---
if (!id(bs_timer_allows_run).state && (id(inv_running).state == 1 && id(glo_pump_active_via_automation))) {
ESP_LOGW("safety-interval", "Abschaltung Pumpe - Außerhalb des Timers!");
ESP_LOGW("safety-interval", "bs_timer_allows_run = %s", id(bs_timer_allows_run).state ? "True" : "False");
ESP_LOGW("safety-interval", "id(inv_running).state = %s", (id(inv_running).state) == 1 ? "True" : "False");
ESP_LOGW("safety-interval", "id(glo_pump_active_via_automation) = %s", id(glo_pump_active_via_automation) ? "True" : "False");
id(script_stopp_all).execute();
}
################################################################################
######## Skripte ###############################################################
################################################################################
script:
############################## Tuya WatchDog ###################################
################################################################################
# - id: script_tuya_watchdog
# mode: single
# then:
# - lambda: |-
# const uint32_t timeout_ms = 25000;
# uint32_t now = millis();
# // 1. WATCHDOG LOGIK
# if (now < id(glo_tuya_last_seen_ms)) {
# ESP_LOGD("tuya_watchdog", "Gnadenfrist aktiv - Warte auf Hardware...");
# }
# else if ((now - id(glo_tuya_last_seen_ms)) < timeout_ms) {
# if (!id(glo_tuya_online)) {
# ESP_LOGI("tuya_watchdog", "Tuya ONLINE");
# id(glo_tuya_online) = true;
# id(bs_tuya_online).publish_state(true);
# }
# }
# else {
# if (id(glo_tuya_online)) {
# ESP_LOGW("tuya_watchdog", "Tuya OFFLINE -> Sensoren auf NAN");
# id(glo_tuya_online) = false;
# id(bs_tuya_online).publish_state(false);
# id(ph_measvalue).publish_state(NAN);
# id(orp_measvalue).publish_state(NAN);
# id(tuya_temp).publish_state(NAN);
# }
# }
# // 2. TOGGLE LOGIK (ELTAKO)
# if (id(uptime_raw).state >= 60 && now >= id(glo_tuya_last_seen_ms)) {
# if (id(glo_tuya_error_lockout)) return;
# const std::string mode = id(select_work_mode).current_option();
# bool is_winter = (mode == "55% Winterbetrieb");
# // LOGIK-KORREKTUR:
# // Wir toggeln NUR, wenn:
# // a) Wir NICHT im Winter sind aber der Sensor offline ist (wir wollen ihn anhaben)
# // b) Wir IM Winter sind aber der Sensor NOCH online ist (wir wollen ihn ausmachen)
# bool needs_toggle = false;
# if (!is_winter && !id(glo_tuya_online)) {
# needs_toggle = true;
# ESP_LOGW("tuya_watchdog", "Sommerbetrieb: Tuya fehlt -> Toggle AN");
# }
# else if (is_winter && id(glo_tuya_online)) {
# needs_toggle = true;
# ESP_LOGW("tuya_watchdog", "Winterbetrieb: Tuya sendet noch -> Toggle AUS");
# }
# if (needs_toggle) {
# // --- SCHALTWÄCHTER ---
# if (now - id(glo_tuya_last_reset_ms) > 1800000) {
# id(glo_tuya_toggle_count) = 0;
# id(glo_tuya_last_reset_ms) = now;
# }
# id(glo_tuya_toggle_count)++;
# if (id(glo_tuya_toggle_count) > 10) {
# id(glo_tuya_error_lockout) = true;
# id(bs_tuya_lockout).publish_state(true);
# return;
# }
# id(switch_relay3_tuya).turn_on(); // Der physische Klick am Eltako
# // WICHTIG: Lange Gnadenfrist, damit er nicht sofort wieder prüft
# id(glo_tuya_last_seen_ms) = now + 30000;
# // Wir nehmen an, dass der Toggle erfolgreich war, um Dauer-Toggles zu verhindern
# if (is_winter) {
# id(glo_tuya_online) = false;
# id(bs_tuya_online).publish_state(false);
# }
# }
# }
# - id: script_tuya_watchdog
# mode: single
# then:
# - lambda: |-
# const uint32_t timeout_ms = 15000; // 15s ohne UART → offline
# if ((millis() - id(glo_tuya_last_seen_ms)) < timeout_ms) {
# if (!id(glo_tuya_online)) { //"Wenn wir bisher dachten, es sei offline..."
# ESP_LOGI("tuya_watchdog", "Tuya ONLINE");
# id(glo_tuya_online) = true; // "...dann merken wir uns jetzt: Es ist ONLINE."
# id(bs_tuya_online).publish_state(true);
# }
# //weil es einen Timeout gibt, gehts weiter mit else
# } else {
# if (id(glo_tuya_online)) { //"Wenn wir bisher dachten, es sei online..."
# ESP_LOGW("tuya_watchdog", "Tuya OFFLINE → Status reset");
# id(glo_tuya_online) = false; // "...dann merken wir uns jetzt: Es ist OFFLINE."
# id(bs_tuya_online).publish_state(false);
# // HIER: Sensoren auf NAN setzen, damit HA nicht mit alten Werten rechnet
# id(ph_measvalue).publish_state(NAN);
# id(orp_measvalue).publish_state(NAN);
# id(tuya_temp).publish_state(NAN);
# }
# }
# - id: script_tuya_watchdog
# mode: single
# then:
# - lambda: |-
# bool needs_toggle = false;
# if (id(glo_tuya_allowed)) {
# if (!id(glo_tuya_online)) { //"Wenn wir bisher dachten, es sei offline..."
# ESP_LOGI("tuya_watchdog", "Tuya ist offline, soll aber Online sein");
# id(glo_tuya_online) = true; // "...dann merken wir uns jetzt: Es ist ONLINE."
# id(bs_tuya_online).publish_state(true);
# needs_toggle = true;
# id(glo_tuya_toggle_count)++;
# }
# } else {
# if (id(glo_tuya_online)) { //"Wenn wir bisher dachten, es sei online..."
# ESP_LOGW("tuya_watchdog", "Tuya ist online, soll aber Offline sein");
# id(glo_tuya_online) = false; // "...dann merken wir uns jetzt: Es ist OFFLINE."
# id(bs_tuya_online).publish_state(false);
# needs_toggle = true;
# id(glo_tuya_toggle_count)++;
# // // HIER: Sensoren auf NAN setzen, damit HA nicht mit alten Werten rechnet
# // id(ph_measvalue).publish_state(NAN);
# // id(orp_measvalue).publish_state(NAN);
# // id(tuya_temp).publish_state(NAN);
# }
# }
# // // 2. TOGGLE LOGIK (ELTAKO)
# // if (id(uptime_raw).state >= 60 && needs_toggle && id(glo_tuya_toggle_count) >= 3 ) {
# // id(switch_relay3_tuya).turn_on(); // Der physische Klick am Eltako
# // id(glo_tuya_toggle_count) = 0;
# // }
# //Wird nicht gebraucht, denn ist lokal und beim nächsten Start false
# //needs_toggle = false;
# - id: script_tuya_watchdog
# mode: single
# then:
# - lambda: |-
# bool needs_toggle = false;
# uint32_t now = millis();
# const uint32_t timeout_ms = 60000; // 60 Sekunden ohne Daten = Offline
# // 1. TIMEOUT-CHECK (Falls der Tuya einfach verstummt)
# // Wenn wir im Modus sind, wo er laufen darf, aber die letzte Meldung zu alt ist
# if (id(glo_tuya_allowed) && (now - id(glo_tuya_last_seen_ms) > timeout_ms)) {
# if (id(glo_tuya_online)) {
# ESP_LOGW("tuya_watchdog", "TIMEOUT: Keine Daten seit 60s. Setze auf Offline.");
# id(glo_tuya_online) = false;
# id(bs_tuya_online).publish_state(false);
# }
# }
# // 2. ZUSTANDS-PRÜFUNG (Soll vs. Ist)
# if (id(glo_tuya_allowed)) {
# if (!id(glo_tuya_online)) {
# needs_toggle = true;
# } else {
# id(glo_tuya_toggle_count) = 0; // Alles okay, Zähler reset
# }
# } else {
# // Im Winterbetrieb: Falls er noch Online ist, wollen wir ihn aus haben
# if (id(glo_tuya_online)) {
# needs_toggle = true;
# } else {
# id(glo_tuya_toggle_count) = 0;
# }
# }
# // 3. TOGGLE LOGIK (ELTAKO)
# if (id(uptime_raw).state >= 60 && needs_toggle) {
# id(glo_tuya_toggle_count)++;
# if (id(glo_tuya_toggle_count) >= 3) {
# ESP_LOGW("tuya_watchdog", "Abweichung bestätigt! Toggle-Impuls...");
# id(switch_relay3_tuya).turn_on();
# id(glo_tuya_toggle_count) = 0;
# // Sicherheits-Gnadenfrist: Wir tun so, als hätten wir gerade erst Daten gesehen,
# // damit der Timeout nicht sofort wieder zuschlägt, während das Teil bootet.
# id(glo_tuya_last_seen_ms) = now;
# if (!id(glo_tuya_allowed)) {
# id(glo_tuya_online) = false;
# id(bs_tuya_online).publish_state(false);
# id(ph_measvalue).publish_state(NAN);
# id(orp_measvalue).publish_state(NAN);
# id(tuya_temp).publish_state(NAN);
# }
# }
# }
- id: script_tuya_watchdog
mode: single
then:
- lambda: |-
bool needs_toggle = false;
uint32_t now = millis();
const uint32_t timeout_ms = 60000;