I use two ESPHome-controlled relays to control my heat pump. I would like to implement automatic fallback logic that turns the heating on if Home Assistant is offline for more than 30 minutes.
I accomplished something similar on a Shelly 2PM by sending a REST command from Home Assistant to the Shelly every minute to reset a timer on the device. If the reset command is missed for more than a certain number of minutes, the device performs a specific action (in this case, retracting an awning).
How can I accomplish something similar with ESPHome?
I use a number of sensors in my heating controller that fallback to a local sensors or a fixed value if the ESP module loses connection to HA.
- platform: template # Sets Heat schedule to ON if Homeassistant not available and connected
name: "${friendly_name} CH Schedule"
id: heat_schedule
lambda: |-
if (id(connected).state) {
return id(ha_heat_schedule).state;
} else {
return "on";
}
Another approach might use the api.connected condition which checks if at least one client is connected to the ESPHome native API.
Possibly check api.connected in an api.on_client_disconnected trigger, and if it was the last connection start a timer. api.on_client_connected trigger can reset the timer since there must now be some connection.