I’m using a dumb IR-panel for heating in my home-office. To control it, I use a Generic Thermostat and a Athom Smart Plug with ESPHome firmware. I wanted to make sure that the plug would switch of after some time in case of trouble with the network or HA itself.
The keep_alive option of the Generic Thermostat seemed the right solution for this, however I was struggling to find the right config in ESPHome. I started out using the switch.on_turn_on
trigger to start a delay after which the switch would be turned off. I hoped that the periodic turn_on commands generated by the Generic Thermostat would reset the delay.
However, since all the delays are processed asynchronous, I ended up with several outstanding delays which resulted in erratic behaviour of the switch. After some looking around I was finally able to write the following configs.
ESPHome:
script:
- id: heartbeat
mode: restart
then:
- switch.turn_on: <relay>
- logger.log: Script Heartbeat is started
- delay: 11s
- switch.turn_off: <relay>
switch:
- platform: template
name: "Thermostat Switch"
lambda: |-
if (id(<relay>).state) {
return true;
} else {
return false;
}
turn_on_action:
- script.execute: heartbeat
turn_off_action:
- switch.turn_off: <relay>
Generic Thermostat:
- platform: generic_thermostat
...
heater: switch.<ESPHome_Device>_thermostat_switch
keep_alive:
seconds: 10
...
The ESPHome template switch is used because it turns out turning on an switch of platform type gpio or output that is already on, does not result in a restart of the script. The lambda function ensures that the generic thermostat state is correctly displayed on the thermostat card on HA’s dashboard.
If anyone has another or beter approach to this problem I’d like to hear it.