Install device firmware update after waking from deep sleep

Let’s assume I have an ESP8266 device with a BME280 sensor attached and that device/sensor combination is running on batteries using deep sleep to conserve power. The sleep interval could be in the order of hours so I’m not always around when the device wakes.

Now while the device is in deep sleep, I edit and then save the device yaml file with say, some new calibration values for the BME280 sensor.

When the device wakes from deep sleep, I can stop it returning to deep sleep by using an input boolean displayed as a toggle switch…toggle switch on and the device does not reenter deep sleep…turn it off, device reenters deep sleep cycles. This works fine with the below script.

script:
  - id: test_ota
    mode: queued
    then:
      - lambda: |-
          std::string version = id(esp8266_1_firmware_version).state.c_str();
          ESP_LOGI("main", "Firmware Version: %s", version.c_str());
      - logger.log: "Checking OTA Mode"
      - if:
          condition:
            binary_sensor.is_on: otamode
          then:
            - logger.log: 'OTA Mode is On so I am going to PREVENT deep sleep from starting'
            - deep_sleep.prevent: gotosleep 
            - delay: 20s
            - homeassistant.service:
                service: input_boolean.turn_off
                data:
                  entity_id: input_boolean.ota_mode
            
          else:
            - logger.log: 'OTA Mode Off so I am going to ALLOW deep sleep to start'
      - delay: 10s
      - script.execute: test_ota

What I now want to do, but can’t seem to find any appropriate information online, is when the device wakes from deep sleep and the input boolean is turned on indicating a new update, install that new yaml file to the device . I’ve looked at the documentation for OTA as well as examples but can’t seem to grasp the process.

I don’t wont to simply keep the device awake until I can manually install the update as that affects the battery life.

Any assistance greatly appreciated.

Hi
Esp8266 cant be put to deepsleep for hours and woken again consistently. Problems i find start arise when you get near 70 mins. Online I see it quoted at 3.5hrs but ive never got it to work at that time. Esp32 can be sent to deepsleep for very long periods.
I would suggest OTA updates are a vulnerable time and you may need to manually restart or reflash to get them working again. I will also say its not necessary to upate all esphome devices each month a new update comes along.
What sort of batteries are you using and hows that working?

The deep sleep time is really irrelevant in this scenerio as I’m just trying to get the OTA part working.