ESP-32 not saving pins on restart

This is a strange puppy. I have a few ESP-VROOM32, and I’m using a 4 module relay card. The only unusual thing is that the coils in the relays are driven with 12V, but I can’t imagine that can be the problem because it happens in the same way without drive voltage, when only the LEDs on the relay card are triggered. It’s totally random, it seems. And it’s not just one of my ESP’s, I’ve tried with three different (of the same brand), and it is just the same way with those. I am using this one from Ali:

Sometimes it saves the pins status, other times it doesn’t. When it doesn’t it will be stuck on the last configuration it saved, no matter what that was. Is there anything I can check here? Here’s parts of my YAML (the rest is just repeats for the other three relays).

The name of the thing is Mad Max Stern (hekk in Norwegian) because it’s going to control some stuff in the stern of my boat. I will use an identical one in the bow, when I get this working. If the ESP boots for some reason or another, I need to have it come up the same state as it was before.

Is there maybe a way to force it to save states? I haven’t found it in the documentations, but that doesn’t man it’s not there. I’m using the command line ESP Home to flash it, but I don’t think that should reall matter, right?

esphome:
  name: madmaxhekk
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "***"
  password: "***"
  manual_ip:
    static_ip: 192.168.3.100
    gateway: 192.168.3.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "***"
    password: "***"

captive_portal:

# Enable logging
#logger:

ota:

mqtt:
  broker: 192.168.3.1
  topic_prefix: madmax/esp/madmaxhekk
  reboot_timeout: 5min
  on_message:
   - topic: madmax/esp/madmaxhekk/on
     payload: "1"
     then:
       - if:
           condition:
             switch.is_on: rele1
           then:
             - mqtt.publish:
                 topic: madmax/esp/madmaxhekk/response
                 payload: "Rele 1 is already on"
           else:
             - mqtt.publish:
                 topic: madmax/esp/madmaxhekk/response
                 payload: "Rele 1 is off, turning on now"
             - switch.turn_on: rele1

   - topic: madmax/esp/madmaxhekk/off
     payload: "1"
     then:
       - if:
           condition:
             switch.is_on: rele1
           then:
             - mqtt.publish:
                 topic: madmax/esp/madmaxhekk/response
                 payload: "Rele 1 is on, turning off"
             - switch.turn_off: rele1
           else:
             - mqtt.publish:
                 topic: madmax/esp/madmaxhekk/response
                 payload: "Rele 1 is already off"

   - topic: madmax/esp/madmaxhekk/omstart
     then:
       - mqtt.publish:
           topic: madmax/esp/madmaxhekk/response
           payload: "Tar omstart av Mad Max hekk"
       - switch.turn_on: omstart

switch:
  - platform: gpio
    name: "rele1"
    id: rele1
    pin:
      number: 12
      inverted: True
    restore_mode: RESTORE_DEFAULT_OFF

Doesn’t seem like anybody knows much about this. :frowning: In the meantime I have made a Node-RED flow that will use a custom birth message as a trigger and set the pins back to the state they had before. With that and redundancy (another ESP with another relay card connected to the same stuff I’m controlling with the first) I can’t imagine I will ever have a problem with this, as long as all the power from both of the batteries in the boat doesn’t go out, and in that case there’s not much to control…