When my esphome devices loose wifi, they start rebooting, which 'flicks' the relay

I have this is my config:

  on_boot:
    then:
      - switch.turn_on: relay

On quite a few items, but this causes the relay to ‘flick’ off/on when it reboots. As it keeps rebooting if I work on the network, and the wifi is gone, it will make this flicker every few minuttes.

I would very much like to keep the possibility to have the items ‘always on’ after a boot, but at the same time I don’t want them to do the flick, as at some point some switchmode psu are going to die from this.

So is there a way to modify the ‘on_boot’ so that it only turns the relay on, if it’s off?

Untested, but I think this is how to do it:

  on_boot:
    then:
      - if: 
          condition:
            - switch.is_off: relay
          then:
            - switch.turn_on: relay

Doc: https://esphome.io/guides/automations.html?highlight=automation#if-action

1 Like

as an alternative:

restore_mode: RESTORE_DEFAULT_ON

will restore it’s previous state and if previous state isn’t known, it will turn the switch on. Wifi dropouts shouldnt wipe the state whereas powerloss will do.

check

and for setting a higher reboot timeout:

2 Likes

Of course! I use that. Just forgot all about it.

Much simpler.

1 Like

That’s great, thanks, I’ll use that!

And it seems to work, that’s great. :slight_smile:

1 Like

How did you test this exactly? I’m trying to use a Blitzwolf BW-6 as a “measure only” plug. If I kick the plug from the wifi network it still toggles off the relay and turns it back on a few seconds later.

I disabled the reboot, and set the
restore_mode: RESTORE_DEFAULT_ON
for the pin tha toggles the switch.

What sort of device do you have? Maybe it simply works in a different way than my BW-6

I have the house full of BW-6 :slight_smile:

My configs looks like this

substitutions:
  device_name: adrianentertainment
  # Higher value gives lower watt readout
  current_res: '0.00290'
  # Lower value gives lower voltage readout
  voltage_div: '940'

esphome:
  name: '${device_name}'
  platform: ESP8266
  board: esp8285

wifi:
  ssid: "NOT"
  password: "WIFIPW"
  reboot_timeout: 60min
  manual_ip:
    static_ip: 10.11.13.40
    gateway: 10.11.13.1
    subnet: 255.255.255.0
#  use_address: 10.11.12.201
  fast_connect: true
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} Hotspot"
    password: "HOTSPOTPW"
    
logger:
  baud_rate: 0

api:

ota:
  password: "OTAPW"

time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: version
    name: "${device_name} ESPHome Version"
  - platform: wifi_info
    ip_address:
      name: "${device_name} ip"
    ssid:
      name: "${device_name} ssid"

binary_sensor:
  - platform: gpio
    id: button
    internal: true
    pin:
      number: GPIO13
    on_press:
      - switch.toggle: relay

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

switch:
  - platform: gpio
    pin: GPIO15
    restore_mode: RESTORE_DEFAULT_ON
    id: relay
    name: '${device_name} relay'
    on_turn_on:
      - output.turn_on: led
    on_turn_off:
      - output.turn_off: led

status_led:
  pin:
    number: GPIO02
    inverted: true

sensor:
  - platform: wifi_signal
    name: '${device_name} wifi signal'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: '${device_name} uptime'
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: true
    cf_pin: GPIO05
    cf1_pin: GPIO14
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}
    current:
      name: '${device_name} current'
      unit_of_measurement: 'A'
      accuracy_decimals: 3
      icon: mdi:flash-outline
    voltage:
      name: '${device_name} voltage'
      unit_of_measurement: 'V'
      icon: mdi:flash-outline
    power:
      name: '${device_name} power'
      unit_of_measurement: 'kW'
      id: power
      accuracy_decimals: 5
      filters:
        - multiply: 0.001
      icon: mdi:flash-outline
    change_mode_every: 4
    update_interval: 10s
  - platform: total_daily_energy
    name: '${device_name} daily energy'
    power_id: power
    unit_of_measurement: kWh
    accuracy_decimals: 5

Though, on this one I haven’t reconfigured the reboot.

1 Like

Ah, well if it works for you it should work here too :slight_smile:

I do see some differences here and there, I’ll have to tweak my config then.

In my tests this afternoon it kept on toggling the relay, also the restore_mode didn’t seem to deal with the LED

hmm mine never turns off, even survives firmware updates, even tho I can hear the click, I guess it comes from ALWAYS_ON and inverted pin, maybe it helps you!

Thanks, that did help. The relay does indeed appear to stay on in all cases (flash, reboot).

I do hear the relay clicking when the BW-6 reboots (e.g. after flashing new firmware) but the device itself (as a test; another BW-6 with the LED on) seems to stay on.

The only downside is that the LED doesn’t go on upon boot, but that can probably be solved some way. I do like the “feedback” it provides.

Also didn’t know that the api also has a reboot_timeout parameter. That explains why the relay turned off before when my HASS was down for a bit :sweat_smile:

Hmmm check my integration’s Blue LED light and the binary sensor. The blue led should have the same state as the relay, except when you press the device button manually. I have commented out the ability to switch the relay because I was pressing it by accident a couple of times. it turns back on after a reboot.

The red led is exposed as a powerindicator it turns on when a certain value is reached.