-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchbot-plug-mini-w2001401.yaml
377 lines (339 loc) · 12.5 KB
/
switchbot-plug-mini-w2001401.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
# SwitchBot Plug Mini (JP) for ESPHome by taikun114
substitutions:
device_name: switchbot-plug-mini
friendly_name: SwitchBot Plug Mini
fw_version: 1.1.0
# Model number
model: "W2001401"
# Calibration data
voltage_divider: "660.7973855893309"
current_resistor: "0.0024013086148925913"
current_multiply: "1.783420806116323"
# When calibrating, use the value shown on the power meter minus the power consumption of the
# Plug Mini itself (about 0.8W / 0.019A with the relay turned on) to calibrate.
#
# See the document below for calibration instructions.
# https://esphome.io/components/sensor/hlw8012#calibration
# Configuration for ESPHome
esphome:
name: ${device_name}
comment: Energy Monitoring Smart Plug (${model})
friendly_name: ${friendly_name}
name_add_mac_suffix: true
platformio_options:
board_build.flash_mode: dio
project:
name: SwitchBot (Customized by taikun114).Plug Mini JP (${model})
version: ${fw_version}
# Configuration for ESP32
esp32:
board: esp32-c3-devkitm-1
variant: ESP32C3
framework:
type: esp-idf
# Configuration to read this configuration file locally
dashboard_import:
package_import_url: github://taikun114/SwitchBot-Plug-Mini-for-ESPHome/switchbot-plug-mini-w2001401.yaml@v1.0.1
import_full_config: true
# Configuration for showing logs
logger:
# Configuration for API
api:
# Configuration for OTA (Over-The-Air) updates
ota:
- platform: esphome
# Configuration for Wi-Fi network
wifi:
# Configure the Wi-Fi network the Plug Mini will connect to
# ssid: !secret wifi_ssid
# password: !secret wifi_password
# Configure the fallback hotspot (access point mode), which is activated when
# Wi-Fi network is unable to connect for a certain period of time (1 minute by default)
ap:
# password: !secret ap_password # Remove comment and set a password is recommended.
# Blinking Blue LED when disconnected from Wi-Fi network
on_connect: # When connected to Wi-Fi network
- light.turn_off: blue_led
on_disconnect: # When disconnected from Wi-Fi network
- while:
condition:
not:
wifi.connected:
then:
- light.turn_on:
id: blue_led
brightness: 100%
- delay: 500ms
- light.turn_off: blue_led
- delay: 500ms
# Configure a captive portal that allows users to change Wi-Fi settings
# and update firmware on the Plug Mini when connected to a fallback hotspot
captive_portal:
# Configuration to allow Wi-Fi setup using Bluetooth LE
# Note: Since using this component increases internal temperature,
# it is recommended to disable (comment out) this component and configure Wi-Fi from the fallback hotspot.
esp32_improv:
authorizer: none
# Configuration for time
time:
# Synchronize time with Home Assistant through API connection
- platform: homeassistant
id: homeassistant_time
on_time: # When a specific time came
# Reset the total monthly energy sensor at 0 am on the first day of every month
- seconds: 0
minutes: 0
hours: 0
days_of_month: 1
then:
- sensor.integration.reset: total_monthly_energy
# Configuration for buttons
button:
# Reboot the Plug Mini
- platform: restart
name: Reboot
id: reboot
# Configuration for binary sensors
binary_sensor:
# Detects that a button on the Plug Mini unit has been pressed
- platform: gpio
id: power_button
pin:
number: GPIO02
inverted: true
internal: true
on_click: # When the button is pressed
# Toggle the relay switch when the button is pressed and immediately released.
- min_length: 50ms
max_length: 500ms
then:
- switch.toggle: relay
# Reboot the Plug Mini when the button is held between 3 and 10 seconds.
- min_length: 3s
max_length: 10s
then:
- button.press: reboot
# Configuration for sensors
sensor:
# Connection strength to Wi-Fi access point
- platform: wifi_signal
name: Wi-Fi Signal
update_interval: 30s
# Time of last power on
- platform: uptime
type: timestamp
name: Uptime
# Internal temperature of Plug Mini
- platform: internal_temperature
name: Internal Temperature
update_interval: 30s
# Power monitoring sensors
- platform: hlw8012
sel_pin:
number: GPIO20
inverted: true
cf_pin: GPIO18
cf1_pin: GPIO19
current_resistor: ${current_resistor}
voltage_divider: ${voltage_divider}
power:
name: Power
unit_of_measurement: W
id: wattage
accuracy_decimals: 1 # Number of decimal points
current:
name: Current
unit_of_measurement: A
id: current
filters:
- multiply: ${current_multiply}
voltage:
name: Voltage
unit_of_measurement: V
id: voltage
accuracy_decimals: 2 # Number of decimal points
initial_mode: VOLTAGE # Initial measurement mode. VOLTAGE or CURRENT
change_mode_every: 1
# Number of measurements before switching between current and voltage measurement modes
# (after switching, the initial value is discarded, so the set value + 1 measurement is taken)
update_interval: 5s
# About sensor update intervals
#
# The power sensor is updated at the interval set by update_interval.
# The current sensor and voltage sensor update intervals depend on the value set in change_mode_every.
#
# If change_mode_every: 1 or more:
# Update every: (value set in change_mode_every + 1) * 2 * value set in update_interval (in seconds)
# For example, if change_mode_every: 1 and update_interval: 1s, then (1 + 1) * 2 * 1s = update every 4 seconds.
#
# If change_mode_every: “never”:
# The sensor in the measurement mode set in initial_mode is updated every update_interval value.
# Unspecified sensors will not be updated. For example, if initial_mode: VOLTAGE, the current sensor will not be updated.
# Apparent power
# Note: This sensor is disabled by default because the accuracy is very low due to the different timing of current and voltage measurements.
- platform: template
name: Apparent Power
lambda: return id(voltage).state * id(current).state;
device_class: apparent_power
unit_of_measurement: VA
state_class: "measurement"
accuracy_decimals: 2 # Number of decimal points
update_interval: 20s # Please match the update intervals of the current and voltage sensors
disabled_by_default: true
# Power factor
# Note: This sensor is disabled by default because the accuracy is very low due to the different timing of current and voltage measurements.
- platform: template
name: Power Factor
lambda: !lambda |-
if ((id(wattage).state / (id(voltage).state * id(current).state)) > 1) {
return 1; // Return 1 if it is over 1
} else {
return id(wattage).state / (id(voltage).state * id(current).state); // Return the calculated value as is if it is less than 1
}
device_class: power_factor
state_class: "measurement"
accuracy_decimals: 2 # Number of decimal points
update_interval: 20s # Please match the update intervals of the current and voltage sensors
disabled_by_default: true
# Total daily energy consumption (reset daily at 0 am)
- platform: total_daily_energy
name: Total Daily Energy
power_id: wattage
accuracy_decimals: 3 # Number of decimal points
filters:
- multiply: 0.001 # Convert unit of measurement from Wh to kWh
unit_of_measurement: kWh
# Total monthly energy consumption (reset monthly at 0 am on the first day of every month)
- platform: integration
name: Total Monthly Energy
id: total_monthly_energy
sensor: wattage
time_unit: h
restore: true
state_class: total_increasing
device_class: energy
accuracy_decimals: 3 # Number of decimal points
filters:
- multiply: 0.001 # Convert unit of measurement from Wh to kWh
unit_of_measurement: kWh
# Total energy consumption since the Plug Mini was first set up with ESPHome
- platform: integration
name: Total Energy
sensor: wattage
time_unit: h
restore: true
state_class: total_increasing
device_class: energy
accuracy_decimals: 3 # Number of decimal points
filters:
- multiply: 0.001 # Convert unit of measurement from Wh to kWh
unit_of_measurement: kWh
# Configuration for switches
switch:
# Relay switch
- platform: gpio
name: Plug
pin: GPIO06
id: relay
device_class: outlet
restore_mode: RESTORE_DEFAULT_OFF # Relay switch state at startup of Plug Mini
# Available configuration:
#
# RESTORE_DEFAULT_OFF: Restore state if possible, turn off if not possible
# RESTORE_DEFAULT_ON: Restore state if possible, turn on if not possible
# RESTORE_INVERTED_DEFAULT_OFF: Restore inverted from previous state if possible, turn off if not possible
# RESTORE_INVERTED_DEFAULT_ON: Restore inverted from previous state if possible, turn on if not possible
# ALWAYS_OFF: Always turn off
# ALWAYS_ON: Always turn on
on_turn_on: # When the relay switch is turned on
- light.turn_on:
id: white_led
brightness: !lambda |-
if (id(led_brightness).state == "Bright") {
return 1; // When the brightness setting of the LED light is set to “Bright”, the White LED is turned on at 100% brightness
} else if (id(led_brightness).state == "Dim") {
return 0.5; // When the brightness setting of the LED light is set to “Dim”, the White LED is turned on at 50% brightness.
} else {
return 0; // When the brightness setting of the LED light is set to other than the above (Off), the White LED is turned on at 0% brightness (turned off)
}
on_turn_off: # When the relay switch is turned off
- light.turn_off: white_led
# Configuration for select
select:
# Brightness setting for built-in Plug Mini LED light
- platform: template
name: LED Brightness
id: led_brightness
icon: mdi:brightness-6
optimistic: true
options:
- "Bright"
- "Dim"
- "Off"
initial_option: "Bright"
restore_value: true
entity_category: config
on_value: # When the state is set
then:
- lambda: |-
if ( x == "Off") {
id(white_led).turn_off().perform(); // Turn off the White LED
} else if ( x == "Dim") {
if (id(relay).state) {
id(white_led).turn_on().set_brightness(0.5).perform(); // Turn on the White LED at 50% brightness only when the relay switch is on.
}
} else if ( x == "Bright") {
if (id(relay).state) {
id(white_led).turn_on().set_brightness(1.0).perform(); // Turn on the White LED at 100% brightness only when the relay switch is on.
}
}
# Configuration of output
output:
# White LED output
- platform: ledc
id: white_output
pin: GPIO07
inverted: true
frequency: 19531Hz
# Blue LED output
- platform: ledc
id: blue_output
pin: GPIO08
inverted: true
frequency: 19531Hz
# Configuration for lights
light:
# White LED light
- platform: monochromatic
output: white_output
name: White LED
id: white_led
icon: mdi:led-off
entity_category: config
default_transition_length: 250ms
restore_mode: RESTORE_DEFAULT_OFF # White LED light state at startup of Plug Mini
# Available configuration:
#
# RESTORE_DEFAULT_OFF: Restore state if possible, turn off if not possible
# RESTORE_DEFAULT_ON: Restore state if possible, turn on if not possible
# RESTORE_INVERTED_DEFAULT_OFF: Restore inverted from previous state if possible, turn off if not possible
# RESTORE_INVERTED_DEFAULT_ON: Restore inverted from previous state if possible, turn on if not possible
# ALWAYS_OFF: Always turn off
# ALWAYS_ON: Always turn on
# Blue LED light
- platform: monochromatic
output: blue_output
name: Blue LED
id: blue_led
icon: mdi:led-off
entity_category: config
default_transition_length: 250ms
restore_mode: ALWAYS_OFF # Blue LED light state at startup of Plug Mini
# Available configuration:
#
# RESTORE_DEFAULT_OFF: Restore state if possible, turn off if not possible
# RESTORE_DEFAULT_ON: Restore state if possible, turn on if not possible
# RESTORE_INVERTED_DEFAULT_OFF: Restore inverted from previous state if possible, turn off if not possible
# RESTORE_INVERTED_DEFAULT_ON: Restore inverted from previous state if possible, turn on if not possible
# ALWAYS_OFF: Always turn off
# ALWAYS_ON: Always turn on