forked from chabad-source/melachaplug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelachaplug.yaml
336 lines (300 loc) · 9 KB
/
melachaplug.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
#--------------------------------------------
#------------ User Configuration ------------
#--------------------------------------------
substitutions:
device_name: "melachaplug" # access web UI using melachaplug.local/ or it's IP
friendly_name: "Melacha Plug"
relay_1_gpio: "13" # Switch this section
button_1_gpio: "4" # based on your chip
built_in_led_gpio: "2"
led_inverted: "false"
deg_shabbos_start: "4" # check a bit early just in case (shkiah is at -0.833)
deg_shabbos_end: "-8.5" # time based off Alter Rebbe www.chabad.org/3209349
deg_tzeit: "-6"
timezone: America/New_York # check format en.wikipedia.org/wiki/List_of_tz_database_time_zones
latitude: "40.669010" # change to your location (important for proper zmanim)
longitude: "-73.942871" # 770 Chabad Lubavitch World Headquarters
# Switch this section based on your chip
# esp8266:
# board: nodemcuv2
# restore_from_flash: true
esp32:
board: esp32doit-devkit-v1
wifi:
ssid: !secret wifi_ssid2
password: !secret wifi_password2
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${friendly_name} Fallback Hotspot"
password: "" # password of fallback hotspot
#--------------------------------------------
#------------- Advanced Section -------------
#--------------------------------------------
captive_portal:
esphome:
name: ${device_name}
includes: shared/melachaplug.h
on_boot:
- priority: 800 # This is where all hardware initialization of vital components is executed
then:
- switch.turn_on: shabbos_mode # ensures the device starts off in Shabbos mode
- priority: -100.0 # pretty much everything should already be initialized
then:
- logger.log: "--- Waiting for time sync ---"
- wait_until:
lambda: 'return id(sntp_time).now().is_valid();'
- logger.log: "--- time & sun state set ---"
- lambda: 'MelachaPlug::onBoot();'
# frequency of data flushed to the flash
preferences:
flash_write_interval: 1min
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 0s # no need to connect to HA so no reboot necessary
# Enable Over The Air updates
ota:
# Enable Web Interface
web_server:
port: 80
include_internal: true
# Enable HTTP Requests (for auto location API)
http_request:
useragent: esphome/device
timeout: 10s
# Gets the current date and time
time:
- platform: sntp
id: sntp_time
timezone: ${timezone}
update_interval: 60min
on_time_sync:
then:
- lambda: 'MelachaPlug::onTimeSync();'
# Enable Sun calculations
sun:
id: mysun
latitude: ${latitude}
longitude: ${longitude}
on_sunset:
- elevation: ${deg_shabbos_start}° # degree to assume Shabbos or Yom Tov starts
then:
- lambda: 'MelachaPlug::onShabbosStart();'
- elevation: ${deg_shabbos_end}° # degree that Shabbos or Yom Tov Ends
then:
- lambda: 'MelachaPlug::onShabbosEnd();'
# Sensors
sensor:
- platform: sun
name: ${friendly_name} Sun Elevation
type: elevation
internal: true
# Text Sensors
text_sensor:
- platform: sun
name: ${friendly_name} Early Shabbos and Yom Tov Start (elevated ${deg_shabbos_start}°)
internal: true
id: ts_shabbos_start
type: sunset
format: "%l:%M:%S %p, %A"
update_interval: never
elevation: ${deg_shabbos_start}°
- platform: sun
name: ${friendly_name} Next Sunset (elevated -0.833° - default)
internal: true
id: ts_sunset
type: sunset
format: "%l:%M:%S %p, %A"
update_interval: never
- platform: sun
name: ${friendly_name} Tzeit Hakochavim - Nightfall (elevated ${deg_tzeit}°)
internal: true
id: ts_tzeit
type: sunset
format: "%l:%M:%S %p, %A"
update_interval: never
elevation: ${deg_tzeit}°
- platform: sun
name: ${friendly_name} Shabbos and Yom Tov Ends (elevated ${deg_shabbos_end}°)
internal: true
id: ts_shabbos_end
type: sunset
format: "%l:%M:%S %p, %A"
update_interval: never
elevation: ${deg_shabbos_end}°
- platform: template
name: ${friendly_name} Hebrew Date
internal: true
id: hebrew_date
update_interval: never
# text sensor to display the auto generated location
- platform: template
name: ${friendly_name} Auto Location Found
internal: true
id: auto_location_found
update_interval: never
- platform: template
name: ${friendly_name} Latitude
id: lat_text_sensor
update_interval: never
on_value:
then:
lambda: 'MelachaPlug::onLatitudeSet(x);'
- platform: template
name: ${friendly_name} Longitude
id: lon_text_sensor
update_interval: never
on_value:
then:
lambda: 'MelachaPlug::onLongitudeSet(x);'
- platform: template
name: ${friendly_name} Keyboard
internal: true
id: keyboard
update_interval: never
# variables publicly (internally) available
globals:
- id: deg_shabbos_start_global
type: float
restore_value: no
initial_value: ${deg_shabbos_start}
- id: deg_shabbos_end_global
type: float
restore_value: no
initial_value: ${deg_shabbos_end}
- id: lat_global
type: float
restore_value: yes
initial_value: ${latitude}
- id: lon_global
type: float
restore_value: yes
initial_value: ${longitude}
- id: lat_and_lon_global
type: std::string
restore_value: no
initial_value: to_string(${latitude}) + "," + to_string(${longitude})
binary_sensor:
- platform: gpio
name: ${friendly_name} Button 1
id: button_1
pin:
number: ${button_1_gpio}
mode: INPUT_PULLUP
inverted: true
filters:
- delayed_on: 10ms
on_click:
then:
- switch.toggle: relay_1
switch:
- platform: gpio
name: ${friendly_name} Relay 1
id: relay_1
pin: ${relay_1_gpio}
restore_mode: "ALWAYS_OFF" # defaults to off in case of reset on Shabbos or Yom Tov
on_turn_on:
then:
- switch.turn_on: built_in_led
on_turn_off:
then:
- switch.turn_off: built_in_led
# switch which can disable the button
- platform: gpio
name: ${friendly_name} Enable Button 1
internal: true
id: enable_1_switch
pin: ${button_1_gpio}
inverted: false
restore_mode: "ALWAYS_OFF"
- platform: gpio
name: ${friendly_name} Built-in LED
disabled_by_default: true
id: built_in_led
pin: ${built_in_led_gpio}
restore_mode: "ALWAYS_OFF"
inverted: ${led_inverted}
# Shabbos mode switch - here is where we set what happens when it turns on and off
- platform: template
name: ${friendly_name} Shabbos Mode
id: shabbos_mode
optimistic: true # set to true for status to be updated immediately
turn_on_action:
then:
- switch.turn_off: relay_1 # turn off plug for Shabbos and Yom Tov
- switch.turn_off: enable_1_switch # disables button for Shabbos and Yom Tov
- switch.turn_on: flash_led
turn_off_action:
then:
- switch.turn_off: flash_led # must be before relay
- switch.turn_on: enable_1_switch # re-enable button for weekdays
- switch.turn_on: relay_1 # turn on plug for weekday
# LED flash preset
- platform: template
name: ${friendly_name} Flash LED
disabled_by_default: true
optimistic: yes
id: flash_led
turn_on_action:
- while:
condition:
lambda: 'return true;'
then:
- switch.turn_on: built_in_led
- delay: 75ms
- switch.turn_off: built_in_led
- delay: 75ms
- switch.turn_on: built_in_led
- delay: 75ms
- switch.turn_off: built_in_led
- delay: 2875ms
turn_off_action:
- switch.turn_off: built_in_led
# update auto detect location
- platform: template
name: ${friendly_name} Auto Detect Location
disabled_by_default: true
optimistic: yes
turn_on_action:
- lambda: 'MelachaPlug::autoSetLocation();'
turn_off_action:
- text_sensor.template.publish:
id: lat_text_sensor
state: "0.0"
- text_sensor.template.publish:
id: lon_text_sensor
state: "0.0"
- text_sensor.template.publish:
id: auto_location_found
state: "Wait 60 seconds then reboot for changes to take effect"
# To update the lat & lon from the web UI we use a select component
select:
- platform: template
name: "Keyboard Select"
internal: true
id: keyboard_select
update_interval: never
optimistic: true
initial_option: ""
options:
- "Repeat last character"
- "1"
- "2"
- "3"
- "4"
- "5"
- "6"
- "7"
- "8"
- "9"
- "0"
- "-"
- "."
- "BACKSPACE"
- "CLEAR"
- "Save to Latitude"
- "Save to Longitude"
- ""
on_value:
- lambda: 'MelachaPlug::onKeyboardSelect(x);'