Skip to content

Instantly share code, notes, and snippets.

@frenck
Last active February 21, 2023 14:39
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save frenck/341323998fbc2557d44608b5bd634b88 to your computer and use it in GitHub Desktop.
Save frenck/341323998fbc2557d44608b5bd634b88 to your computer and use it in GitHub Desktop.
Blog: Calibrating an ESPHome flashed power plug: https://frenck.dev/calibrating-an-esphome-flashed-power-plug/
# Current sensor
current:
name: current
unit_of_measurement: A
accuracy_decimals: 3
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 0.013
- 0.08208 -> 0.071
- 1.34223 -> 1.066
- 5.57170 -> 4.408
- 6.69184 -> 5.259
- 6.97187 -> 5.540
# Make everything below 0.01A appear as just 0A.
# Furthermore it corrects 0.013A for the power usage of the plug.
- lambda: if (x < (0.01 - 0.013)) return 0; else return (x - 0.013)
# Power sensor
power:
id: power
name: esphome_esp08_power
unit_of_measurement: W
accuracy_decimals: 0
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 1.14
- 62.06167 -> 10.93
- 1503.27161 -> 247.6
- 1599.81213 -> 263.7
- 3923.67700 -> 631.4
- 7109.50928 -> 1148.0
- 7237.0857 -> 1193.0
- 7426.71338 -> 1217.0
# Make everything below 2W appear as just 0W.
# Furthermore it corrects 1.14W for the power usage of the plug.
- lambda: if (x < (2 + 1.14)) return 0; else return (x - 1.14);
# Voltage sensor
voltage:
name: esphome_esp08_voltage
unit_of_measurement: V
accuracy_decimals: 1
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 0.0
- 602.87506 -> 229.9
- 609.8 -> 232.8
---
esphome:
name: powerplug
platform: ESP8266
board: esp8285
# WiFi connection
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable logging
logger:
# Enable Home Assistant API
api:
# Enable over-the-air updates
ota:
# Enable Web server
web_server:
port: 80
sensor:
# Power sensor
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: true
cf_pin: GPIO04
cf1_pin: GPIO05
change_mode_every: 3
update_interval: 3s
# Current sensor
current:
name: current
unit_of_measurement: A
accuracy_decimals: 3
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 0.013
- 0.08208 -> 0.071
- 1.34223 -> 1.066
- 5.57170 -> 4.408
- 6.69184 -> 5.259
- 6.97187 -> 5.540
# Make everything below 0.01A appear as just 0A.
# Furthermore it corrects 0.013A for the power usage of the plug.
- lambda: if (x < (0.01 - 0.013)) return 0; else return (x - 0.013);
# Voltage sensor
voltage:
name: voltage
unit_of_measurement: V
accuracy_decimals: 1
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 0.0
- 602.87506 -> 229.9
- 609.8 -> 232.8
# Power sensor
power:
id: power
name: power
unit_of_measurement: W
accuracy_decimals: 0
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 1.14
- 62.06167 -> 10.93
- 1503.27161 -> 247.6
- 1599.81213 -> 263.7
- 3923.67700 -> 631.4
- 7109.50928 -> 1148.0
- 7237.0857 -> 1193.0
- 7426.71338 -> 1217.0
# Make everything below 2W appear as just 0W.
# Furthermore it corrects 1.14W for the power usage of the plug.
- lambda: if (x < (2 + 1.14)) return 0; else return (x - 1.14);
binary_sensor:
# Binary sensor for the button press
- platform: gpio
name: button
pin:
number: GPIO3
inverted: true
on_press:
- switch.toggle: relay
switch:
# Switch to toggle the relay
- platform: gpio
id: relay
name: switch
pin: GPIO14
on_turn_on:
- light.turn_on: led
on_turn_off:
- light.turn_off: led
output:
# Relay state led
- platform: esp8266_pwm
id: state_led
pin:
number: GPIO1
inverted: true
light:
# Relay state light
- platform: monochromatic
output: state_led
id: led
# Uses the red LED as a status indicator
status_led:
pin:
number: GPIO13
inverted: true
---
esphome:
name: powerplug
platform: ESP8266
board: esp8285
# WiFi connection
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable logging
logger:
# Enable Home Assistant API
api:
password: !secret esphome_api_password
# Enable over-the-air updates
ota:
password: !secret esphome_ota_password
# Enable Web server
web_server:
port: 80
# Sync time with Home Assistant
time:
- platform: homeassistant
id: homeassistant_time
# Text sensors with general information
text_sensor:
- platform: version
name: version
- platform: wifi_info
ip_address:
name: ip
ssid:
name: ssid
bssid:
name: bssid
sensor:
# Uptime sensor
- platform: uptime
name: uptime
# WiFi Signal sensor
- platform: wifi_signal
name: wifi_signal
update_interval: 10s
# Power sensor
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: true
cf_pin: GPIO04
cf1_pin: GPIO05
change_mode_every: 3
update_interval: 3s
# Current sensor
current:
name: current
unit_of_measurement: A
accuracy_decimals: 3
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 0.013
- 0.08208 -> 0.071
- 1.34223 -> 1.066
- 5.57170 -> 4.408
- 6.69184 -> 5.259
- 6.97187 -> 5.540
# Make everything below 0.01A appear as just 0A.
# Furthermore it corrects 0.013A for the power usage of the plug.
- lambda: if (x < (0.01 - 0.013)) return 0; else return (x - 0.013);
# Voltage sensor
voltage:
name: voltage
unit_of_measurement: V
accuracy_decimals: 1
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 0.0
- 602.87506 -> 229.9
- 609.8 -> 232.8
# Power sensor
power:
id: power
name: power
unit_of_measurement: W
accuracy_decimals: 0
filters:
# Map from sensor -> measured value
- calibrate_linear:
- 0.0 -> 1.14
- 62.06167 -> 10.93
- 1503.27161 -> 247.6
- 1599.81213 -> 263.7
- 3923.67700 -> 631.4
- 7109.50928 -> 1148.0
- 7237.0857 -> 1193.0
- 7426.71338 -> 1217.0
# Make everything below 2W appear as just 0W.
# Furthermore it corrects 1.14W for the power usage of the plug.
- lambda: if (x < (2 + 1.14)) return 0; else return (x - 1.14);
# Total daily energy sensor
- platform: total_daily_energy
name: daily_energy
power_id: power
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
unit_of_measurement: kWh
binary_sensor:
# Binary sensor for the button press
- platform: gpio
name: button
pin:
number: GPIO3
inverted: true
on_press:
- switch.toggle: relay
switch:
# Switch to restart the plug
- platform: restart
name: restart
# Switch to toggle the relay
- platform: gpio
id: relay
name: switch
pin: GPIO14
on_turn_on:
- light.turn_on: led
on_turn_off:
- light.turn_off: led
output:
# Relay state led
- platform: esp8266_pwm
id: state_led
pin:
number: GPIO1
inverted: true
light:
# Relay state light
- platform: monochromatic
output: state_led
id: led
# Uses the red LED as a status indicator
status_led:
pin:
number: GPIO13
inverted: true
---
esphome:
name: powerplug
platform: ESP8266
board: esp8285
# WiFi connection
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable logging
logger:
# Enable Home Assistant API
api:
# Enable over-the-air updates
ota:
# Enable Web server
web_server:
port: 80
sensor:
# Power sensor
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: true
cf_pin: GPIO04
cf1_pin: GPIO05
change_mode_every: 3
update_interval: 3s
# Current sensor
current:
name: current
unit_of_measurement: A
accuracy_decimals: 3
# Voltage sensor
voltage:
name: voltage
unit_of_measurement: V
accuracy_decimals: 1
# Power sensor
power:
id: power
name: power
unit_of_measurement: W
accuracy_decimals: 0
binary_sensor:
# Binary sensor for the button press
- platform: gpio
name: button
pin:
number: GPIO3
inverted: true
on_press:
- switch.toggle: relay
switch:
# Switch to toggle the relay
- platform: gpio
id: relay
name: switch
pin: GPIO14
on_turn_on:
- light.turn_on: led
on_turn_off:
- light.turn_off: led
output:
# Relay state led
- platform: esp8266_pwm
id: state_led
pin:
number: GPIO1
inverted: true
light:
# Relay state light
- platform: monochromatic
output: state_led
id: led
# Uses the red LED as a status indicator
status_led:
pin:
number: GPIO13
inverted: true
@shbatm
Copy link

shbatm commented Sep 5, 2019

Line 77 and 108 of the combined firmware lambda functions are different -- one adds the offset the other subtracts the offset. Shouldn't they both be addition in the first half of the function?

@Harvsch
Copy link

Harvsch commented Oct 26, 2021

This calibration looks useful, I have some caveats.
Power measurement is highly dependent on the power factor of the load. Power factor is the cosine of the angle between applied Voltage and load current. I expect that the power measurement of these sockets does not apply a power factor correction. If that is true, power measurements for loads with motors or switch mode power supplies will not be accurate. Calibrating with the intended load will come closer but, even then, accuracy will vary with load.
Current measurements will be distorted for switch mode power supplies, because their current draw is not continuous. Most modern line operated power supplies are switch mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment