Skip to content

Instantly share code, notes, and snippets.

@andrew-codechimp
Created November 17, 2022 10:07
Show Gist options
  • Save andrew-codechimp/280e3d48ab12190d3ddf57dae6ebb1a3 to your computer and use it in GitHub Desktop.
Save andrew-codechimp/280e3d48ab12190d3ddf57dae6ebb1a3 to your computer and use it in GitHub Desktop.
Gosund SP111 Smart Plug ESPHome
#
# gosund sp111 smart plug with power sensor
#
substitutions:
devicename: esphplug01
upper_devicename: EsphPlug01
# WiFi connection
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: ${upper_devicename}
password: !secret ap_password
captive_portal:
# Enable over-the-air updates
ota:
# Enable Home Assistant API
api:
packages:
gosundplug: !include common/gosundplug.yaml
#
# gosund sp111 smart plug with power sensor
#
esphome:
name: ${devicename}
platform: ESP8266
board: esp8285
esp8266_restore_from_flash: true
on_boot:
priority: -100
then:
- if:
condition:
- lambda: 'return id(relay_startup_state);'
then:
- switch.turn_on: ${devicename}_relay
logger:
globals:
- id: led_enabled
type: bool
restore_value: yes
initial_value: "true"
- id: relay_startup_state
type: bool
restore_value: yes
initial_value: "false"
binary_sensor:
- platform: gpio
pin:
number: GPIO3
inverted: True
name: ${upper_devicename} Power Button
on_press:
- switch.toggle: ${devicename}_relay
light:
- platform: status_led
id: led
restore_mode: ALWAYS_OFF
pin:
number: GPIO1
inverted: True
switch:
- platform: gpio
name: ${upper_devicename} Relay
pin: GPIO14
id: ${devicename}_relay
restore_mode: ALWAYS_OFF
on_turn_on:
then:
- if:
condition:
- lambda: 'return id(led_enabled);'
then:
- light.turn_on: led
on_turn_off:
- light.turn_off: led
- platform: template
name: ${upper_devicename} LED Enabled
entity_category: config
lambda: |-
if (id(led_enabled)) {
return true;
} else {
return false;
}
turn_on_action:
- globals.set:
id: led_enabled
value: 'true'
- if:
condition:
switch.is_on: ${devicename}_relay
then:
- light.turn_on: led
turn_off_action:
- light.turn_off: led
- globals.set:
id: led_enabled
value: 'false'
- platform: template
name: ${upper_devicename} relay power on state
entity_category: config
lambda: |-
if (id(relay_startup_state)) {
return true;
} else {
return false;
}
turn_on_action:
- globals.set:
id: relay_startup_state
value: 'true'
turn_off_action:
- globals.set:
id: relay_startup_state
value: 'false'
sensor:
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: True
cf_pin: GPIO04
cf1_pin: GPIO05
current_resistor: 0.00221
voltage_divider: 871
current:
name: ${upper_devicename} Current
unit_of_measurement: A
voltage:
name: ${upper_devicename} Voltage
unit_of_measurement: V
power:
name: ${upper_devicename} Wattage
id: energy_temp_Wattage
unit_of_measurement: W
change_mode_every: 8
update_interval: 10s
- platform: total_daily_energy
name: ${upper_devicename} Total Daily Energy
power_id: energy_temp_Wattage
restore: false
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
unit_of_measurement: kWh
# Enable time component to reset energy at midnight
time:
- platform: sntp
id: my_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment