-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermostat.yaml
522 lines (501 loc) · 15.9 KB
/
thermostat.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
esphome:
name: thermostat
platform: ESP8266
board: d1_mini
on_boot:
priority: -10
then:
- script.execute: checker
wifi:
ssid: '5920 Talisman'
password: 'Asifmk1972'
# Enable logging
logger:
ota:
# Defining variables
globals:
- id: target_temp
type: int
initial_value: "22"
- id: mode
type: int
initial_value: "0"
# 0 = off
# 1 = heat
# 2 = ac
- id: fan_mode
type: int
initial_value: "1"
# 1 = auto
# 0 = always on
- id: away_mode
type: int
initial_value: "0"
# 1 = on
# 0 = off
- id: away_temp
type: int
restore_value: yes
### Defining variables for uptime heater
- id: heater_last_turn_on
type: int
# Defining MQTT
mqtt:
broker: 192.168.86.27
username: homeassistant
password: Asifmk1972
birth_message:
topic: stat/thermostat/availability
payload: online
will_message:
topic: stat/thermostat/availability
payload: offline
### Defining all relays
switch:
# Heater
- platform: gpio
id: heater
pin: D2
on_turn_on:
- lambda: 'id(heater_last_turn_on) = id(time).now().time;'
# A/C
- platform: gpio
id: ac
pin: D1
# Fan
- platform: gpio
id: fan
pin: D8
### Defining Time
time:
- platform: sntp
timezone: American/Toronto
id: time
### Defining Dallas temperature sensor and Pulishing the value
dallas:
- pin: D5
sensor:
- platform: dallas
address: 0xDF03099779475228
name: "Thermostat Temperature"
resolution: 12
id: current_temp
state_topic: stat/thermostat/dallas
filters:
- calibrate_linear:
# Map 0.0 (from sensor) to 0.0 (true value)
- 29.9 -> 23.1
- 31.4 -> 24.6
- 32.1 -> 25.4
- 32.2 -> 25.5
- 31.2 -> 25.2
- 18.4 -> 12.3
- 35.1 -> 26.2
- 29.2 -> 22.4
- 28.8 -> 21.8
- 28.1 -> 21.1
- 27.6 -> 20.8
- 27.7 -> 20.8
- 29.1 -> 22.1
on_value:
then:
- script.execute: checker
### Defining target temperature sensor for MQTT purposes
- platform: template
name: Target Temperature
id: target_temp_state_sensor
lambda: |-
return id(target_temp);
update_interval: 60s
state_topic: stat/thermostat/target
on_value:
then:
- component.update: cold_tolerence
- component.update: heat_tolerence
- platform: mqtt_subscribe
id: target_temp_command_sensor
topic: cmnd/thermostat/target
on_value:
then:
- lambda: 'id(display).send_command_no_ack("sleep=0");'
- delay: 0.5s
- lambda: |-
id(target_temp) = x;
- component.update: display
- component.update: target_temp_state_sensor
- logger.log:
format: "The target temperature has been set to %d"
args: [ 'id(target_temp)' ]
### Defining Target offsets
- platform: template
id: cold_tolerence
lambda: |-
return id(target_temp_state_sensor).state -= 0.3;
- platform: template
id: heat_tolerence
lambda: |-
return id(target_temp_state_sensor).state += 0.3;
### Defining stat mode sensor for MQTT purposes
- platform: template
name: Mode
id: mode_state_sensor
lambda: |-
return id(mode);
update_interval: 60s
state_topic: stat/thermostat/mode
### Defining stat fan mode sensor for MQTT purposes
- platform: template
name: Fan Mode
id: fan_mode_state_sensor
lambda: |-
return id(fan_mode);
update_interval: 60s
state_topic: stat/thermostat/fmode
### Recording the uptime of heater
- platform: template
name: Heater Uptime
id: heat_uptime
update_interval: 1s
lambda: |-
if (id(heater).state){
return id(time).now().time - id(heater_last_turn_on);
} else {
return id(heat_uptime).state;
}
### Defining command mode sensor for MQTT purposes
text_sensor:
- platform: mqtt_subscribe
id: mode_command_sensor
topic: cmnd/thermostat/mode
on_value:
then:
- lambda: 'id(display).send_command_no_ack("sleep=0");'
- delay: 0.25s
- lambda: 'id(display).goto_page("1");'
- if:
condition:
lambda: 'return id(mode_command_sensor).state == "off";'
then:
lambda: 'id(mode) = 0;'
else:
- if:
condition:
lambda: 'return id(mode_command_sensor).state == "heat";'
then:
lambda: 'id(mode) = 1;'
else:
- if:
condition:
lambda: 'return id(mode_command_sensor).state == "cool";'
then:
lambda: 'id(mode) = 2;'
- component.update: mode_state_sensor
- logger.log:
format: "The mode has been set to %d"
args: [ 'id(mode)' ]
- script.execute: checker
- delay: 0.25s
- if:
condition:
lambda: 'return id(mode) == 0;'
then:
- lambda: |-
id(display).send_command_no_ack("main.bg.pic=0");
id(display).send_command_no_ack("main.target.picc=0");
id(display).send_command_no_ack("main.current.picc=0");
else:
- if:
condition:
lambda: 'return id(mode) == 1;'
then:
- lambda: |-
id(display).send_command_no_ack("main.bg.pic=1");
id(display).send_command_no_ack("main.target.picc=1");
id(display).send_command_no_ack("main.current.picc=1");
else:
- if:
condition:
lambda: 'return id(mode) == 2;'
then:
- lambda: |-
id(display).send_command_no_ack("main.bg.pic=2");
id(display).send_command_no_ack("main.target.picc=2");
id(display).send_command_no_ack("main.current.picc=2");
### Defining command fan mode sensor for MQTT purposes
- platform: mqtt_subscribe
id: fan_mode_command_sensor
topic: cmnd/thermostat/fmode
on_value:
then:
- lambda: 'id(display).send_command_no_ack("sleep=0");'
- delay: 0.5s
- if:
condition:
lambda: 'return id(fan_mode_command_sensor).state == "auto";'
then:
lambda: 'id(fan_mode) = 1;'
else:
- if:
condition:
lambda: 'return id(fan_mode_command_sensor).state == "on";'
then:
lambda: 'id(fan_mode) = 0;'
- component.update: display
- component.update: fan_mode_state_sensor
- logger.log:
format: "The fan mode has been set to %d"
args: [ 'id(fan_mode)' ]
- script.execute: checker
### Defining what to do on all types of modes including fan modes
script:
id: checker
then:
# turn on the fan if the fan mode is set to always on
- if:
condition:
lambda: 'return id(fan_mode) == 0;'
then:
- switch.turn_on: fan
# If mode is set to heat, then...
- if:
condition:
lambda: 'return id(mode) == 1;'
then:
# turn on the heater and fan if its cold
- if:
condition:
lambda: 'return id(current_temp).state < id(cold_tolerence).state;'
then:
- switch.turn_on: heater
- switch.turn_on: fan
# turn off the heater if its hot
else:
- if:
condition:
lambda: 'return id(current_temp).state > id(heat_tolerence).state;'
then:
- switch.turn_off: heater
# if fan mode is set to auto then turn off the fan
- if:
condition:
lambda: 'return id(fan_mode) == 1;'
then:
switch.turn_off: fan
else:
# if mode is set to cool, then...
- if:
condition:
lambda: 'return id(mode) == 2;'
then:
# turn on the ac and fan if its hot
- if:
condition:
lambda: 'return id(current_temp).state > id(heat_tolerence).state;'
then:
- switch.turn_on: ac
- switch.turn_on: fan
else:
# turn off the ac if its cold
- if:
condition:
lambda: 'return id(current_temp).state < id(cold_tolerence).state;'
then:
- switch.turn_off: ac
# if the fan mode is set to auto then turn off the fan
- if:
condition:
lambda: 'return id(fan_mode) == 1;'
then:
- switch.turn_off: fan
else:
# if the mode is set to off, then turn off the ac and heater
- if:
condition:
lambda: 'return id(mode) == 0;'
then:
- switch.turn_off: heater
- switch.turn_off: ac
# if fan mode is set to auto then turn off the fan
- if:
condition:
lambda: 'return id(fan_mode) == 1;'
then:
- switch.turn_off: fan
### Defining Nextion Display and Defining What To Display
uart:
rx_pin: D3
tx_pin: D4
baud_rate: 9600
display:
- platform: nextion
id: display
lambda: |-
it.set_component_text_printf("main.target", "%d", id(target_temp));
it.set_component_text_printf("main.current", "%.1f", id(current_temp).state);
### Defining Touch components and What will they do
binary_sensor:
- platform: nextion
page_id: 1
component_id: 4
name: "Increase Target Temperature"
on_press:
then:
- lambda: |-
(id(target_temp) += 1);
- component.update: display
- component.update: target_temp_state_sensor
- logger.log:
format: "The target temperature has been increased to %d"
args: [ 'id(target_temp)' ]
- platform: nextion
page_id: 1
component_id: 5
name: "Decrease Target Temperature"
on_press:
then:
- lambda: |-
(id(target_temp) -= 1);
- component.update: display
- component.update: target_temp_state_sensor
- logger.log:
format: "The target temperature has been decreased to %d"
args: [ 'id(target_temp)' ]
- platform: nextion
page_id: 2
component_id: 5
name: "mode"
on_press:
then:
- if:
condition:
lambda: |-
return id(mode) == 0;
then:
- lambda: |-
id(mode) = 1;
id(display).send_command_no_ack("off_heat.en=1");
- delay: 0.60s
- lambda: |-
id(display).send_command_no_ack("off_heat.en=0");
id(display).send_command_no_ack("main.bg.pic=1");
id(display).send_command_no_ack("main.target.picc=1");
id(display).send_command_no_ack("main.current.picc=1");
- component.update: mode_state_sensor
else:
- if:
condition:
lambda: |-
return id(mode) == 1;
then:
- lambda: |-
id(mode) = 2;
id(display).send_command_no_ack("heat_cool.en=1");
- delay: 0.60s
- lambda: |-
id(display).send_command_no_ack("heat_cool.en=0");
id(display).send_command_no_ack("main.bg.pic=2");
id(display).send_command_no_ack("main.target.picc=2");
id(display).send_command_no_ack("main.current.picc=2");
- component.update: mode_state_sensor
else:
- if:
condition:
lambda: |-
return id(mode) == 2;
then:
- lambda: |-
id(mode) = 0;
id(display).send_command_no_ack("cool_off.en=1");
- delay: 0.60s
- lambda:
id(display).send_command_no_ack("cool_off.en=0");
id(display).send_command_no_ack("main.bg.pic=0");
id(display).send_command_no_ack("main.target.picc=0");
id(display).send_command_no_ack("main.current.picc=0");
- component.update: mode_state_sensor
- script.execute: checker
- platform: nextion
page_id: 1
component_id: 6
name: "page_2"
on_press:
then:
- if:
condition:
lambda: 'return id(fan_mode) == 1;'
then:
lambda: 'id(display).send_command_no_ack("p2.main2.pic=12");'
else:
- if:
condition:
lambda: 'return id(fan_mode) == 0;'
then:
lambda: 'id(display).send_command_no_ack("p2.main2.pic=13");'
- if:
condition:
lambda: |-
return id(mode) == 0;
then:
lambda: |-
id(display).send_command_no_ack("p2.p1.pic=14");
- if:
condition:
lambda: |-
return id(mode) == 1;
then:
lambda: |-
id(display).send_command_no_ack("p2.p1.pic=24");
- if:
condition:
lambda: |-
return id(mode) == 2;
then:
lambda: |-
id(display).send_command_no_ack("p2.p1.pic=34");
- script.execute: checker
- platform: nextion
page_id: 2
component_id: 4
name: Fan Mode Button
on_press:
then:
- if:
condition:
lambda: 'return id(fan_mode) == 1;'
then:
- lambda: 'id(fan_mode) = 0;'
- lambda: 'id(display).send_command_no_ack("p2.main2.pic=13");'
else:
- if:
condition:
lambda: 'return id(fan_mode) == 0;'
then:
- lambda: 'id(fan_mode) = 1;'
- lambda: 'id(display).send_command_no_ack("p2.main2.pic=12");'
- if:
condition:
lambda: |-
return id(mode) == 0;
then:
lambda: |-
id(display).send_command_no_ack("p2.p1.pic=14");
- if:
condition:
lambda: |-
return id(mode) == 1;
then:
lambda: |-
id(display).send_command_no_ack("p2.p1.pic=24");
- if:
condition:
lambda: |-
return id(mode) == 2;
then:
lambda: |-
id(display).send_command_no_ack("p2.p1.pic=34");
- component.update: display
- component.update: fan_mode_state_sensor
- logger.log:
format: "The fan mode has been set to %d"
args: [ 'id(fan_mode)' ]
- script.execute: checker