forked from brianhanifin/esphome-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelly1_02.yaml
186 lines (163 loc) · 4.88 KB
/
shelly1_02.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
---
# Shelly 2 Power Module
# Location: Upstairs Bathroom
#
substitutions:
project: Upstairs Bathroom Exhaust
id: shelly1_02
default_humidity_diff_threshold: "10" # Difference between house humidity and bathroom humidity.
countdown_duration: "600" # 600 seconds = 10 minutes
<<: !include common/substitutions/gpio/shelly1.yaml
<<: !include common/esphome/esp8266.yaml
<<: !include common/common.yaml
script:
- id: countdown_restart
then:
- globals.set:
id: remaining_time
value: !lambda return $countdown_duration;
binary_sensor:
# Physical Toggle Switch. Monitor for switch position changes internally. Do not expose to Home Assistant.
- platform: gpio
pin:
number: $button_gpio
id: button
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_state:
- switch.toggle: relay
- text_sensor.template.publish:
id: trigger
state: PHYSICAL
# Use motion detection to keep fan running.
- platform: homeassistant
id: motion
entity_id: binary_sensor.upstairs_bathroom_presence
globals:
- id: remaining_time
type: int
restore_value: no
initial_value: "0"
- id: humidity_diff
type: float
restore_value: no
initial_value: "0.1"
- id: diff_trigger
type: float
restore_value: no
initial_value: $default_humidity_diff_threshold
# Update the countdown timers every 5 seconds.
interval:
- interval: 5s
then:
#- lambda: ESP_LOGD("interval", "diff %.1f > trigger %.1f", id(humidity_diff), id(diff_trigger));
- if: # Humidity threshold trigger.
condition:
and:
# If the difference is greater than the trigger.
- lambda: return id(humidity_diff) > id(diff_trigger);
# - sensor.in_range:
# id: humidity_diff
# above: $default_humidity_diff_threshold
# and the fan is off.
- switch.is_off: relay
then:
- switch.turn_on: relay
- if: # When motion is active, restart the countdown.
condition:
- binary_sensor.is_on: motion
then:
- script.execute: countdown_restart
- if: # Decrement the countdown.
condition:
and:
- lambda: return id(remaining_time) > 0;
- switch.is_on: relay
then:
- globals.set:
id: remaining_time
value: !lambda return id(remaining_time) -= 5;
- sensor.template.publish:
id: countdown
state: !lambda return id(remaining_time);
# Time to turn off the relay.
else:
- if: # Decrement the countdown.
condition:
- switch.is_on: relay
then:
- switch.turn_off: relay
number:
- platform: template
name: Upstairs Bathroom Humidity Trigger
id: diff_trigger_ui
icon: "mdi:water-percent"
optimistic: true
initial_value: $default_humidity_diff_threshold
restore_value: true
min_value: 5
max_value: 15
step: 0.5
set_action:
then:
- globals.set:
id: diff_trigger
value: !lambda return id(diff_trigger_ui).state;
sensor:
# Measure the difference between the house humidity and bathroom humidity from Home Assistant.
- platform: homeassistant
id: humidity_diff_ha
internal: true
accuracy_decimals: 1
entity_id: sensor.upstairs_bathroom_humidity_difference
on_value:
then:
- globals.set:
id: humidity_diff
value: !lambda return id(humidity_diff_ha).state;
# Retrieve the difference trigger value from the Home Assistant UI.
# - platform: homeassistant
# id: diff_trigger_ha
# internal: true
# accuracy_decimals: 1
# entity_id: input_number.upstairs_bath_humidity_trigger
# on_value:
# then:
# - globals.set:
# id: diff_trigger
# value: !lambda return id(diff_trigger_ha).state;
- platform: template
id: countdown
name: "$project: Countdown"
accuracy_decimals: 0
icon: "mdi:camera-timer"
switch:
- platform: gpio
id: relay
pin: $relay_gpio
on_turn_on:
# Start the countdown timer.
- script.execute: countdown_restart
on_turn_off:
- sensor.template.publish:
id: countdown
state: 0
- platform: template
id: virtual_switch
name: $project
optimistic: no
lambda: |-
return (id(relay).state);
turn_on_action:
- switch.turn_on: relay
- text_sensor.template.publish:
id: trigger
state: VIRTUAL
turn_off_action:
- switch.turn_off: relay
text_sensor:
# Trigger states: PHYSICAL, VIRTUAL (UI or Alexa/Google), THRESHOLD
- platform: template
id: trigger
name: "$project: Last Trigger"