-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathair-purifier.yml
309 lines (288 loc) · 7.12 KB
/
air-purifier.yml
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
substitutions:
device_name: "air-purifier"
device_friendly_name: "Air Purifier"
device_description: "BlitzWolf BH-AP2501"
time_timezone: "Europe/Amsterdam"
esphome:
name: $device_name
friendly_name: $device_friendly_name
comment: $device_description
name_add_mac_suffix: false
on_boot:
priority: -100
then:
# Wait for the tuya startup to complete
- delay: 7s
- switch.turn_on: auto_clean
esp8266:
board: esp12e
# Enable status LED
status_led:
pin:
number: GPIO2
inverted: true
# Enable logging
logger:
level: INFO
# Disable logging via UART, we need it for the Tuya MCU
baud_rate: 0
# Enable Home Assistant API
api:
encryption:
key: !secret home_assistant_key
ota:
password: !secret ota_password
# Configure WiFi
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: $device_friendly_name
password: !secret wifi_fallback_password
captive_portal:
# Initialize the UART that connects to the Tuya MCU
uart:
rx_pin: GPIO3
tx_pin: GPIO1
baud_rate: 9600
# Register the Tuya MCU connection
tuya:
fan:
# This fan component is presented to Home Assistant
- platform: speed
output: fan_output
id: speed_fan
name: "Fan"
speed_count: 3
# This internal fan component allows you to control the TuYa hardware
- platform: "tuya"
id: "tuya_fan"
internal: True
switch_datapoint: 1
speed_datapoint: 3
speed_count: 4 # 1=Auto, 2=High, 3=Medium, 4=Sleep
# Bind speed changes on device to our speed_fan
on_speed_set:
- lambda: |-
if (id(tuya_fan).state) {
auto call = id(speed_fan).turn_on();
if (id(tuya_fan).speed == 2) {
call.set_speed(3);
}
if (id(tuya_fan).speed == 3) {
call.set_speed(2);
}
if (id(tuya_fan).speed == 4) {
call.set_speed(1);
}
call.perform();
}
# Bind power changes on device to our speed_fan
on_turn_on:
- logger.log: "Fan Turned On!"
- fan.turn_on:
id: speed_fan
- switch.turn_on:
id: auto_clean
# Bind power changes on device to our speed_fan
on_turn_off:
- logger.log: "Fan Turned Off!"
- fan.turn_off:
id: speed_fan
- switch.turn_off:
id: auto_clean
# Fan control logic
output:
- platform: template
id: fan_output
type: float
# Off = 0
# Low = 0.33333
# Medium = 0.66666
# High = 1
write_action:
- logger.log:
level: DEBUG
format: "Fan output: %f"
args: ["state"]
- if:
condition:
lambda: return (state == 0);
then:
- fan.turn_off:
id: tuya_fan
else:
- if:
condition:
lambda: return (state < .4);
then:
# Fan = Low
- fan.turn_on:
id: tuya_fan
speed: 4
else:
- if:
condition:
lambda: return (state < .8);
then:
# Fan = Mid
- fan.turn_on:
id: tuya_fan
speed: 3
else:
# Fan = High
- fan.turn_on:
id: tuya_fan
speed: 2
sensor:
# PM2.5 sensor
- platform: "tuya"
name: "PM2.5"
id: pm_25
sensor_datapoint: 2
device_class: PM25
unit_of_measurement: µg/m³
state_class: "measurement"
accuracy_decimals: 1
# PM2.5 sensor with smoothing
- platform: template
name: "PM2.5 Filtered"
id: pm_25_filtered
device_class: PM25
unit_of_measurement: µg/m³
state_class: "measurement"
update_interval: 1s
lambda: return id(pm_25).state;
filters:
- median:
window_size: 4
send_every: 2
send_first_at: 2
# Remove 999 error spikes
#- filter_out: 999
# Filter outlier "1" values
#- quantile:
# window_size: 5
# send_every: 1
# send_first_at: 1
# quantile: .9
# Smooth out noise
- exponential_moving_average:
alpha: 0.05
send_every: 1
send_first_at: 1
on_value:
then:
- if:
condition:
switch.is_on: auto_clean
then:
- script.execute:
id: auto_fan_speed
# Filter utilization
- platform: "tuya"
name: "Filter remaining"
sensor_datapoint: 5
unit_of_measurement: "%"
icon: mdi:ticket-percent-outline
entity_category: diagnostic
filters:
- lambda: return 100-x;
# Countdown remaining
- platform: "tuya"
name: "Countdown Remaining"
sensor_datapoint: 19
device_class: duration
unit_of_measurement: "min"
# Air quality
- platform: "tuya"
name: "Air Quality"
device_class: aqi
sensor_datapoint: 21
number:
# Set countdown timer
- platform: "tuya"
name: "Countdown"
number_datapoint: 18
unit_of_measurement: h
device_class: duration
min_value: 0
max_value: 24
step: 1
switch:
# Ionizer
- platform: "tuya"
name: "Ionizer"
id: ionizer
switch_datapoint: 6
icon: mdi:chart-bubble
# UV sterilization
- platform: "tuya"
name: "UV sterilization"
switch_datapoint: 9
icon: mdi:sun-wireless
# Filter reset
- platform: "tuya"
id: "filter_reset"
switch_datapoint: 11
internal: True
# Auto mode
- platform: template
name: "Auto clean"
id: auto_clean
icon: "mdi:shimmer"
optimistic: True
restore_mode: ALWAYS_ON
turn_on_action:
- fan.turn_on:
id: speed_fan
speed: 1
# Button to reset filter cartridge
button:
- platform: template
name: "Filter cartridge reset"
entity_category: diagnostic
icon: mdi:air-filter
on_press:
then:
- fan.turn_on: tuya_fan
- switch.turn_on: filter_reset
- delay: 1s
- switch.turn_off: filter_reset
script:
# Determine which speeed the fan needs to run on based on the pm2.5
- id: auto_fan_speed
then:
- if:
condition:
sensor.in_range:
id: pm_25_filtered
below: 20
then:
- fan.turn_on:
id: speed_fan
speed: 1
- switch.turn_off:
id: ionizer
- if:
condition:
sensor.in_range:
id: pm_25_filtered
above: 30
below: 50
then:
- fan.turn_on:
id: speed_fan
speed: 2
- if:
condition:
sensor.in_range:
id: pm_25_filtered
above: 60
then:
- fan.turn_on:
id: speed_fan
speed: 3
- switch.turn_on:
id: ionizer