Restore state

Hi, I have a power outlet and it’s working fine but after reboot or home assistant restart, the switch is powered on (it has voltage) but shows as off in HA


#
# gosund sp111 smart plug with power sensor
#
substitutions:
  devicename: "gosundsp111"
  upper_devicename: "Gosund SP111"
  # Higher value gives lower watt readout
  current_res: "0.00230"
  # Lower value gives lower voltage readout
  voltage_div: "733"


esphome:
  name: $devicename
  platform: ESP8266
  board: esp8285
  esp8266_restore_from_flash: true

# WiFi connection
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: on

  manual_ip:
    static_ip: !secret static_ip
    gateway: !secret gateway
    subnet: 255.255.255.0
  
  ap:
#    ssid: "gosund-sp111"
    ssid: "${devicename} Hotspot"
    password: !secret hotspot_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

# Enable over-the-air updates
ota:

# Enable Web server
web_server:
  port: 80
  auth:
    username: admin
    password: !secret web_server_password
  
time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: version
    name: "${devicename} - Version"
    icon: mdi:cube-outline

binary_sensor:
  - platform: status
    name: "${devicename} - Status"
    device_class: connectivity

  # toggle relay on/off
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    id: "${devicename}_button_state"
    on_press:
      - switch.toggle: button_switch

sensor:
  - platform: wifi_signal
    name: "${devicename} - Wifi Signal"
    update_interval: 60s
    icon: mdi:wifi

  - platform: uptime
    name: "${devicename} - Uptime"
    update_interval: 60s
    icon: mdi:clock-outline

  - platform: total_daily_energy
    name: "${devicename} - Todays Usage"
    power_id: "power_wattage"
    filters:
      # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:calendar-clock

  - platform: adc
    pin: VCC
    name: "${devicename} - VCC Volt"
    icon: mdi:flash-outline

  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO05
    cf1_pin: GPIO04
    change_mode_every: 4
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}
    update_interval: 3s

    current:
      name: "${devicename} - Ampere"
      unit_of_measurement: A
      accuracy_decimals: 3
      icon: mdi:current-ac

    voltage:
      name: "${devicename} - Volt"
      unit_of_measurement: V
      accuracy_decimals: 1
      icon: mdi:flash-outline

    power:
      name: "${devicename} - Watt"
      unit_of_measurement: W
      id: "power_wattage"
      icon: mdi:gauge

status_led:
  pin:
    number: GPIO02
    inverted: True
  id: led_blue

output:
  - platform: gpio
    pin: GPIO00
    inverted: true
    id: led_red

switch:
  - platform: template
    name: "${devicename} - Switch"
    icon: mdi:power
    optimistic: true
    id: button_switch
    turn_on_action:
      - switch.turn_on: relay
    turn_off_action:
      - switch.turn_off: relay
  - platform: gpio
    pin: GPIO15
    id: relay

Screenshot 2022-03-11 151940
Screenshot 2022-03-11 152019

try

esphome:
    esp8266_restore_from_flash: true

It’s already there

I added “lambda” line and now retain state after reboot

switch:
  - platform: template
    name: "${devicename} - Switch"
    icon: mdi:power
    optimistic: true
    id: button_switch
    lambda: 'return id(relay).state;'
    turn_on_action:
      - switch.turn_on: relay
    turn_off_action:
      - switch.turn_off: relay
  - platform: gpio
    pin: GPIO15
    id: relay
1 Like