Relays turn off when rebooting with bk7231t (WB2S)

I recently bought some tuya devices on sale when I realised I could flash them with esphome. The only problem is that the relays are turning off when it reboots (when I OTA flash it). I’ve tried a few things: using on_boot to turn the relays on, changing the restore_mode, removing everything not needed in the config etc but no matter what the relays always turn off.

I sadly didn’t check if this happened with openbeken. Can anyone help or give suggestions?
The esphome device is Deta Grid Connect Double Powerpoint with Dual USB 6920HA | ESPHome Devices
The openbeken device is Smart Double Power Point Series 2

Thanks for any help.

Hello,

when esphome is rebooting or updating it will momentarly reset, so you will have a moment the ralay is OFF, as soon as it has booted it will set the relay in the requested condition. For example on boot turn on, this action will happen as soon as the boot is completed.
The is no way to avoid the ralay to change condition while powering on or rebooting after an update or similar.

I have other esphome smart plugs and I’ve never encountered a device powering off on OTA flashing. I thought maybe this was something to do with the Tuya chip.

Hi,
can I ask you which device with relay does not power off? even if these are US plug, because I am looking at something similar.
In my experience any relay device will switch off during an update, it is my experience.

The most recent ones are these esp32 ones https://www.athom.tech/esphome

But I have random old tuya esp8266 devices which have been using esphome for years and I’ve never noticed this before.

Hello,

if the products is this https://www.athom.tech/blank-1/esp32-c3-us-plug-for-esphome, or anyway the esp32 based there is the code available at esp32-configs/athom-smart-plug.yaml at fd1e0d4310ad30305c38b16fb76121e1f738e3c3 · athom-tech/esp32-configs · GitHub.

looking at the code they restore the relay condition:
# Restore the relay (GPO switch) upon reboot to state: relay_restore_mode: RESTORE_DEFAULT_ON
So they restore the mode when booting.

that is my understanding :slight_smile:

I have a similar issue with a plug with relay lying around; I just wanted only to monitor current draw roughly but this might switch on/off in some condition, eventually I will use a monitor device only.

What is your end user requirement? Heart pacemaker? ISS Oxygen generator?

There are commands in both OpenBeken and ESPHome to set relay status to on at powerup, but the mechanical nature of relays is that the unpowered status is usually off for one portion of a SPST relay, while the other part is on.
If you want SPDT relay rather than SPST, then you may have to externally connect an appropriate relay.

Any code?

My end user requirement is power monitoring my modem and server so yes, I’d prefer them to not be powered down.

I’m just shocked that a 5 year old esp8266 device I bought from a dolar store can stay powered during reboot but a fairly recent and much more expensive device can’t. I assumed there must be a bug in esphome or there was a setting I was missing for the bk7231T.

Below is my yaml:

substitutions:
  devicename: "deta-6920ha-ff439bec"
  friendlyname: Deta Powerpoint USB
  friendlyname_left: Powerpoint Left
  friendlyname_right: Powerpoint Right
  deviceid: deta-6920ha-ff439bec
  devicemodel: Deta Grid Connect 6920HA Series 2
  relay_restore_mode_left: RESTORE_DEFAULT_ON
  relay_restore_mode_right: RESTORE_DEFAULT_ON
  relay_lock_left: false
  relay_lock_right: false
  apikey: "key"
  otapass: "pass"


esphome:
  name: ${devicename}
  friendly_name: ${devicename}
  

bk72xx:
  board: generic-bk7231t-qfn32-tuya

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: ${apikey}

ota:
  - platform: esphome
    password: ${otapass}

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${devicename}

web_server:

captive_portal:
  

sensor:
  - platform: wifi_signal
    name: ${friendlyname} Wifi Signal Strength
    update_interval: 60s
  - platform: uptime
    name: ${friendlyname} Uptime
  - platform: cse7766
    current:
      name: "${friendlyname} Current"
      icon: mdi:current-ac
      filters:
        - multiply: 4.867
        - throttle_average: 30s
    voltage:
      name: "${friendlyname} Voltage"
      icon: mdi:flash
      filters:
        - multiply: 1.905
        - throttle_average: 30s
    power:
      name: "${friendlyname} Power"
      icon: mdi:gauge
      id: powerpoint_wattage
      filters:
        - throttle_average: 30s
    energy:
      name: "${friendlyname} Energy"
      icon: mdi:gauge
      id: powerpoint_energy
      filters:
        - throttle: 30s
  - platform: total_daily_energy
    name: "${friendlyname} Daily Energy"
    power_id: powerpoint_wattage
    filters:
      - multiply: 0.001
      - throttle: 60s
    unit_of_measurement: kWh
    icon: mdi:chart-bar

text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${friendlyname} IP
    ssid:
      name: ${friendlyname} SSID
    bssid:
      name: ${friendlyname} BSSID
    mac_address:
      name: ${friendlyname} Mac
  - platform: version
    name: ${friendlyname} ESPHome Version

# Enable time component for use by daily power sensor
time:
  - platform: homeassistant
    id: homeassistant_time

uart:
  rx_pin: P10
  baud_rate: 4800
  parity: EVEN

###Buttons
binary_sensor:
# Left button
  - platform: gpio
    device_class: power
    pin:
      number: P7
      mode: INPUT
      #inverted: true
    name: "${friendlyname} Left Button"
    on_press:
      - switch.toggle: relay_a
# Right button
  - platform: gpio
    device_class: power
    pin:
      number: P24
      mode: INPUT
      #inverted: true
    name: "${friendlyname} Right Button"
    on_press:
      - switch.toggle: relay_b

switch:
  - platform: gpio
    pin: 
      number: P6
      mode: 
        output: true
    name: "${friendlyname_left}"
    restore_mode: ${relay_restore_mode_left}
    id: relay_a
    icon: mdi:power-socket-au

  - platform: gpio
    pin: 
      number: P26
      mode: 
        output: true
    name: "${friendlyname_right}"
    restore_mode: ${relay_restore_mode_left}
    id: relay_b
    icon: mdi:power-socket-au

I have a bk7232 running on openbeken and it does the same. Probably a hardware limitation.

I seriously doubt the ESP8266 itself is holding a GPIO in a non-default state through a reboot. I don’t think you’ll find any chip that can do that.

That of course doesn’t mean a relay has to open during a reboot. Depending on how it is connected, what the default state of the GPIO is, what the pull-up/pull-down resistors are, and whether the relay is NC/NO will all affect the behavior observed during a chip reboot.

Blaming the chip itself for this behavior is misdirected.

1 Like

More expensive but tuya? You paid too much.
MCU pins have all kind of different behaviours during reboot, if the relay trigger doesn’t have pullup (or pull-down), quite likely switching will happen. Independently how pricy the device is.


I use this when choosing pins in 8266.

1 Like

That’s good example.
This thread is confusing, but the title points to problem with some bk7231t device.

All my devices latch the relay when rebooting, esp & beken. If relay control is not required I’ve shorted the AC thru the relay. I did this on 3 fridges so they can never open, accidentally or by device failure. Comes down to your use case.

I was going to suggest a small UPS to tide you over any small power interruptions as a desired workaround with bonus implications.
Is this relevant?

I think it’s impossible that any esp will hold state during reset. If any device holds relay in pre-set position i’m pretty sure that hardware is made so that it allows such a state. So, any external chip, could be MCP23008, MCP23017, SN74HC595… i can’t say for any of them if they hold state, but i’m sure to can be done.

Thanks for all of the help/ideas.
I was asking about the chip as I know the esp8266 has special options that can help them keep state eg

esp8266:
board: esp01_1m
restore_from_flash: True
early_pin_init: False

Anyway, I guess this device just can’t maintain state during reboot.

I’d suggest this is a hardware limitation depending on the pin they have used and the relay drive circuit.

You can get power monitoring plugs without the relay that don’t switch if that is all you need.