forked from RoadkillUK/Motor-on-a-Roller-Blind-for-ESPHOME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyblind.yaml
447 lines (418 loc) · 15.3 KB
/
myblind.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
#https://github.com/RoadkillUK/Motor-on-a-Roller-Blind-for-ESPHOME - Written by RoadkillUK
# This sketch will add 2 switches named <upper_devicename> Setup Switch and Setup Button
# Use your mobile or tablet and connect to http://<devicename>.local to set up the blind
# Turn on the Setup Switch to enter Setup Mode and use the Setup Button as shown below to setup blinds.
# 1) Turn on the Setup Switch to enter setup mode
# 2) Press Setup button to start the blind closing
# 3) Press Setup button again when closed and blind starts to open (actually resets the stepper position to 0)
# 4) Press Setup button again when blind is fully open
# 5) Job Done
# This sketch also includes a momentary button on D7 which can be used in the following way
# 1) Press button for > 1 second to enter setup mode
# 2) Press button again to start the blind closing
# 3) Press button again when closed and blind starts to open (actually resets the stepper position to 0)
# 4) Press button again when blind is fully open
# 5) Job Done
# Button is also used to open/close the blind (must be fully open/closed first)
# Copy the file to the config/esphome/ directory
substitutions:
devicename: roller_blind
upper_devicename: Roller Blind
mystepper: my_stepper # Name of the stepper motor (leave as is)
speed: 600 steps/s # Set the speed of the motor
reportin: "0" # Set if you want the blind to report position while moving (causes delay)
pina: D1 # Swap these \_ coil a
pinb: D3 # Swap these /
pinc: D2 # Swap these \_ coil b
pind: D4 # Swap these /
# NOTE: If you find that your shades are going the wrong way, you can change the pin
# settings or reverse the + and – wires for each of the A and B motor
# pairs on your driver and the motor will spin in the opposite direction.
# These pinouts are for the original 'Motor on a Roller Blind'esphome:
esp8266:
board: d1_mini
restore_from_flash: True
esphome:
name: $devicename
on_boot:
- priority: -200.0
then:
- stepper.report_position: # Set stepper to global variable
id: $mystepper
position: !lambda return id(${mystepper}_global);
- stepper.set_target: # Set stepper to global variable
id: $mystepper
target: !lambda return id(${mystepper}_global);
- if: # If blind is Closed
condition:
- lambda: 'return id(${mystepper}_global) == 0;'
then: # Publish state etc.
- cover.template.publish:
id: blinded
state: CLOSED
current_operation: IDLE
- if: # If blind is Open
condition:
- lambda: 'return id(${mystepper}_global) == id(endstop);'
then: # Publish state etc.
- cover.template.publish:
id: blinded
state: OPEN
current_operation: IDLE
- if: # If blind is Neither
condition:
- lambda: 'return (id(${mystepper}_global) != 0) && (id(${mystepper}_global) != id(endstop));'
then: # # Publish state etc.
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: IDLE
wifi:
networks:
- ssid: !secret wifi
password: !secret wifi_password
ap:
ssid: $upper_devicename
password: !secret wifi_password
web_server:
port: 80
logger:
api:
encryption:
key: !secret enc_key
ota:
password: !secret ota_password
platform: esphome
captive_portal:
stepper:
- platform: uln2003
id: $mystepper
pin_a: $pina
pin_b: $pinb
pin_c: $pinc
pin_d: $pind
max_speed: ${speed} # Speed of the motor
sleep_when_done: True
acceleration: inf
deceleration: inf
globals:
- id: ${mystepper}_global # Integer for storing the stepper position in case of reboot
type: unsigned long
restore_value: True
initial_value: '0'
- id: openclosed # Boolean to store OPEN/CLOSED state
type: bool
restore_value: True
initial_value: '0'
- id: endstop # Variable for storing ENDSTOP (how far to move stepper)
type: unsigned long
restore_value: True
initial_value: '1000'
- id: settingmode # Integer for Setup Mode
type: int
restore_value: no
initial_value: '0'
- id: reportin # Boolean for reporting the position of blind when moving (See testblind.yaml)
type: bool
restore_value: no
initial_value: '${reportin}'
text_sensor:
- platform: version
name: ${upper_devicename} ESPHome Version
hide_timestamp: true
- platform: wifi_info
ip_address:
name: ${upper_devicename} IP
icon: mdi:ip-network
ssid:
name: ${upper_devicename} SSID
icon: mdi:wifi
mac_address:
name: "${upper_devicename} MAC"
icon: mdi:identifier
- platform: template
name: ${upper_devicename} Friendly Name
icon: 'mdi:information-outline'
lambda: |-
return {"$upper_devicename"};
- platform: template
name: ${upper_devicename} Uptime
id: uptime_human
icon: mdi:clock-start
binary_sensor:
- platform: gpio
pin:
number: D7 # Connect Button to D7 and GND
mode: INPUT_PULLUP
inverted: True
name: Button
internal: True
on_click:
- min_length: 50ms
max_length: 500ms
then: # Short press to OPEN/CLOSE blinds and also for setting up
- if: # If settings variable is on
condition:
- lambda: 'return id(settingmode) != 0;'
then: # Enter Setting Mode
- script.execute: setupbutton
else:
- if: # If blind is closed
condition:
- lambda: 'return id(openclosed) == 0;'
then: # Open blind
- cover.open: blinded
else: # Close blind
- cover.close: blinded
- min_length: 1000ms
max_length: 3000ms
then: # Long press to Enter Setting Mode
- logger.log: "Entered Settings Mode"
- globals.set:
id: settingmode
value: '1'
sensor:
# Uptime sensor.
- platform: uptime
name: ${upper_devicename} Uptime Internal
id: 'upptime'
internal: True
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(upptime).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? String(days) + "d " : "") +
(hours ? String(hours) + "h " : "") +
(minutes ? String(minutes) + "m " : "") +
(String(seconds) + "s")
).c_str();
# WiFi Signal sensor.
- platform: wifi_signal
name: ${upper_devicename} WiFi Signal
update_interval: 120s
switch:
- platform: template
name: ${upper_devicename} Setup Switch # Switch to enter Setup Mode
id: setupswitch
lambda: |-
if (id(settingmode) != 0) {
return true;
} else {
return false;
}
turn_on_action:
then:
- logger.log: "Entered Settings Mode"
- globals.set:
id: settingmode
value: '1'
turn_off_action:
then:
- logger.log: "Exiting Settings Mode"
- globals.set:
id: settingmode
value: '0'
button:
- platform: template
name: ${upper_devicename} Setup Button # Software Button to replicate the Physical Button
id: hasetup
on_press:
- if: # If settings variable is on
condition:
- lambda: 'return id(settingmode) != 0;'
then: # Enter Setting Mode
- script.execute: setupbutton
cover:
- platform: template
name: $upper_devicename
id: blinded
open_action:
then:
- logger.log: "Opening"
- logger.log:
format: "Endstop is: %d"
args: [ 'id(endstop)' ]
- stepper.set_target: # Send stepper to endstop
id: $mystepper
target: !lambda return id(endstop);
- if:
condition:
lambda: 'return id(reportin) == 1;' # If Reporting is Selected
then:
- while:
condition:
lambda: 'return id($mystepper).current_position != id(endstop);'
then:
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: OPENING
- delay: 1000 ms
else:
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: OPENING
- wait_until: # Wait until endstop reached
lambda: 'return (id($mystepper).current_position == id(endstop));'
- globals.set: # Set global to current position
id: ${mystepper}_global
value: !lambda return id($mystepper).current_position;
- globals.set: # Set toggle to OPEN (No need for 'optimistic mode')
id: openclosed
value: '1'
- cover.template.publish:
id: blinded
state: OPEN
current_operation: IDLE
close_action:
then:
- logger.log: "Closing"
- stepper.set_target: # Send stepper to 0
id: $mystepper
target: '0'
- if:
condition:
lambda: 'return id(reportin) == 1;'
then:
- while:
condition:
lambda: 'return id($mystepper).current_position != 0;'
then:
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: CLOSING
- delay: 1000 ms
else:
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: CLOSING
- wait_until: # Wait until endstop reached
lambda: 'return id($mystepper).current_position == 0;'
- globals.set: # Set global to current position
id: ${mystepper}_global
value: !lambda return id($mystepper).current_position;
- globals.set: # Set toggle to CLOSED (No need for 'optimistic mode')
id: openclosed
value: '0'
- cover.template.publish:
id: blinded
state: CLOSED
current_operation: IDLE
position_action:
then:
- stepper.set_target:
id: $mystepper
target: !lambda return int(id(endstop) * pos);
- if:
condition:
lambda: 'return id(reportin) == 1;'
then:
- while:
condition:
lambda: 'return id($mystepper).current_position != int(id(endstop) * pos);'
then:
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
- delay: 1000 ms
else:
- wait_until: # Wait until endstop reached
lambda: 'return id($mystepper).current_position == (int(id(endstop) * pos));'
- globals.set: # Set global to current position
id: ${mystepper}_global
value: !lambda return id($mystepper).current_position;
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: IDLE
stop_action:
then:
- stepper.set_target:
id: $mystepper
target: !lambda return id($mystepper).current_position;
- globals.set: # Set global to current position
id: ${mystepper}_global
value: !lambda return id($mystepper).current_position;
- cover.template.publish:
id: blinded
position: !lambda 'return (float(float(id($mystepper).current_position) / float(id(endstop))));'
current_operation: IDLE
has_position: true
device_class: blind
script:
- id: setupbutton
then:
- if:
condition:
- lambda: 'return (id(settingmode) == 3);'
then:
- logger.log: "Pressed Setup Button: Mode 3"
- logger.log: "Blind is now set up"
- logger.log:
format: "Endstop is: %d"
args: [ 'id(endstop)' ]
- stepper.set_target: # Set Stepper position
id: $mystepper
target: !lambda return id($mystepper).current_position;
- globals.set: # Set Endstop Variable
id: endstop
value: !lambda return id($mystepper).current_position;
- globals.set: # Set Global stepper position
id: ${mystepper}_global
value: !lambda return id($mystepper).current_position;
- globals.set: # Reset Setting Mode
id: settingmode
value: '0'
- globals.set: # Set toggle to Open
id: openclosed
value: '1'
- cover.template.publish:
id: blinded
state: OPEN
current_operation: IDLE
- logger.log: "Exiting Setting Mode"
- if:
condition:
- lambda: 'return (id(settingmode) == 2);'
then:
- logger.log: "Pressed Setup Button: Mode 2"
- logger.log: "Blind is Opening, Press button when fully open"
- stepper.report_position: # Reset Stepper position to 0
id: $mystepper
position: '0'
- stepper.set_target: # Reset Stepper position to 0
id: $mystepper
target: '0'
- globals.set: # Move stepper to 0 (doesn't move it's already there!)
id: ${mystepper}_global
value: '0'
- stepper.set_target: # Reset Stepper position to 500000
id: $mystepper
target: '500000'
- globals.set: # Advance setup to next mode
id: settingmode
value: '3'
- if:
condition:
- lambda: 'return (id(settingmode) == 1);'
then:
- logger.log: "Pressed Setup Button: Mode 1"
- logger.log: "Blind is Closing, Press button when fully closed"
- stepper.report_position: # Set Stepper position to 500000, makes it move to 0 (Closed)
id: $mystepper
position: '500000'
- globals.set: # Advance setup to next mode
id: settingmode
value: '2'