I added a new template switch in the configuration.yaml.
The turn_on action works but turn_off doesn’t get executed. I can see it in the history and never ever started. I even put something simple in the turn_off section but nothing.
Can somebody please help
#Pi-Hole für 45 Sekunden deaktivieren
pihole_aus_fuer_45_sekunden_deaktiveren:
friendly_name: Pi-Hole für 45 Sekunden deaktivieren
icon_template: mdi:shield-lock-open
value_template: >-
{% if is_state('script.pi_hole_45_sekunden_deaktivieren', 'on') %}
on
{% else %}
off
{% endif %}
turn_on:
service: script.pi_hole_45_sekunden_deaktivieren
turn_off:
service: script.enable_pihole
template switches don’t automatically run the turn_off script when they naturally turn off. They only run the turn_off actions when you run a switch.turn_off service or when you turn it off via the UI when it’s on.
You’d want an automation if you want it to run the turn off script when script.pi_hole_45_sekunden_deaktivieren turns off, assuming that script can turn off. Which should have an on/off state if you have a delay (which you do).
After a closer look at your turn_off script, my suggestion may not have the desired effect because the script initially turns off switch.pi_hole but 45 seconds later it turns it back on.
It’s unclear to me why it does that but it means the state of switch.pi_hole may not be a reliable basis for the Template Switch’s state.
I want to turn of the pihole for a special amount of time (this example is 45 seconds). After that it shall turn back on automatically. So when I turn on the switch it does exactly that. That works fine.
When I turn the switch off it should turn on the pihole right away again.
Point is: do you understand that sentence involves you actually turning the switch off in the gui, or using the “switch.turn_off” action, e.g. in an automation?
Toggle only works if the state updates properly. It requires the state of your entity to be ‘on’ in order for it to run the off commands. I’d wager you aren’t waitng for the template to catch up. (the state)
That can be handled by changing the script’s mode to restart and enhancing its logic so that its behavior depends on the current state of switch.pi_hole. If you do that, you eliminate the need for a Template Switch.