ESPHome OTA Updates for Deep Sleep Devices
ฝัง
- เผยแพร่เมื่อ 22 พ.ค. 2024
- Two methods for OTA updates of sleeping ESPHome devices. These devices can be quite hard to catch awake for firmware/YAML updates... so, here are a couple of ways to do it. The first is automated using some built in Home Assistant tools, but I only know how to do that for ESPHome updates. For configuration updates, there's a manual method using a Home Assistant Helper to tell the ESPHome device to stay awake next time it wakes up.
The second method is the one I prefer. I got it from stevemann (Stephen Mann) on the HA forums. See post here for YAML: community.home-assistant.io/t...
===Timestamps===
00:00 Introduction
00:40 Method 1: Automated version updates
01:14 Automation setup
02:48 Verify Automation (Traces)
03:45 Method 2: Manual Keep Awake
04:39 Set up HA Helper
06:30 YAML configuration
09:50 Log verification
10:17 Use toggle entity in Lovelace
11:03 Conclusion - วิทยาศาสตร์และเทคโนโลยี
You can respond to the on_state trigger of the binary_sensor, and the api's on_client_connected trigger. That way you don't have to run the script every second.
You do have to add a delay (e.g.: 1s) in the on_client_connected before calling the script, to give esphome a chance to initialize logging. And also, not every client is home assistant, but that just means you check a few times if you have other clients connected.
So you mean use on_client_connected trigger under the API to run the script after a 1s delay, and then change the script mode to "single" instead of queued? like this:
on_client_connected:
then:
- delay: 1s
- script.execute: test_ota
and change the script to
script:
- id: test_ota
mode: single
then:
- logger.log: "Checking OTA Mode"
- if:
condition:
binary_sensor.is_on: otamode
then:
- logger.log: 'OTA Mode ON'
- deep_sleep.prevent: gotosleep
else:
- logger.log: 'OTA Mode OFF'
- deep_sleep.allow: gotosleep
Great Job. Thank you
Awesome, tip for deepsleep, please keep working on esphome content thanks
The second method is really awesome! Thank you 🙏
For the first method: How do you ensure that the device will be online long enough to receive the update? The problem is that once you tell ESPHome to update it would first need to compile the firmware binary before it will actually update the device, wouldn’t it?
Yeah, that could be an issue. To be honest, I put that method in there as mostly just as an example... I don't use it myself. That's why it's so basic. I anticipate there are probably several little quirks that people might have to work around with it. It's also hard to test, because you have to wait for an ESPHome version update to check to see if things work. Plus, honestly, even if it works, there are probably times that people don't want to update to a new firmware before they actually look through the release notes to see if it's necessary.
The second method has far fewer things that can go wrong, if only because you're in control of way more. It's definitely the one I prefer.
Yeah, couldn’t agree more 😊👍🏻
Thanks! Also, how can you change the color of the graphs in the card at 4:37?
If you look on the left hand side, you'll see the code for the temperature graph. You can see where it specifies "line_color". Just change that hex code or use any CSS3 color keyword: www.w3.org/TR/css-color-3/#svg-color
My esphome devices are all powered via usb adapter yet it is very very difficult to update them. It is very frustrating that these devices are not easy to update.
If they are always on, you can just update them from the ESPHome addon via wifi.
@@TechDregs Yes, I think that's the way to do it. I updated my code for it and now, they are staying online most of the time.
Can you make a better soil moisture Sensor
Better in what way?
Thanks! I have tried to use your code however, the program never go into deep_sleep.prevent. The logger always shown " OTA Mode OFF" statement. I suspected either the condition statement is never checking or the program never receive the "ON" signal from the HA helper. Please help!!!!
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
substitutions:
device_name: esp01-temp
esphome:
name: ${device_name}
friendly_name: ESP01-temp
#### This is for flashing the esp script from avoiding deep sleep#####
on_boot:
priority: -100.0
then:
- delay: 1s
- script.execute: test_ota
########################################################################
esp8266:
board: esp01_1m
# Enable logging
logger:
level: VERBOSE
# Enable Home Assistant API
api:
encryption:
key: ""
ota:
#password: ""
safe_mode: True
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp01-Temp Fallback Hotspot"
password: ""
captive_portal:
sensor:
- platform: dht
pin: GPIO2
model: DHT11
temperature:
name: "Living Room Temperature"
filters:
- offset: +2.0
humidity:
name: "Living Room Humidity"
update_interval: 5s
- platform: wifi_signal
name: ${device_name} WiFi Signal Sensor"
update_interval: 60s
#################################### This is for flashing the esp script from avoiding deep sleep#####
deep_sleep:
id: deep_sleep_handler
run_duration: 15s
sleep_duration: 60s
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
#################################################
#################################################
# 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'
#- deep_sleep.allow: deep_sleep_handler
- delay: 2s
- script.execute: test_ota
#################################################
################################################
#Make a button to reboot the ESP device
button:
- platform: restart
name: ${device_name} Restart
##################################################################################################
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
[D][script:100]: Script 'test_ota' queueing new instance (mode: queued)
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
[D][script:100]: Script 'test_ota' queueing new instance (mode: queued)
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
[W][dht:169]: Requesting data from DHT failed!
[W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[V][sensor:043]: 'Living Room Temperature': Received new state nan
[D][sensor:093]: 'Living Room Temperature': Sending state nan °C with 1 decimals of accuracy
[V][sensor:043]: 'Living Room Humidity': Received new state nan
[D][sensor:093]: 'Living Room Humidity': Sending state nan % with 0 decimals of accuracy
[W][component:214]: Component dht.sensor took a long time for an operation (0.05 s).
[W][component:215]: Components should block for at most 20-30ms.
[D][script:100]: Script 'test_ota' queueing new instance (mode: queued)
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
[D][script:100]: Script 'test_ota' queueing new instance (mode: queued)
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
fpm close 3
pm open,type:0 0
[W][dht:169]: Requesting data from DHT failed!
[W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[V][sensor:043]: 'Living Room Temperature': Received new state nan
[D][sensor:093]: 'Living Room Temperature': Sending state nan °C with 1 decimals of accuracy
[V][sensor:043]: 'Living Room Humidity': Received new state nan
[D][sensor:093]: 'Living Room Humidity': Sending state nan % with 0 decimals of accuracy
[W][component:214]: Component dht.sensor took a long time for an operation (0.05 s).
[W][component:215]: Components should block for at most 20-30ms.
[D][script:100]: Script 'test_ota' queueing new instance (mode: queued)
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
[D][script:100]: Script 'test_ota' queueing new instance (mode: queued)
[D][main:445]: Checking OTA Mode
[D][main:456]: OTA Mode OFF
[I][deep_sleep:117]: Beginning Deep Sleep
[I][deep_sleep:119]: Sleeping for 60000000us
Do you have the binary sensor set up in ESPHome to read from the HA helper? And do the entity names for those match? And is the HA helper toggled to "on"?
Yes, I wrote this
binary_sensor:
- platform: status
name: "Jeep Status"
- platform: homeassistant
id: otamode
entity_id: input_boolean.jeep_ota_mode
the entiry name is entity_id: input_boolean.jeep_ota_mode and turn it on.
@@TechDregs
I'm not sure then. I see you're using an 8266. I don't think that would cause issues, but I have never worked with those. It's clearly not seeing the OTA toggle, so I would focus there. Look at the HA helper again, make sure it's enabled and visible. Check the name again to make sure it matches the binary sensor in the YAML. Check in "Developer Tools - > States" that the entity state is being flipped correctly when you toggle it. Also, make sure you've added your ESPHome device in the ESPHome integration (via settings in Home Assistant).