How can I stop a template switch from activating on esphome startup?

I had a template cover sensor:

   - platform: template
     name: "Garage Door"
     lambda: |-
       if (id(attached_garage_door).state) {
         return COVER_OPEN;
       } else {
         return COVER_CLOSED;
       - switch.turn_on: attached_garage_switch
       - switch.turn_on: attached_garage_switch
       - switch.turn_on: attached_garage_switch
     optimistic: true

This wasn’t a great experience since I had 3 buttons that all got mapped to the single door switch and it was confusing. So I thought I’d try to simplify it to just have a switch. And it’s largely working and better than before with one glaring problem. When the esphome device starts it activates the garage door (opens if closed, closes if open) which did not happen with the cover:

  - platform: gpio
      number: D1
    id: attached_garage_switch
    name: "Door"
    icon: "mdi:door"
    - delay: 500ms
    - switch.turn_off: attached_garage_switch
  - platform: template
    name: "Garage Door Template"
    id: attached_garage_door_template
    lambda: |-
      if (id(attached_garage_door).state) {
        return true;
      } else {
        return false;
      - switch.turn_on: attached_garage_switch
      - switch.turn_on: attached_garage_switch
    optimistic: true

It seems that on startup for a template switch, the switch is being triggered by initializing it’s state?

Also I was trying to update the icon based on the garage door state, per another old post but that doesn’t work as described so looking for ideas on how to do that too.

You could try setting the restore mode to off.

restore_mode: ALWAYS_OFF

However you appear to be using an ESP8266. So see the restore from flash option here:

For ESP8266 it also makes sense to look at the early_pin_init option and setting it to false (default is true) if one is experiencing switch toggling upon reboot.

1 Like

No joy yet with any of the suggestions.

restore_from_flash was already set.
Changing restore_mode and early_pin_init didn’t help.

Can you show your log at startup.
Also, please describe the actual hardware setup with your door.

I ran into a similar problem yesterday where, after boot esphome performed turn_off_action if lambda returned true and template switch was off. In my case, setting restore_mode: disabled solves the issue.

Yes! Adding that to the template switch markup fixed it.

I’m about to go on vacation and was thinking I’d have to switch back to the cover template I had before in case there was a power outage while I was away. So perfect timing. Thanks.

In hindsight it makes sensor to me now. If the template is ever explicitly set it executes the turn on or turn off action depending on the state it was set to regardless of what previous state it had. And restore does an explicit set. So blocking the restore entirely avoids that. Where the always disabled setting I tried before just meant it always got explicitly set to off on startup.