-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathswitch-exhaust.yaml
509 lines (460 loc) · 15.2 KB
/
switch-exhaust.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
---
# Device: Shelly 1 PM
# Location: Upstairs bathroom in wall
#
substitutions:
friendly_name: Upstairs bathroom exhaust
id: switch_exhaust
hostname: switch-exhaust
#ip: 10.0.20.50
#humidity_reference_sensor: "sensor.upstairs_bathroom_humidity_average_last_24_hours"
humidity_reference_sensor: "sensor.upstairs_humidity_average"
#humidity_reference_sensor: "input_number.test"
remaining_time_duration: "600" # 600 seconds = 10 minutes
stuck_duration: "1min"
max_temp: "70.0" # Max power measurement board temperature in °C.
<<: !include common/substitutions/gpio/shelly1pm.yaml
esphome:
friendly_name: $friendly_name
name: $hostname
on_boot:
- sensor.template.publish:
id: remaining_time
state: 0
- script.execute:
id: status_set
value: "BOOT"
esp8266:
board: esp01_1m
framework:
version: recommended
restore_from_flash: true
<<: !include common/common.yaml
globals:
- id: current_status
type: std::string
initial_value: ""
- id: previous_status
type: std::string
initial_value: ""
interval:
- interval: 5s
then:
# Update the current status.
- script.execute: humidity_delta_calculate
- script.execute: humidity_status_update
- script.execute: presence_status_update
# Continue the countdown, then take action.
- script.execute: remaining_time_decrement
- script.execute: status_change_actions
script:
################################
# TAKE ACTION ON STATUS CHANGE #
################################
- id: status_change_actions
then:
#- logger.log: "##### status_change_actions"
# OFF, WARM, BOOT
# MANUAL_ON, PHYSICAL, VIRTUAL
# PRESENCE_DETECTED, PRESENCE_CLEARED, HUMIDITY_LOWER, HUMIDITY_UPPER, WARM_STUCK
- if:
condition:
or:
- lambda: return id(current_status) == "PHYSICAL";
- lambda: return id(current_status) == "VIRTUAL";
- lambda: return id(current_status) == "PRESENCE_DETECTED";
- lambda: return id(current_status) == "PRESENCE_CLEARED";
- lambda: return id(current_status) == "HUMIDITY_LOWER";
- lambda: return id(current_status) == "HUMIDITY_UPPER";
- lambda: return id(current_status) == "WARM_STUCK";
then:
- script.execute: relay_on
# Start the countdown in these cases.
- if:
condition:
and:
- lambda: return id(humidity_delta).state <= id(delta_lower_trigger).state;
- or:
- lambda: return id(current_status) == "PHYSICAL";
- lambda: return id(current_status) == "VIRTUAL";
- lambda: return id(current_status) == "PRESENCE_CLEARED";
- lambda: return id(current_status) == "HUMIDITY_LOWER";
then:
- script.execute: remaining_time_start
# Stop the countdown in these cases.
- if:
condition:
or:
- lambda: return id(current_status) == "PRESENCE_DETECTED";
- lambda: return id(current_status) == "HUMIDITY_UPPER";
- lambda: return id(current_status) == "WARM_STUCK";
- lambda: return id(humidity_delta).state >= id(delta_lower_trigger).state;
then:
- script.execute: remaining_time_clear
else:
- if:
condition:
and:
- lambda: return id(current_status) == "OFF";
- lambda: return id(humidity_delta).state <= id(delta_lower_trigger).state;
then:
- script.execute: relay_off
- if:
condition:
and:
- lambda: return id(previous_status) != "BOOT";
- or:
- lambda: return id(current_status) == "WARM";
- lambda: return id(current_status) == "BOOT";
then:
- script.execute: relay_off
###########################
# STATUS CHANGING ACTIONS #
###########################
- id: humidity_delta_calculate
then:
#- logger.log: "##### humidity_delta_calculate"
# Recalculate the difference between the bathroom humidity and the reference humidity.
- sensor.template.publish:
id: humidity_delta
state: !lambda return id(humidity_now).state - id(humidity_reference).state;
- id: humidity_status_update
then:
#- logger.log: "##### humidity_status_update"
# When PRESENCE_DETECTED and the humidity is between the lower and upper trigger values.
- if:
condition:
and:
- lambda: return id(humidity_delta).state > id(delta_lower_trigger).state;
- lambda: return id(humidity_delta).state < id(delta_upper_trigger).state;
# ... and the humidity delta was NOT previously HUMIDITY_UPPER.
- lambda: return id(current_status) != "HUMIDITY_UPPER";
- lambda: return id(current_status) != "WARM_STUCK";
#- switch.is_on: relay
then:
# Humidity has entered the warming zone.
- script.execute:
id: status_set
value: "WARM"
# When the humidity is in the warming zone and the upper trigger is reached.
- if:
condition:
and:
- lambda: return id(current_status) == "WARM";
- lambda: return id(humidity_delta).state >= id(delta_upper_trigger).state;
then:
# Set the new status.
- script.execute:
id: status_set
value: "HUMIDITY_UPPER"
# When the humidity reaches or falls below the lower trigger.
- if:
condition:
and:
- lambda: return id(humidity_delta).state <= id(delta_lower_trigger).state;
# But only when one of the following are the current state.
- or:
- lambda: return id(current_status) == "HUMIDITY_UPPER";
- lambda: return id(current_status) == "WARM_STUCK";
- lambda: return id(current_status) == "WARM";
then:
- script.execute:
id: status_set
value: "HUMIDITY_LOWER"
- id: presence_status_update
then:
#- logger.log: "##### presence_status_update"
# Ignore presence state changes when the following states are active.
- if:
condition:
and:
- lambda: return id(current_status) != "WARM";
- lambda: return id(current_status) != "HUMIDITY_UPPER";
- lambda: return id(current_status) != "WARM_STUCK";
then:
- if:
condition:
and:
- binary_sensor.is_on: presence
- lambda: return id(current_status) != "PRESENCE_DETECTED";
then:
- script.execute:
id: status_set
value: "PRESENCE_DETECTED"
- if:
condition:
and:
- binary_sensor.is_off: presence
- lambda: return id(current_status) == "PRESENCE_DETECTED";
then:
- script.execute:
id: status_set
value: "PRESENCE_CLEARED"
- id: warm_stuck_update
then:
#- logger.log: "##### warm_stuck_update"
- if:
condition:
for:
time: $stuck_duration
condition:
- lambda: return id(current_status) == "WARM";
then:
# When warm is stuck start clearning out the humidity.
- script.execute:
id: status_set
value: "WARM_STUCK"
###################
# UTILITY ACTIONS #
###################
- id: relay_off
then:
#- logger.log: "##### relay_off"
- if:
condition:
- switch.is_on: relay
then:
- switch.turn_off: relay
- script.execute: remaining_time_clear
- id: relay_on
then:
#- logger.log: "##### relay_on"
- if:
condition:
- switch.is_off: relay
then:
- switch.turn_on: relay
- id: remaining_time_clear
then:
#- logger.log: "##### remaining_time_clear"
- if:
condition:
- lambda: return id(remaining_time).state > 0;
then:
- sensor.template.publish:
id: remaining_time
state: "0"
- id: remaining_time_decrement
then:
#- logger.log: "##### remaining_time_decrement"
- if:
condition:
- lambda: return id(remaining_time).state > 0;
then:
# Decrement the timer.
- sensor.template.publish:
id: remaining_time
state: !lambda return id(remaining_time).state -= 5;
# Set the state to "OFF" when the remaining time reaches zero.
- if:
condition:
- lambda: return id(remaining_time).state == 0;
then:
- script.execute:
id: status_set
value: "OFF"
- id: remaining_time_start
then:
#- logger.log: "##### remaining_time_start"
- if:
condition:
and:
- lambda: return id(remaining_time).state == 0;
- switch.is_on: relay
- lambda: return id(previous_status) != id(current_status);
then:
- sensor.template.publish:
id: remaining_time
state: !lambda return $remaining_time_duration;
- id: status_set
parameters:
value: string
then:
#- logger.log: "##### status_set"
- if:
condition:
- lambda: return id(current_status) != value;
then:
# Store previous state.
- globals.set:
id: previous_status
value: !lambda return id(current_status);
# Store the changed value.
- globals.set:
id: current_status
value: !lambda return value;
# Update the status sensor.
- text_sensor.template.publish:
id: status
state: !lambda return value;
binary_sensor:
# - id: physical_switch
# platform: gpio
# internal: true
# pin:
# number: $button_gpio
# filters:
# - delayed_on_off: 50ms
# on_state:
# # Set the new status.
# - if:
# condition:
# - binary_sensor.is_on: physical_switch
# then:
# - switch.turn_on: relay
# - script.execute:
# id: status_set
# value: "PHYSICAL"
# else:
# - script.execute:
# id: status_set
# value: "OFF"
- id: presence
platform: homeassistant
entity_id: binary_sensor.upstairs_bathroom_presence_occupancy
number:
- id: delta_lower_trigger
platform: template
name: "humidity delta lower trigger"
icon: "mdi:timer-stop"
entity_category: config
unit_of_measurement: "%"
optimistic: true
restore_value: true
min_value: 0.5
max_value: 15.0
step: 0.5
- id: delta_upper_trigger
platform: template
name: "humidity delta upper trigger"
icon: "mdi:timer-play"
entity_category: config
unit_of_measurement: "%"
optimistic: true
restore_value: true
min_value: 0.5
max_value: 30.0
step: 0.5
sensor:
- id: humidity_delta
platform: template
name: "humidity delta"
accuracy_decimals: 1
icon: "mdi:delta"
entity_category: diagnostic
unit_of_measurement: "%"
- id: humidity_now
platform: homeassistant
internal: true
accuracy_decimals: 1
entity_id: sensor.upstairs_bathroom_humidity
unit_of_measurement: "%"
- id: humidity_reference
platform: homeassistant
internal: true
accuracy_decimals: 1
entity_id: $humidity_reference_sensor
unit_of_measurement: "%"
- id: remaining_time
platform: template
name: "remaining time"
icon: "mdi:sort-clock-ascending"
accuracy_decimals: 0
entity_category: diagnostic
unit_of_measurement: "seconds"
## Power monitoring
- platform: adc
id: temp_analog_reading
pin: A0
- platform: hlw8012
cf_pin: GPIO05
cf1_pin: GPIO13 # not used because it is not available on the 1PM but it is needed to compile
sel_pin: GPIO14 # not used because it is not available on the 1PM but it is needed to compile
power:
name: "power"
id: "shelly1pm_power"
device_class: power
state_class: measurement
unit_of_measurement: W
accuracy_decimals: 0
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 1.0
- 110.33186 -> 20.62
- 131.01909 -> 24.32
- 341.33920 -> 62.08
- 5561.41553 -> 1000.0
- 2975.51221 -> 535.7
- 9612.66309 -> 1720.0
- 14891.35352 -> 2679.0
# Make everything below 2W appear as just 0W.
- lambda: if (x < 2) return 0; else return x;
update_interval: 60s
- platform: total_daily_energy
name: "daily energy"
power_id: "shelly1pm_power"
device_class: energy
state_class: total_increasing
unit_of_measurement: kWh
filters:
- multiply: 0.001
# Temperature overheat monitoring.
- platform: ntc
sensor: temp_resistance_reading
name: "temperature"
accuracy_decimals: 1
device_class: temperature
unit_of_measurement: "°C"
disabled_by_default: true
entity_category: diagnostic
calibration:
b_constant: 3350
reference_resistance: 10kOhm
reference_temperature: 298.15K
on_value_range:
- above: $max_temp
then:
- homeassistant.event:
event: esphome.overheat
data:
title: "$friendly_name has overheated."
- platform: resistance
id: temp_resistance_reading
sensor: temp_analog_reading
configuration: DOWNSTREAM
resistor: 32kOhm
switch:
- id: relay
platform: gpio
pin: $relay_gpio
- id: virtual_switch
platform: template
name: switch
turn_off_action:
- script.execute:
id: status_set
value: "OFF"
turn_on_action:
- script.execute:
id: status_set
value: "VIRTUAL"
lambda: |-
std::string off_values[3] = {"OFF", "WARM", "BOOT"};
bool result = true;
for (int i=0; i<sizeof(off_values); i++) {
if (id(current_status) == off_values[i]) result = false;
}
return result;
text_sensor:
- id: status
platform: template
name: "status"
entity_category: diagnostic
on_value:
then:
- script.execute: warm_stuck_update
time:
- id: homeassistant_time
platform: homeassistant
timezone: America/Los_Angeles