-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowbox.yaml
618 lines (577 loc) · 17.8 KB
/
growbox.yaml
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
esphome:
name: growbox
on_boot:
priority: 600.0
then:
- wait_until:
- wifi.connected:
- time.has_time:
- script.execute: thermostat_update
esp32:
board: nodemcu-32s
# Enable logging
logger:
logs:
climate: INFO
component: ERROR
sensor: INFO
aht10: ERROR
ads1115: INFO
main: INFO
light: INFO
switch: INFO
esp32.preferences: INFO
sntp: INFO
fan: INFO
level: DEBUG
# Enable Home Assistant API
api:
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# captive_portal:
web_server:
port: 80
# esp32_ble_tracker:
# bluetooth_proxy:
packages:
soil: !include homeConfig/soil.yaml
substitutions:
friendly_name: "Growbox"
soil_sensor_name: "${friendly_name} Soil Sensor"
soil_sensor_raw_name: "${friendly_name} Soil Raw Sensor"
# ads1115_gain: "6.144"
soil_moisture_default_dry: "3.2"
soil_moisture_default_wet: "3.2"
ads1115_i2c_bus: i2cPortB
ads1115_i2c_id: ads1115_hub
soil_moisture_id: soil_moisture
soil_moisture_raw_id: soil_moisture_raw
ads1115_multiplexer: 'A0_GND'
script:
- id: display_update_script
mode: single
then:
- switch.turn_on: displayOn
- component.update: my_display
- delay: 5s
- switch.turn_off: displayOn
# - sensor.rotary_encoder.set_value:
# id: rotary
# value: 0
- component.update: my_display
- id: mode_update
mode: single
then:
lambda: |-
bool noTemp = !id(temperature_internal).has_state() || !id(humidity_internal).has_state();
bool noTime = !id(sntp_time).now().is_valid();
if (noTemp || noTime) {
ESP_LOGD("main", "all off Time noTemp %d noTime %d", noTemp, noTime);
id(select_mode).publish_state("offAll");
return;
}
if (id(sntp_time).now().hour >= 5 && id(sntp_time).now().hour < 17) {
ESP_LOGD("main", "light on Time %02d:%02d:%02d", id(sntp_time).now().hour, id(sntp_time).now().minute, id(sntp_time).now().second);
id(select_mode).publish_state("lightOn");
return;
} else {
ESP_LOGD("main", "light off Time %02d:%02d:%02d", id(sntp_time).now().hour, id(sntp_time).now().minute, id(sntp_time).now().second);
id(select_mode).publish_state("lightOff");
return;
}
- id: thermostat_update
mode: single
then:
lambda: |-
if (id(select_mode).state == "offAll") {
id(growLight).turn_off().perform();
id(climate_temperature).make_call().set_mode("OFF").perform();
id(climate_humidity).make_call().set_mode("OFF").perform();
id(climate_humidity_pid).make_call().set_mode("OFF").perform();
ESP_LOGD("main", "all off");
return;
} else if (id(select_mode).state == "lightOn") {
id(growLight).turn_on().perform();
id(climate_temperature).make_call().set_mode("COOL").perform();
id(climate_humidity).make_call().set_mode("HEAT_COOL").perform();
id(climate_humidity_pid).make_call().set_mode("OFF").perform();
ESP_LOGD("main", "light on");
return;
} else if (id(select_mode).state == "lightOff") {
id(growLight).turn_off().perform();
id(climate_temperature).make_call().set_mode("OFF").perform();
id(climate_humidity).make_call().set_mode("COOL").perform();
id(climate_humidity_pid).make_call().set_mode("COOL").perform();
ESP_LOGD("main", "light off");
return;
}
select:
- platform: template # Mode Select
name: "${friendly_name} Mode"
id: select_mode
optimistic: true
options:
- lightOff
- lightOn
- offAll
- maintenance
initial_option: lightOff
restore_value: true
on_value:
- script.execute: thermostat_update
sensor:
# - platform: rotary_encoder # Rotary Encoder
# filters:
# - throttle: 0.01s
# name: "Rotary Encoder"
# internal: true
# min_value: 0
# max_value: 50
# restore_mode: ALWAYS_ZERO
# pin_a:
# number: 17
# mode:
# input: true
# pullup: true
# pin_b:
# number: 5
# mode:
# input: true
# pullup: true
# on_clockwise:
# then:
# script.execute: display_update_script
# on_anticlockwise:
# then:
# script.execute: display_update_script
# id: rotary
- platform: uptime # Uptime
name: "${friendly_name} Uptime"
update_interval: 10s
id: growbox_uptime
- platform: template # DHT20 External Sensor Temperature
name: "${friendly_name} dht20 Temperature"
lambda: |-
return id(temperature_internal).state;
update_interval: 5s
unit_of_measurement: "°C"
accuracy_decimals: 2
- platform: template # DHT20 External Sensor Humidity
name: "${friendly_name} dht20 Humidity"
lambda: |-
return id(humidity_internal).state;
update_interval: 5s
unit_of_measurement: "%"
accuracy_decimals: 2
- platform: aht10 # DHT20 Internal Sensor
i2c_id: i2cPortB
update_interval: 0.5s
temperature:
internal: True
name: "${friendly_name} dht20 Temperature Internal"
id: temperature_internal
filters:
- sliding_window_moving_average:
window_size: 4
send_every: 4
humidity:
name: "${friendly_name} dht20 Humidity Internal"
id: humidity_internal
internal: True
filters:
- sliding_window_moving_average:
window_size: 4
send_every: 4
# on_value_range:
# - above: 70
# then:
# - climate.control:
# id: template_climate
# mode: DRY
# - below: 70
# then:
# - climate.control:
# id: template_climate
# mode: AUTO
- platform: pid # PID Result Temperature
name: "${friendly_name} Climate PID Result"
type: RESULT
internal: True
climate_id: climate_temperature
id: pid_result
- platform: pid # PID Cool Result Fan Speed
name: "${friendly_name} Fan Speed Climate"
type: COOL
climate_id: climate_temperature
id: pid_cool
accuracy_decimals: 0
on_value:
then:
sensor.template.publish:
id: fan_speed_template
state: !lambda 'return x;'
- platform: pid # PID Dry Result Fan Speed
name: "${friendly_name} Fan Speed Humidity"
type: COOL
climate_id: climate_humidity_pid
id: pid_dry
accuracy_decimals: 0
on_value:
then:
sensor.template.publish:
id: fan_speed_template
state: !lambda 'return x;'
- platform: template # Fan Speed Template
id: fan_speed_template
name: "${friendly_name} Fan Speed Template"
unit_of_measurement: "%"
accuracy_decimals: 0
filters:
lambda: |-
if (id(climate_temperature).mode == CLIMATE_MODE_COOL) {
if (id(pid_cool).state < id(idle_fan_speed).state) {
return id(idle_fan_speed).state;
}
return id(pid_cool).state;
} else if (id(climate_humidity_pid).mode == CLIMATE_MODE_COOL) {
if (id(pid_dry).state < id(idle_fan_speed).state) {
return id(idle_fan_speed).state;
}
return id(pid_dry).state;
} else {
return id(idle_fan_speed).state;
}
on_value:
then:
output.set_level:
id: fanOutput
level: !lambda 'return x / 100.0f;'
- platform: template # Dew Point
name: "${friendly_name} Dew Point"
id: dew_point
lambda: |-
if (!id(temperature_internal).has_state() || !id(humidity_internal).has_state()) {
return NAN;
}
float dew_point_constant_a = 17.27;
float dew_point_constant_b = 237.7;
float alpha = ((dew_point_constant_a * id(temperature_internal).state) / (dew_point_constant_b + id(temperature_internal).state)) + log(id(humidity_internal).state / 100.0);
float dew_point = (dew_point_constant_b * alpha) / (dew_point_constant_a - alpha);
return dew_point;
unit_of_measurement: "°C"
accuracy_decimals: 2
update_interval: 10s
- platform: template # VPD
name: "${friendly_name} VPD"
id: vpd_sensor
lambda: |-
if (!id(temperature_internal).has_state() || !id(humidity_internal).has_state()) {
return NAN;
}
float tempValue = id(temperature_internal).state;
float humidityValue = id(humidity_internal).state;
// Calculate SVP
float svp = 610.78 * exp(tempValue / (tempValue + 237.3) * 17.2694);
// Convert SVP to kPa
svp = svp / 1000.0;
// Calculate VPD
float vpd = svp * (1.0 - (humidityValue / 100.0));
return vpd;
unit_of_measurement: "kPa"
accuracy_decimals: 2
update_interval: 10s
- platform: template # VPD Target
name: "${friendly_name} VPD Target"
id: vpd_target_sensor
lambda: |-
float tempValue = id(climate_temperature).target_temperature;
float humidityValue = id(climate_humidity).target_temperature;
// Calculate SVP
float svp = 610.78 * exp(tempValue / (tempValue + 237.3) * 17.2694);
// Convert SVP to kPa
svp = svp / 1000.0;
// Calculate VPD
float vpd = svp * (1.0 - (humidityValue / 100.0));
return vpd;
unit_of_measurement: "kPa"
accuracy_decimals: 2
update_interval: 1s
button:
- platform: template # Restart Button
name: "${friendly_name} Restart"
on_press:
then:
- switch.turn_on: restart_switch
- platform: template # PID Autotune Button
name: "${friendly_name} PID Climate Autotune"
on_press:
- climate.pid.autotune: climate_temperature
climate:
- platform: pid # pid temperature controller
internal: false
id: climate_temperature
name: "${friendly_name} Climate Temperature PID Controller"
sensor: temperature_internal
default_target_temperature: 24.5°C
cool_output: fanPIDTemplate
visual:
min_temperature: 23
max_temperature: 30
control_parameters:
kp: 0.5
ki: 0.00100
kd: 0.00000
- platform: pid # pid humidity controller
internal: false
id: climate_humidity_pid
name: "${friendly_name} Climate Humidity PID Controller"
sensor: humidity_internal
default_target_temperature: 55
cool_output: fanPIDTemplate
visual:
min_temperature: 40
max_temperature: 65
control_parameters:
kp: 0.15
ki: 0.00500
kd: 0.00000
- platform: thermostat # humidity controller
name: "${friendly_name} Climate Humidity Controller"
sensor: humidity_internal
id: climate_humidity
min_heating_off_time: 3s
min_cooling_off_time: 10s
min_heating_run_time: 3s
min_cooling_run_time: 10s
min_idle_time: 30s
visual:
min_temperature: 40
max_temperature: 65
cool_action:
- switch.turn_off: humiditySwitch
- switch.turn_on: dehumidifierSwitch
heat_action:
- switch.turn_on: humiditySwitch
- switch.turn_off: dehumidifierSwitch
idle_action:
- switch.turn_off: humiditySwitch
- switch.turn_off: dehumidifierSwitch
default_preset: Home
heat_overrun: 1
cool_overrun: 1
cool_deadband: 1
heat_deadband: 1
preset:
- name: Home
default_target_temperature_low: 55
default_target_temperature_high: 60
mode: "OFF"
switch: # restart, humidity, dehumidifier switches
- platform: restart
internal: True
id: restart_switch
name: "${friendly_name} Switch Restart"
- platform: output
name: "${friendly_name} Switch Humidity"
output: humidityOutput
id: humiditySwitch
restore_mode: ALWAYS_OFF
- platform: output
name: "${friendly_name} Switch Dehumidifier"
output: dehumidifierOutput
id: dehumidifierSwitch
restore_mode: ALWAYS_OFF
- platform: template
name: "${friendly_name} Display On"
id: displayOn
optimistic: True
restore_mode: ALWAYS_OFF
output: # light, fan, humidity, dehumidifier outputs
- platform: gpio
pin: 12
id: lightOutput
# inverted: True
- platform: gpio
pin: 14
id: humidityOutput
# inverted: True
- platform: gpio
pin: 27
id: dehumidifierOutput
# inverted: True
- platform: ledc
pin: 26
id: fanOutput
frequency: 4000 Hz
# min_power: 0.2
- platform: template
id: fanPIDTemplate
type: float
write_action:
lambda: |-
id(fan_speed_template).publish_state(state);
- platform: template
id: fanPWMTemplate
type: float
write_action:
lambda: |-
id(fan_speed_template).publish_state(state);
fan: # fan speed
- platform: speed
output: fanPWMTemplate
name: "Tent Fan PWM"
id: fanid
# internal: true
# binary_sensor: # rotary encoder button
# - platform: gpio
# internal: true
# pin:
# number: 16
# inverted: true
# mode:
# input: true
# pullup: true
# id: sw1
# on_press:
# then:
# - script.execute: display_update_script
number: # fan speed idle
- platform: template
name: ${friendly_name} Fan Speed Idle
id: idle_fan_speed
min_value: 0
max_value: 100
step: 5
initial_value: 30
optimistic: true
light:
- platform: binary
name: "${friendly_name} Light Grow"
output: lightOutput
id: growLight
restore_mode: RESTORE_DEFAULT_OFF
i2c:
- id: i2cPortA
sda: GPIO15
scl: GPIO2
frequency: 400khz
scan: False
- id: i2cPortB
sda: GPIO33
scl: GPIO25
frequency: 50khz
scan: False
- id: i2cPortC
sda: GPIO16
scl: GPIO17
frequency: 50khz
scan: False
# ads1115:
# - address: 0x48
# i2c_id: i2cPortB
# id: ads1115_hub
font:
- file: "gfonts://Roboto"
id: roboto
size: 12
time:
- platform: sntp
id: sntp_time
on_time:
- seconds: /10
then:
script.execute: mode_update
display:
- platform: ssd1306_i2c
i2c_id: i2cPortA
id: my_display
model: "SH1106_128x64"
address: 0x3C
update_interval: never
lambda: |-
if (!id(displayOn).state) {
return;
}
// Draw the graph at position [x=10,y=20]
// int yPos = id(rotary).state * -2;
int yPos = 0;
int fontSize = 12;
it.printf(2, yPos, id(roboto), "temp : %.1f°C", id(temperature_internal).state);
yPos = yPos + fontSize;
it.printf(2, yPos, id(roboto), "humidity : %.1f%%", id(humidity_internal).state);
yPos = yPos + fontSize;
if (!id(ads1115_hub).is_failed()) {
if (id(soil_moisture).state) {
it.printf(2, yPos, id(roboto), "soil : %.1f%%", id(soil_moisture).state);
} else {
it.printf(2, yPos, id(roboto), "soil : no state");
}
} else {
it.printf(2, yPos, id(roboto), "soil : error");
}
yPos = yPos + fontSize;
it.printf(2, yPos, id(roboto), "fan: %.1f%%", id(pid_cool).state);
yPos = yPos + fontSize;
if (id(growLight).current_values.is_on()) {
it.printf(2, yPos, id(roboto), "light : on");
} else {
it.printf(2, yPos, id(roboto), "light : off");
}
yPos = yPos + fontSize;
switch (id(climate_temperature).mode) {
case CLIMATE_MODE_OFF:
it.printf(2, yPos, id(roboto), "template : off %.0f", id(climate_temperature).target_temperature);
break;
case CLIMATE_MODE_HEAT:
it.printf(2, yPos, id(roboto), "template : heat %.0f", id(climate_temperature).target_temperature);
break;
case CLIMATE_MODE_HEAT_COOL:
it.printf(2, yPos, id(roboto), "template : heat + cool %.0f", id(climate_temperature).target_temperature);
break;
case CLIMATE_MODE_COOL:
it.printf(2, yPos, id(roboto), "template : cool %.0f", id(climate_temperature).target_temperature);
break;
case CLIMATE_MODE_AUTO:
it.printf(2, yPos, id(roboto), "template : auto %.0f", id(climate_temperature).target_temperature);
break;
case CLIMATE_MODE_DRY:
it.printf(2, yPos, id(roboto), "template : dry %.0f", id(climate_temperature).target_temperature);
break;
case CLIMATE_MODE_FAN_ONLY:
it.printf(2, yPos, id(roboto), "template : fan %.0f", id(climate_temperature).target_temperature);
break;
default:
it.printf(2, yPos, id(roboto), "template : unknown %.0f", id(climate_temperature).target_temperature);
break;
}
yPos = yPos + fontSize;
switch (id(climate_humidity).mode) {
case CLIMATE_MODE_OFF:
it.printf(2, yPos, id(roboto), "climate : off %.0f", id(climate_humidity).target_temperature);
break;
case CLIMATE_MODE_HEAT:
it.printf(2, yPos, id(roboto), "climate : heat %.0f", id(climate_humidity).target_temperature);
break;
case CLIMATE_MODE_HEAT_COOL:
it.printf(2, yPos, id(roboto), "climate : heat + cool %.0f", id(climate_humidity).target_temperature);
break;
case CLIMATE_MODE_COOL:
it.printf(2, yPos, id(roboto), "climate : cool %.0f", id(climate_humidity).target_temperature);
break;
case CLIMATE_MODE_AUTO:
it.printf(2, yPos, id(roboto), "climate : auto %.0f", id(climate_humidity).target_temperature);
break;
case CLIMATE_MODE_DRY:
it.printf(2, yPos, id(roboto), "climate : dry %.0f", id(climate_humidity).target_temperature);
break;
case CLIMATE_MODE_FAN_ONLY:
it.printf(2, yPos, id(roboto), "climate : fan %.0f", id(climate_humidity).target_temperature);
break;
default:
it.printf(2, yPos, id(roboto), "climate : unknown %.0f", id(climate_humidity).target_temperature);
break;
}
yPos = yPos + fontSize;
it.printf(2, yPos, id(roboto), "uptime : %.fs", id(growbox_uptime).state);