ESPhome shelly toggles relay on restart

Hi, I’m using a shelly behind a wall switch to catch the button pushes and send to home-assistant. The connected light is a zigbee light that should always have power.

I notice that on reboot of the shelly, the relay toggles and this causes the light to go on. This happened now at night when the wifi went offline for a bit. This is a problem I’m looking into but into, but I’m also trying to not toggle the relay on reboot.

I already enabled these settings:

  1. reboot_timeout: 60min, will make it less sensitive to shorter wifi outages
  2. esp8266: early_pin_init: false according to the documentation: Specifies whether pins should be initialised as early as possible to known values. Recommended value is false where switches are involved, as these will toggle when updating the firmware or when restarting the device. Defaults to true .
  3. switch: restore_mode: ALWAYS_ON → Should keep it on during reboot?

Currently testing by updating the configuration and triggering a restart via home assistant and obseverving the light status.

This is my configuration:

substitutions:
  devicename: "bedroom_wall_switch"
  upper_devicename: "Bedroom wall switch"
  device_group: "bedroom"
  manual_ip: "192.168.0.242"
  multiclick_GPIO: GPIO5
  multiclick_relay_for_offline: relay

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_passwd

  manual_ip:
    # Set this to the IP of the ESP
    static_ip: $manual_ip
    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.0.1
    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: $upper_devicename
    password: !secret access_mode_passwd

  # No power saving
  power_save_mode: none

  # Only reboot after 1h
  reboot_timeout: 60min

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

# OTA updates:
ota:
  - platform: esphome

# ESP:
esphome:
  name: $devicename
  # Enable the relay at boot:
  #on_boot:
  #  then:
  #    - switch.turn_on: relay

esp8266:
  board: esp01_1m
  early_pin_init: false

switch:
  # Relais:
  - platform: gpio
    name: Relay
    pin: GPIO4
    id: ${multiclick_relay_for_offline}
    restore_mode: ALWAYS_ON
  # Trigger restart
  - platform: restart
    name: Restart

# Event configuration
event:
  - platform: template
    id: button_press_events
    name: button press events
    event_types:
      - "single_click"
      - "double_click"
      - "hold"

binary_sensor:
  # Multi-click sensor:
  - platform: gpio
    pin:
      number: ${multiclick_GPIO}
    name: switch_sensor
    on_multi_click:
      - timing:
          - ON for at most 1s
          - OFF for at most 0.5s
          - ON for at most 1s
          - OFF for at least 0.1s
        then:
          - logger.log: "Double Clicked"
          - event.trigger:
              id: button_press_events
              event_type: double_click
          - delay: 3s
      - timing:
          - ON for at least 1s
        then:
          - while:
              condition:
                binary_sensor.is_on: switchid
              then:
                - logger.log: "Hold"
                - event.trigger:
                    id: button_press_events
                    event_type: hold
                - delay: 3s
      - timing:
          - ON for at most 1s
          - OFF for at least 0.5s
        then:
          - if:
              condition:
                and:
                  - wifi.connected:
                  - api.connected:
              then:
                - logger.log: "Single Short Clicked"
                - event.trigger:
                    id: button_press_events
                    event_type: single_click
                - delay: 3s
              else:
                - logger.log: "Toggling relay because the device is offline"
                - switch.toggle: ${multiclick_relay_for_offline}

    internal: false
    id: switchid

  # Status sensor:
  - platform: status
    name: Status

So why it’s passing through relay?

In case of a wifi failure, I want to be able to switch the power of the lights with the manual switch.

What I think is happening is that when the Shelly reboots it starts up having the switch turned off and then when it goes through its restore phase it turns the switch on. I’m not sure you will be able to change the behaviour such that the switch doesn’t go off during reboot.

To be honest, I don’t really see the point of you putting a Shelly there as flipping the switch will turn the lights off by changing their power state and sending them offline. My advice is just remove the Shelly and leave the switch in the On position - being able to turn it off / on in the event your zigbee network goes down.

I’m still using the physical switch in a toggle mode in case I want to manually control the zigbee lights. In this case the relay state is not changed but I trigger a sensor in home assistant.

I still tried a bit and figured out that it was not the shelly in the end. The shelly is working fine as is. There is a state change from “unavailable” to “single_click” after the reboot when the shelly comes online again.

So I added now a detection that if the state changes and the old state is “unavailable”, to ignore the new state. This solved it for me!

I have the same problem with the classic Shelly 1 in 12V mode. After every restart the relay toggles, which (in my case) enables the alarm system - wife not so happy when she’s at home.

Any idea what causes the relay to toggle?
Is it maybe my cabling? In my case I have to toggle a line (S1) with GND, so I bridged GND to 1 on the shelly and just connected S1 to 0 on the shelly.

substitutions:
  friendly_name: "Alarm"

esphome:
  name: "alarm"
esp8266:
  board: esp01_1m

button:
  - platform: restart
    name: "Restart"

binary_sensor:
  - platform: gpio
    pin: GPIO5
    name: "${friendly_name} State"
    id: alarm_status
    device_class: safety

switch:
  - platform: gpio
    pin: GPIO4
    id: relay
    internal: true
    restore_mode: RESTORE_DEFAULT_OFF

  - platform: template
    name: "${friendly_name}"
    icon: mdi:alarm-light
    lambda: |-
      return !id(alarm_status).state;
    turn_on_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
    turn_off_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay

wifi:
ota:
  platform: esphome