ESPHome: Over The Air Update

At the beginning of 2024 I started using ESPHome to replace Raspberry PI Pico Ws and wrote about it. Then I didn't activate OTA update because I didn't know about it and all the Picos I deployed then are now a bit outdated.

Before changing anything or compiling a firmware we need to update the ESPHome Docker image. This felt a bit strange, because pulling a new image wasn't enough, so I deleted the some generated folders, too.

# update esphome, to get the current firmware code
docker pull esphome/esphome
# delete some cached folders to redownload platformio and guarantee a new compile
sudo rm -rf .esphome/build/pico2 .esphome/platformio

Next is to add ota to the config (best with a password defined in secrets.yaml).

ota:
  password: !secret ota_password
The other change I needed was platform: bme280 is named platform: bme280_i2c because this changed in the new ESPHome firmware.
After changing the configuration we compile a new firmware which creates a firmware.uf2 because it is a Raspberry PI Pico.
For me this worked like this:
docker compose run --rm esphome compile pico2.yaml
# mount the Pico before by pressing the button when plugging into the notebook
cp config/.esphome/build/pico2/.pioenvs/pico2/firmware.uf2 /run/media/mfa/RPI-RP2/

When bootet after this we can now update the Pico via OTA and don't need to plug it in again (hopefully ever). The update via OTA for my Pico using docker compose:

docker compose run --rm esphome run pico2.yaml

Because the Pico is not connected to my notebook (anymore) there is only the OTA option and it will update the Pico via OTA automatically. Running the command is very verbose and you see success and the following logs when the Pico is booting again:

<...>
INFO Upload took 4.52 seconds, waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from pico2.local using esphome API
<...>
[22:49:27][I][app:100]: ESPHome version 2024.5.3 compiled on <...>

In the log output I see the device has booted and is measuring using a BME280 as expected.

Because we can update the Pico now very easy we can add a new sensor that adds the current version as text sensor. The firmware version is already shown in the ESPHome Integration, but with an Entity we can add a reminder for outdated versions to our Dashboards. Adding a firmware version text_sensor is already an example in the documentation, so we add this to the config:

text_sensor:
  - platform: version
    name: "ESPHome Version"
    hide_timestamp: true

Compile and run as described above and we will get a new sensor in Homeassistant:

img1

Activating OTA is absolutely worth it. A Pico (or ESP32) can stay in place where ever it is deployed and still gets a new firmware or new settings.