Sonoff S31 very slow to respond to relay change

Hi All!

So I’ve just recently started trying out ESPHome, and so far it has been a fun exercise (aside from some headaches flashing the device initially - hardware isn’t my thing), but now I’m hitting an issue.

I flashed 2 of these plugs with ESPHome and then connected them to home assistant. Now, I’m seeing frequent updates to the sensors, but making changes to the relay state is difficult if even possible. When I toggle it, it might respond 1 in 5 times, or after an extended delay like half a minute

I’ve followed the guides, so I’m kind of at a loss for what I could have done wrong. Does anyone have any suggestions? Both of my plugs have the same issue, and the following config (with different name and IP):

  name: my-plug-name
  friendly_name: My Plug Name

esp8266:
  board: esp12e
  early_pin_init: false

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "[SECRET]"

ota:
  - platform: esphome
    password: "[SECRET]"

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

  manual_ip:
    static_ip: 192.168.84.68
    gateway: 192.168.86.1
    subnet: 255.255.252.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "My-Plug-Name"
    password: "[SECRET]"

captive_portal:
    
uart:
  rx_pin: RX
  baud_rate: 4800
  parity: EVEN # required since 2024-08

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "button"
    on_press:
      # Ensure the switch always operates the relay
      - switch.toggle: relay 
  - platform: status
    name: "status"

sensor:
  - platform: wifi_signal
    name: "wifi_signal"
    update_interval: 60s
  - platform: cse7766
    current:
      name: "current"
      accuracy_decimals: 2
    voltage:
      name: "voltage"
      accuracy_decimals: 2
    power:
      name: "power"
      accuracy_decimals: 2
      id: power
  - platform: integration
    name: "energy"
    sensor: power
    time_unit: h
    state_class: measurement
    unit_of_measurement: kWh
    filters:
      - multiply: 0.001
  - platform: total_daily_energy
    name: "Total Daily Energy"
    power_id: power

time:
  - platform: sntp
    id: the_time

switch:
  - platform: gpio
    name: "relay"
    pin: GPIO12
    id: relay
    restore_mode: ALWAYS_OFF

status_led:
  pin:
    number: GPIO13
    inverted: True

It’s probably worth noting the issue isn’t Wi-Fi signal - the sensors on both update regularly and show Wi-Fi signal on the order of -30 dBm

Also, I wanted to note I’m intentionally not averaging the values over a time period. Getting updates to one of the sensors frequently is essential for my project - even the once every two seconds updates I’m getting by default isn’t perfect and I’d love to drop that below once a second

Does anyone have any thoughts how to fix it? I’m really out of ideas at this point

How are you changing the relay state? If it’s from the button, you will need to debounce that.

I’m trying to change it via home assistant command. So far I’ve used the HA UX, but I assume it would be the same if I sent a switch.turn_on command directly

I’ve also noticed the physical button on the device doesn’t seem to be working, which might be related, but the button isn’t meaningful for my scenario so I was just planning to leave it broken

For current and power I only get the values every 5 seconds to not fill up the history like crazy, so I set the variable in the code below to 5s - might be worth trying to force it to 1s

    - platform: cse7766
      current:
        name: "Current"
        id: current
        state_class: measurement
        device_class: current
        unit_of_measurement: A
        accuracy_decimals: 2
        filters:
          - throttle: ${cse7766_throttle_interval}

For the relay, I use a setup very similar to yours. I have about a dozen of the S31s and they all respond almost instantly, so: sorry, can’t give you any hints for that.

Maybe another interesting bit, especially if you’re planning to flash a few more:
I use a basic file for each device that holds the individual info but it als uses a common template that’s pulled in by using the yaml file from a subfolder through
<<: !include templates/template-s31-outlet.yaml

logs?
both from the device and from HA

I would guess that HA is not able to always talk to your device.

As a test, add the webserver component and try switching the relay that way.

You probably also should look here:

Your config doesn’t match, in ways that will likely cause you problems.

So I re-configured the entire plug from the start, and copied directly from the devices.esphome.io section, and got it working. Now I am going to just remove line-by-line until it either works for my use case or breaks

From what I can tell, the issue was that I was missing these lines

logger:
  baud_rate: 0

But I can’t say for sure - I am just happy its working now

Thanks for the help everyone!

Looks like a reasonable guess!

The original file I used to create my S31 code has this comment in it :face_with_open_eyes_and_hand_over_mouth:

  logger:
    baud_rate: 0 # (UART logging interferes with cse7766)