How to OTA flash sleeping esphome sensor

It’s been a while since I did this. I have an ESP01 in my Jeep that I use for presence.
If the ESP connects, then the Jeep is present.
I use a binary helper that I turn on on the dashboard that is basically a semaphore to tell the ESP that I want it to not go to sleep. The test_ota script in the code below is where this is tested when the ESP boots.

So, my sequence is:

  1. Turn on OTA mode.
  2. Wait for the sleep period to end, then the ESP reboots and will not go to sleep.
  3. Perform my OTA upload.
  4. Turn off the OTA mode.
  5. Press “Restart” on the dashboard to reboot the ESP.

jeep

# This is the ESP device inside the Jeep to provide presence (status=connected).
substitutions:
  device_name: jeep
  
esphome:
  name: ${device_name}
  on_boot:
    priority: -100.0
    then:
      - delay: 1s
      - script.execute: test_ota

esp8266:
  board: esp01_1m

api:

ota:
  safe_mode: True
  
packages:
  wifi: !include common/wifi.yaml

logger:
  level: VERBOSE     # default is DEBUG


binary_sensor:
  - platform: status
    name: "Jeep Status"
  - platform: homeassistant
    id: otamode
    entity_id: input_boolean.jeep_ota_mode
    
    
#################################################
# Get the WiFi details
text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${device_name} IP Address
    ssid:
      name: ${device_name} SSID
    mac_address:
      name: ${device_name} Mac Address
      
sensor:
  - platform: wifi_signal
    name: ${device_name} WiFi Signal Sensor"
    update_interval: 60s
    
    
#################################################    
# Script to test if the otamode switch is on or off
script:
  - id: test_ota
    mode: queued
    then:
      - logger.log: "Checking OTA Mode"
      - if:
          condition:
            binary_sensor.is_on: otamode
          then:
            - logger.log: 'OTA Mode ON'
            - deep_sleep.prevent: deep_sleep_handler
          else:
            - logger.log: 'OTA Mode OFF'
      - delay: 2s
      - script.execute: test_ota


    
#################################################
#Deep Sleep
deep_sleep:
  id: deep_sleep_handler
  run_duration: 5s
  sleep_duration: 120s
  

################################################
#Make a button to reboot the ESP device
button:
  - platform: restart
    name: ${device_name} Restart
9 Likes