-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalBytes-ESPHome.yaml
375 lines (336 loc) · 8.62 KB
/
LocalBytes-ESPHome.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
# Localbytes
# Substitutions
substitutions:
# Device Names
device_name: "device-name"
friendly_name: "Device Name Plug"
device_description: "Smart Plug"
# Icon
main_icon: "power-socket-uk"
# Default Relay State
# Aka: `restore_mode` in documentation
# Options: `RESTORE_DEFAULT_OFF`, `RESTORE_DEFAULT_ON`, `ALWAYS_ON` & `ALWAYS_OFF`
default_state: "RESTORE_DEFAULT_OFF"
# Activity State Threshold
# Threshold (number) that the device will change from `Idle` to `Active` if power is greater than or equal to
activity_threshold: "5"
# Basic Config
esphome:
name: "${device_name}"
comment: '${device_description}'
esp8266:
board: esp01_1m
framework:
version: recommended
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: !secret api_encryption_key
services:
- service: calibrate_voltage
variables:
actual_value: float
then:
- lambda: |-
id(voltage_multiply) = actual_value / id(voltage).raw_state;
- number.set:
id: voltage_factor
value: !lambda "return id(voltage_multiply);"
- service: calibrate_power
variables:
actual_value: float
then:
- lambda: |-
id(power_multiply) = actual_value / id(power).raw_state;
- number.set:
id: power_factor
value: !lambda "return id(power_multiply);"
- service: calibrate_current
variables:
actual_value: float
then:
- lambda: |-
id(current_multiply) = actual_value / id(current).raw_state;
- number.set:
id: current_factor
value: !lambda "return id(current_multiply);"
globals:
- id: voltage_multiply
type: float
restore_value: true
initial_value: "0.838916"
- id: power_multiply
type: float
restore_value: true
initial_value: "0.917278"
- id: current_multiply
type: float
restore_value: true
initial_value: "0.838916"
# OTA Updates Password
ota:
password: "ENTER OTA PASSWORD HERE"
# Enable Webserver
web_server:
port: 80
auth:
username: !secret web_server_username
password: !secret web_server_password
ota: true
# Wifi Setup
wifi:
ssid: !secret Wifi_SSID
password: !secret Wifi_Password
power_save_mode: light
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${device_name} Hotspot"
password: !secret Fallback_Wifi_Password
# Enable captive portal on fallback wifi
captive_portal:
time:
- platform: homeassistant
# Device Specific Config
binary_sensor:
# Push Button (Toggles Relay When Pressed)
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
name: "${friendly_name} Button"
on_click:
- max_length: 1s
then:
if:
condition:
switch.is_off: disable_button
then:
switch.toggle: relay
- min_length: 1.5s
max_length: 5s
then:
switch.toggle: disable_led
- min_length: 8s
max_length: 12s
then:
switch.toggle: disable_button
- platform: status
name: "${friendly_name} Server Status"
switch:
# Relay (As Switch)
- platform: gpio
name: "${friendly_name}"
icon: "mdi:${main_icon}"
pin: GPIO14
id: relay
restore_mode: "${default_state}"
on_turn_on:
- light.turn_on:
id: led
on_turn_off:
- light.turn_off:
id: led
- platform: template
name: "${friendly_name} Disable LED"
id: disable_led
icon: "mdi:led-variant-off"
restore_mode: "RESTORE_DEFAULT_OFF"
optimistic: true
on_turn_on:
# Flash twice
- light.turn_off: led
- delay: 0.1s
- light.turn_on: led
- delay: 0.1s
- light.turn_off: led
- delay: 0.1s
- light.turn_on: led
- delay: 0.1s
# Final state
- light.turn_off: led
on_turn_off:
# Flash twice
- light.turn_on: led
- delay: 0.1s
- light.turn_off: led
- delay: 0.1s
- light.turn_on: led
- delay: 0.1s
- light.turn_off: led
- delay: 0.7s
# Final state
- if:
condition:
switch.is_on: relay
then:
light.turn_on: led
- platform: template
name: "${friendly_name} Disable Button"
id: disable_button
icon: "mdi:toggle-switch-off-outline"
restore_mode: "RESTORE_DEFAULT_OFF"
optimistic: true
on_turn_on:
# Flash thrice
- light.turn_off: led
- delay: 0.15s
- light.turn_on: led
- delay: 0.15s
- light.turn_off: led
- delay: 0.15s
- light.turn_on: led
- delay: 0.15s
- light.turn_off: led
- delay: 0.15s
- light.turn_on: led
- delay: 0.15s
# Final state
- if:
condition:
switch.is_off: relay
then:
light.turn_off: led
on_turn_off:
# Flash thrice
- light.turn_on: led
- delay: 0.15s
- light.turn_off: led
- delay: 0.15s
- light.turn_on: led
- delay: 0.15s
- light.turn_off: led
- delay: 0.15s
- light.turn_on: led
- delay: 0.15s
- light.turn_off: led
- delay: 0.7s
# Final state
- if:
condition:
switch.is_on: relay
then:
light.turn_on: led
sensor:
# WiFi Signal Sensor
- platform: wifi_signal
name: "${friendly_name} WiFi Status"
update_interval: 60s
# Power Monitoring
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: true
cf_pin: GPIO4
cf1_pin: GPIO05
current_resistor: 0.0025
voltage_divider: 880
change_mode_every: 3
update_interval: 6s
voltage:
name: "${friendly_name} Voltage"
id: voltage
unit_of_measurement: V
accuracy_decimals: 1
filters:
- lambda: return x * id(voltage_multiply);
power:
name: "${friendly_name} Power"
id: power
unit_of_measurement: W
accuracy_decimals: 0
filters:
- lambda: return x * id(power_multiply);
current:
name: "${friendly_name} Current"
id: current
unit_of_measurement: A
accuracy_decimals: 2
filters:
- lambda: return x * id(current_multiply);
# Total daily energy sensor
- platform: total_daily_energy
name: "${friendly_name} Daily Energy"
power_id: power
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
unit_of_measurement: kWh
# Make calibration factor data readable/setable from home assistant
number:
- platform: template
name: "${friendly_name} Voltage Calibration Factor"
id: voltage_factor
icon: "mdi:sine-wave"
min_value: 0
max_value: 10
step: 0.001
entity_category: diagnostic
mode: box
lambda: |-
return id(voltage_multiply);
set_action:
lambda: |-
id(voltage_multiply) = x;
- platform: template
name: "${friendly_name} Power Calibration Factor"
id: power_factor
icon: "mdi:flash"
min_value: 0
max_value: 10
step: 0.001
entity_category: diagnostic
mode: box
lambda: |-
return id(power_multiply);
set_action:
lambda: |-
id(power_multiply) = x;
- platform: template
name: "${friendly_name} Current Calibration Factor"
id: current_factor
icon: "mdi:current-ac"
min_value: 0
max_value: 10
step: 0.001
entity_category: diagnostic
mode: box
lambda: |-
return id(current_multiply);
set_action:
lambda: |-
id(current_multiply) = x;
text_sensor:
# Device Activity State
# E.G. shows as active if power reading above a certain threshold
- platform: template
name: "${friendly_name} Activity State"
icon: "mdi:${main_icon}"
lambda: |-
if (id(power).state >= $activity_threshold) {
return {"Active"};
} else {
return {"Idle"};
}
update_interval: 5s
# Relay State LED
output:
- platform: esp8266_pwm
id: state_led
pin:
number: GPIO13
inverted: true
light:
- platform: binary
output: state_led
id: led
status_led:
pin:
number: GPIO1
inverted: True
button:
- platform: restart
id: restart_button
name: "${friendly_name} Restart"