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