NOUS A1T ESP Home Retain Switch after power loss/ Restore default

Hello all,

I am currently facing the following problem.
I have flashed a Nous A1T socket (WiFi model) with ESP Home, which works great so far.
Now the socket should be used to switch the Raspberry Pi, on which Home Assistant is running (via browser over the IP) if Home Assistant has hung up. Furthermore, if a power failure has occurred, the socket should assume the previously switched state.

Example:
Before power failure “On”, then this should take after voltage return also again the state “On”.
If the socket is “Off” before a power failure, it should also return to the “Off” state after the power returns.

Unfortunately, this does not work somehow and I have no idea what I could still change, the following I have already tried:

  1. Retain switch states on a power failure - ESPHome - Home Assistant Community (home-assistant.io)

  2. esp8266_restore_from_flash

  • RESTORE_DEFAULT_OFF (Default) - Attempt to restore state and default to OFF if not possible to restore.
  • RESTORE_DEFAULT_ON - Attempt to restore state and default to ON.
  • ALWAYS_OFF - Always initialize the pin as OFF on bootup.
  • ALWAYS_ON - Always initialize the pin as ON on bootup.

Dies ist mein Code, vielleicht fällt euch etwas ein oder auf:

substitutions:
  devicename: "nous_reserve"
  # Higher value gives lower watt readout
  current_res: "0.00280"
  # Lower value gives lower voltage readout
  voltage_div: "775"


esphome:
  name: $devicename
  comment: "Nous Smart Wifi Socket A1T (Tasmota)"
  name_add_mac_suffix: true
  project:
    name: "NOUS.Smart-Wifi-Socket"
    version: "A1T"

esp8266:
  board: esp8285
  restore_from_flash: true

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: XXXXXXXX
  password: XXXXXXXX
  fast_connect: on
  # static ip
  manual_ip:
    static_ip: XXXXXXXX
    gateway: XXXXXXXX
    subnet: XXXXXXXX
 # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${devicename}-AP"

captive_portal:

# see: https://esphome.io/components/time.html
time:
  - platform: homeassistant
    id: homeassistant_time

# Enable Web server
web_server:
  port: 80

light:
  - platform: status_led
    id: led
    pin:
      number: GPIO13
      inverted: true

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

  # toggle relay on/off
  - platform: gpio
    pin:
      number: GPIO00
      mode: INPUT_PULLUP
    id: "button_state"
    on_press:
      - switch.toggle: "button_switch"

switch:
  - platform: template
    name: "${devicename} - Switch"
    icon: mdi:power
    optimistic: true
    id: "button_switch"
    lambda: |-
      if (id(relay).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - switch.turn_on: relay
      - light.turn_on: led
    turn_off_action:
      - switch.turn_off: relay
      - light.turn_off: led
  - platform: gpio
    restore_mode: RESTORE_DEFAULT_ON
    pin: GPIO14
    id: relay

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} - Electric Consumption [kWh]"
    power_id: "nous_a1t_watt"
    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: GPIO04
    cf1_pin: GPIO05
    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: 2
      icon: mdi:current-ac
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.02 -> 0.000
            - 0.1 -> 0.19 
            - 2.5 -> 5.28
            - 4.2 -> 8.42
            #- 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:
      name: "${devicename} - Voltage"
      unit_of_measurement: V
      accuracy_decimals: 1
      icon: mdi:flash-outline
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 0.0
            - 255.0 -> 235.0   
            - 255.3 -> 235.1 

    power:
      name: "${devicename} - Power"
      id: "nous_a1t_watt"
      unit_of_measurement: W
      accuracy_decimals: 2
      icon: mdi:gauge
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 5.0 -> 0.00   
            - 25.6 -> 31.8 
            - 1178.0 -> 1222.0
            - 1922.0 -> 1951.05
            #- 3923.67700 -> 631.4
            #- 7109.50928 -> 1148.0
            #- 7237.0857 -> 1193.0
            #- 7426.71338 -> 1217.0
        - lambda: if (x < (3 + 1.14)) return 0; else return (x - 1.14);


text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${devicename} - IP Address"
    ssid:
      name: "${devicename} - Wi-Fi SSID"
    bssid:
      name: "${devicename} - Wi-Fi BSSID"
  - platform: version
    name: "${devicename} - ESPHome Version"
    hide_timestamp: true

Thanks in advance for the help

Hi, did you ever find a solution? I just put some NOUS A1T to work and one of them keeps turning off all the time, i guess after restarts on failed wifi connection.

Edit:

I double checked with another NOUS A1T. With the original Tasmota firmware the power state (on) is retained through a restart, does not even switch off/on in the process.

I could not find any solution how to achieve this with esphome, it always boots with the swith off.
Some recommended solutions like esp8266_restore_from_flash do not seem to exist anymore (error in yaml parsing).