ESPHome automation does not run without WiFi connection on ESP32

Hi,

I understand that automations in ESPHome should continue to work even if the WiFi network is down or the MQTT server is not reachable - https://esphome.io/guides/automations.html#do-automations-work-without-a-network-connection

My ESP32 is in a poor WiFi location. When the network doesn’t connect, the automations do not run (in this case, the Close Door or Open Door scripts).

Is there anything missing from my code, that would enable the automations to run when there is no WiFi or MQTT network?

esphome:
  name: chicken-coop
  platform: ESP32
  board: esp32dev
  on_boot:
      priority: -100
      then:
          lambda: |-
            if (id(deepSleepCycleBool) == false)
            {
                id(deepSleepCycleBool) = true;
                id(mqtt_client).publish("chicken-coop/ id(deepSleepCycleBool)", "true");
            }
            else
            {
                id(deepSleepCycleBool) = false;
                id(mqtt_client).publish("chicken-coop/ id(deepSleepCycleBool)", "false");
            }

# Enable logging
logger:

# Enable Home Assistant API
#api:

ota:
  password: "<removed>"

wifi:
  networks:
  - ssid: "<removed>"
    password: "<removed>"
  - ssid: "<removed>"
    password: "<removed>"
  power_save_mode: none
  reboot_timeout: 0s
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Chicken-Coop Fallback Hotspot"
    password: "<removed>"

deep_sleep:
  id: deep_sleep_1
  run_duration: 90s
  sleep_duration: 29min

mqtt:
  broker: 192.168.1.110
  id: mqtt_client
  reboot_timeout: 0s
  on_message:
    - topic: chickens/deepsleepactive
      payload: 'DISABLED'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: chickens/deepsleepactive
      payload: 'FORCE'
      then:
        - deep_sleep.enter: deep_sleep_1
    - topic: chickens/doorMode
      payload: 'MANUAL'
      then:
        - lambda: |-
            id(isInManualMode) = true;
    - topic: chickens/doorMode
      payload: 'AUTO'
      then:
        - lambda: |-
            id(isInManualMode) = false;
    - topic: chickens/manualDoorRequest
      payload: 'CLOSE'
      then:
        - script.execute: closeDoorScript
    - topic: chickens/manualDoorRequest
      payload: 'OPEN'
      then:
        - script.execute: openDoorScript

globals:
  - id: lightSensorReadingsArry
    type: int[2]
    restore_value: yes
  - id: valueIndex
    type: int
    restore_value: yes
    initial_value: '0'
  - id: sensorReadingMovingAverage
    type: int
    restore_value: yes
    initial_value: '0'
  - id: doorIsClosed
    type: bool
    restore_value: yes
    initial_value: 'true'
  - id: isInManualMode
    type: bool
    restore_value: yes
    initial_value: 'false'
  - id: deepSleepCycleBool
    type: bool
    restore_value: yes
    initial_value: 'true'
  - id: deepSleepCycleBoolPreviousValue
    type: bool
    restore_value: yes
    initial_value: 'false'

script:
  - id: closeDoorScript
    then:
      - lambda: |-
          id(doorIsClosed) = true;
      - mqtt.publish:
          topic: chicken-coop/doorAction
          payload: "Door Closing"
      - switch.turn_on: closedoorswitch1
      - switch.turn_on: closedoorswitch2
      - delay: 30s
      - switch.turn_off: closedoorswitch1
      - switch.turn_off: closedoorswitch2
      - mqtt.publish:
          topic: chicken-coop/doorAction
          payload: "Door Closed"
  - id: openDoorScript
    then:
      - lambda: |-
          id(doorIsClosed) = false;
      - mqtt.publish:
          topic: chicken-coop/doorAction
          payload: "Door Opening"
      - switch.turn_on: opendoorswitch1
      - switch.turn_on: opendoorswitch2
      - delay: 30s
      - switch.turn_off: opendoorswitch1
      - switch.turn_off: opendoorswitch2
      - mqtt.publish:
          topic: chicken-coop/doorAction
          payload: "Door Open"

switch:
  - platform: gpio
    name: "closedoorswitch1"
    id: closedoorswitch1
    pin: GPIO12
  - platform: gpio
    name: "closedoorswitch2"
    id: closedoorswitch2
    pin: GPIO13
  - platform: gpio
    name: "opendoorswitch1"
    id: opendoorswitch1
    pin: 
      number: GPIO18
      #inverted: true
  - platform: gpio
    name: "opendoorswitch2"
    id: opendoorswitch2
    pin: 
      number: GPIO19
      #inverted: true

sensor:
  - platform: adc
    pin: GPIO32
    name: "TEMT6000 Illuminance"
    device_class: illuminance
    unit_of_measurement: lx
    update_interval: 30s
    on_value:
        then:
            - if:
                condition:
                    lambda: |-
                      if (id(sensorReadingMovingAverage) < 50)
                      {
                          if (id(doorIsClosed) == false)
                          {
                              if(id(isInManualMode) == false)
                              {
                                 return true;
                              }
                              else
                              {
                                return false;
                              }
                          }
                          else
                          {
                              return false;
                          }
                      }
                      else
                      {
                          return false;
                      }
                then: 
                  - script.execute: closeDoorScript
            - if:
                condition:
                    lambda: |-
                      //int newReading = static_cast<int>((x / 10000) * 2000000);
    
                      //if (newReading > 100)
                      if (id(sensorReadingMovingAverage) > 50)
                      {
                            if (id(doorIsClosed) == true)
                            {
                                if(id(isInManualMode) == false)
                                {
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                return false;
                            }
                      }
                      else
                      {
                        return false;
                      }
                then:
                    - script.execute: openDoorScript
            - lambda: |-
                int windowSize = 2;
                bool allowAverageCalcToRun = true;
                
                int sensorReadingInt = static_cast<int>((x / 10000) * 2000000);
                std::string sensorReadingStr = to_string(sensorReadingInt);
                id(mqtt_client).publish("chicken-coop/newSensorReading", sensorReadingStr);
                
                if (id(deepSleepCycleBool) == false)
                {
                    if (id(deepSleepCycleBoolPreviousValue) == true)
                    {
                        allowAverageCalcToRun = true;
                        id(deepSleepCycleBoolPreviousValue) = false;
                    }
                    else
                    {
                        allowAverageCalcToRun = false;
                    }
                }
                else
                {
                    if (id(deepSleepCycleBoolPreviousValue) == false)
                    {
                        allowAverageCalcToRun = true;
                        id(deepSleepCycleBoolPreviousValue) = true;
                    }
                    else
                    {
                        allowAverageCalcToRun = false;
                    }
                }
                
                if (allowAverageCalcToRun)
                {
                    id(lightSensorReadingsArry)[id(valueIndex)]  = sensorReadingInt;
                    
                    std::string currentIndexStr = to_string(id(valueIndex));
                    id(mqtt_client).publish("chicken-coop/valueIndex", currentIndexStr);
                    
                    if (id(valueIndex) < windowSize) 
                    {               
                      id(valueIndex) = id(valueIndex) + 1;
                    }
                      else
                    {
                      id(valueIndex) = 0;
                    }
                    
                    id(sensorReadingMovingAverage) = 0;
                    
                    for (int i = 0; i < windowSize; i++)
                    {
                      id(sensorReadingMovingAverage) = id(sensorReadingMovingAverage) + id(lightSensorReadingsArry)[i];
                    }
                    
                    id(sensorReadingMovingAverage) = id(sensorReadingMovingAverage) / windowSize;
                    
                    int localReadingAverage = id(sensorReadingMovingAverage);
                    
                    std::string sensorReadingMovingAverageStr = to_string(localReadingAverage);
                    id(mqtt_client).publish("chicken-coop/id(sensorReadingMovingAverage)", sensorReadingMovingAverageStr);
                }

The same problem and question…
+1

I have found the solution:
priority: 600
As I understant it doesn’t wait wifi network in this case. At least it works.