blob: 03d0235397c2f69b109ca3f39f0ed41368f8add6 (
plain)
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
|
substitutions:
# **** CHANGE FRIENDLY NAME TO SOMETHING UNIQUE PER DEVICE ****
friendly_name: Kauf Plug
esphome:
# **** CHANGE DEVICE NAME TO SOMETHING UNIQUE PER DEVICE ****
name: kauf-plug # **** RENAME YAML FILE TO SAME NAME ****
platform: ESP8266
board: esp01_1m
esp8266_restore_from_flash: true
# remove next line if renaming to something unique above
name_add_mac_suffix: true
project:
name: "kauf.plf10"
version: "1.7"
on_boot:
then:
# wait until ESPHome tries loading wifi credentials
- wait_until:
lambda: 'return ( wifi_wificomponent->tried_loading_creds );'
- if:
condition: # don't do anything if credentials were loaded unless force_ap is on as override
lambda: 'return ( wifi_wificomponent->loaded_creds && !id(force_ap) );'
then:
- logger.log: "------------------->>>>>>>>>>>>>>>>> already has wifi configured, leaving ap timeout cranked"
else: # if credentials not loaded or force_ap is set, lower ap timeout to allow saving new credentials
- logger.log: "------------------->>>>>>>>>>>>>>>>> wifi not configured, enabling ap and waiting"
# set ap timeout to 15 seconds in milliseconds.
- lambda: |-
wifi_wificomponent->set_ap_timeout(15000);
- wait_until:
wifi.connected
- logger.log: "------------------->>>>>>>>>>>>>>>>> wifi connected, cranking ap timeout back up"
- globals.set: # clear global
id: force_ap
value: 'false'
# set ap timeout to max 32 bit value in milliseconds. About 3.5 weeks.
- lambda: |-
wifi_wificomponent->set_ap_timeout(2147483647);
globals:
- id: force_ap
type: bool
restore_value: yes
initial_value: "true"
wifi:
# default credentials as backup. Also needed to show found networks in captive portal.
ssid: initial_ap2
password: asdfasdfasdfasdf
# Uncomment below to set a static IP
# manual_ip:
# static_ip: !secret kauf_bulb_ip_address
# gateway: !secret wifi_gateway
# subnet: !secret wifi_subnet
# dns1: !secret wifi_dns1
# use_address allows wireless programming through dashboard. remove after programming.
# use_address: 192.168.1.3
# default is 20, 17 is recommended.
output_power: 17
ap:
ssid: ${friendly_name} Hotspot
ap_timeout: 2147483647ms # default to max 32-bit value in milliseconds. About 3.5 weeks.
captive_portal: # for fallback wifi hotspot
logger: # Enable logging
# baud_rate: 0 # Disable UART logging since TX pad not easily available
api: # Enable Home Assistant API
# password: !secret api_password # optional password field for Home Assistant API.
ota:
# password: !secret ota_password # optional password for OTA updates.
debug: # outputs additional debug info when logs start
web_server: # web server allows access to device with a web browser
# auth: # optional login details for web interface
# username: admin
# password: !secret web_server_password
# host css and js file directly on device
css_include: "z_includes/webserver-v1.min.css"
css_url: ""
js_include: "z_includes/webserver-v1.min.js"
js_url: ""
# red led, blink when not connected to wifi or Home Assistant
status_led:
pin:
number: GPIO0
inverted: true
binary_sensor:
# button input toggles relay and thereby blue led
- platform: gpio
id: button
name: Kauf Plug Button
pin:
number: GPIO13
mode: INPUT_PULLUP
inverted: true
on_click:
- min_length: 30ms
max_length: 2s
then:
- if:
condition: # only toggle relay if button is enabled
lambda: 'return (id(select_button).state == "Enabled");'
then:
switch.toggle: relay
# restart with AP enabled if button is held for 5 seconds (up to 30 seconds because ESPHome requires a max).
# Not disabled by button disable option because you may not be able to re-enable button if wifi is not connecting.
# If your kid accidentally triggers this, the plug will just reboot and reconnect to wifi. Shouldn't harm anything.
# we have to restart because ESPHome's AP won't show the captive portal once wifi connects.
- min_length: 5s
max_length: 30s
then:
- logger.log: "------------------->>>>>>>>>>>>>>>>> HELD BUTTON 5 SECONDS, FORCING AP"
# blink LED for 10s then restart to get captive portal to turn on.
- globals.set:
id: force_ap
value: 'true'
- script.execute: blink_led
- delay: 10s
- switch.turn_on: restart_switch
# indicates whether plugged-in devices is running based on configurable threshold.
- platform: template
id: in_use
name: ${friendly_name} Device In Use
# blink LED forever. Used when button is held to re-enable AP. Stops blinking because plug restarts.
script:
- id: blink_led
mode: queued
then:
- switch.toggle: blue_led
- delay: 333ms
- script.execute: blink_led
- id: clear_wifi_script
then:
- lambda: |-
wifi_wificomponent->clear_stored_creds();
- delay: 2s
- switch.turn_on: restart_switch
switch:
# blue LED follows relay power state
- platform: gpio
id: blue_led
pin:
number: GPIO2
inverted: true
# relay output
- platform: gpio
id: relay
name: $friendly_name
pin: GPIO4
# automatically make blue led equal relay state
on_turn_on:
- if:
condition: # only if blue LED enabled
lambda: 'return (id(select_led).state == "Enabled");'
then:
switch.turn_on: blue_led
on_turn_off:
- switch.turn_off: blue_led
- platform: restart
id: restart_switch
- platform: template
id: clear_wifi_switch
name: Kauf Plug Clear WiFi Credentials
optimistic: true
disabled_by_default: true
# restore_state: no # no is default already per https://esphome.io/components/switch/template.html
on_turn_on:
then:
script.execute: clear_wifi_script
# clock input from Home Assistant used to calculate total daily energy
time:
- platform: homeassistant
id: homeassistant_time
sensor: # Power monitoring sensors output to Home Assistant
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: True
cf_pin: GPIO5
cf1_pin: GPIO14
current_resistor: 0.001 # The value of the shunt resistor for current measurement.
voltage_divider: 2401 # The value of the voltage divider on the board as (R_upstream + R_downstream) / R_downstream.
power:
name: ${friendly_name} Power
unit_of_measurement: W
id: wattage
filters:
- calibrate_linear:
- 0.0 -> 0.0
- 333.8 -> 60 # value with 60W bulb.
on_value: # set or clear in_use template binary sensor depending on whether power usage is over threshold
- if:
condition:
lambda: 'return (x >= id(threshold).state);'
then:
- binary_sensor.template.publish:
id: in_use
state: ON
else:
- binary_sensor.template.publish:
id: in_use
state: OFF
current:
name: ${friendly_name} Current
unit_of_measurement: A
filters:
- calibrate_linear:
- 0.0 -> 0.0
- 0.6 -> 0.515 # value with 60W bulb.
voltage:
name: ${friendly_name} Voltage
unit_of_measurement: V
filters:
- calibrate_linear:
- 0.0 -> 0.0
- 302.1 -> 117.1 # Tested using a meter
change_mode_every: 1
update_interval: 10s # 20 second effective update rate for Power, 40 second for Current and Voltage.
# Reports the total Power so-far each day, resets at midnight
# See https://esphome.io/components/sensor/total_daily_energy.html
- platform: total_daily_energy
name: ${friendly_name} Total Daily Energy
power_id: wattage
filters:
- multiply: 0.001 ## convert Wh to kWh
unit_of_measurement: kWh
number: # used as a threshold for whether the plugged-in devices is running.
- platform: template
name: ${friendly_name} Use Threshold
min_value: 1
max_value: 100
step: 1
initial_value: 3
id: threshold
optimistic: true # required for changing value from home assistant
restore_value: true
on_value:
- if: # set or clear in_use template binary sensor depending on whether power usage is above threshold
condition:
lambda: 'return (id(wattage).state >= x);'
then:
- binary_sensor.template.publish:
id: in_use
state: ON
else:
- binary_sensor.template.publish:
id: in_use
state: OFF
select:
# option to disable button
- platform: template
name: "Kauf Plug Button"
id: select_button
optimistic: true
options:
- Enabled
- Disabled
initial_option: Enabled
restore_value: true
icon: mdi:circle-double
# option to disable blue LED
- platform: template
name: "Kauf Plug LED"
id: select_led
optimistic: true
options:
- Enabled
- Disabled
initial_option: Enabled
restore_value: true
icon: mdi:led-on
on_value:
then:
- if:
condition:
lambda: 'return ( (id(select_led).state == "Enabled") && id(relay).state );'
then:
switch.turn_on: blue_led
else:
switch.turn_off: blue_led
# Send IP Address to HA
text_sensor:
- platform: wifi_info
ip_address:
name: $friendly_name IP Address
|