Template Switch Only Logging "Turning ON", Not "Turning OFF"

Hey folks, I’m running into a weird issue with a template switch in ESPHome.

Here’s what I’m trying to do:

  • I have a global variable allow_sleep (type: bool)
  • A template switch called sleep_allowed toggles that variable
  • Everything works fine when I turn the switch on — I see this in the logs:
[switch:012] 'sleep_allowed' Turning ON.

BUT when I turn it off, I get [switch:012] 'sleep_allowed' Turning ON.


Here’s the relevant config:

globals:
  - id: allow_sleep
    type: bool
    restore_value: yes
    initial_value: 'false'

switch:
  - platform: template
    name: "sleep_allowed"
    id: sleep_allowed
    restore_state: true
    turn_on_action:
      - lambda: |-
          id(allow_sleep) = true;
      - logger.log: "Sleep allowed: ON"

    turn_off_action:
      - lambda: |-
          id(allow_sleep) = false;
      - logger.log: "Sleep allowed: OFF"

I’ve tried:

  • Testing directly from ESPHome dashboard
  • Watching live logs
  • Rebooting and reflashing
  • Checking YAML formatting and indentation
  • Verifying there’s no other switch or automation conflicting with sleep_allowed

Still no joy. The turn_off_action just seems to never run.

Anyone seen this before or have any ideas?

Thanks in advance :pray:

Why do you need both the global AND the switch which has state that should be what the global is?

You should also see this:

If not, your board doesn’t have same yaml that you posted here.

edit:
add option
optimistic: true