Template Switch turn_off doesn‘t get executed

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

The turn_off script looks like this:

alias: Enable PiHole
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: script.pi_hole_45_sekunden_deaktivieren
            state: "on"
        sequence:
          - service: script.turn_off
            data: {}
            target:
              entity_id: script.pi_hole_45_sekunden_deaktivieren
      - conditions:
          - condition: state
            entity_id: script.pi_hole_2_minuten_deaktivieren
            state: "on"
        sequence:
          - service: script.turn_off
            data: {}
            target:
              entity_id: script.pi_hole_2_minuten_deaktivieren
      - conditions:
          - condition: state
            entity_id: script.pi_hole_5_minuten_deaktivieren
            state: "on"
        sequence:
          - service: script.turn_off
            data: {}
            target:
              entity_id: script.pi_hole_5_minuten_deaktivieren
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.pi_hole
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id:
        - switch.pi_hole
        - binary_sensor.pi_hole_status
mode: single
icon: mdi:pi-hole

Do you expect the turn_off script to be triggered after the turn_on script has finished?
That won’t work.

Yes, that’s what I want. As you can see the turn_off script stops the started script.

But as I wrote, I also just changed an input-text-field and that doesn’t work either.

Plus I’ve done many other of these buttons before and they worked. To me it seems that an Home Assistant Update changed something.

Your turn_off script turns on switch.pi_hole.

What does your turn_on script do? Does it turn off switch.pi_hole?

Post the turn_on script.

Yes it turns it off. Here is the code:

alias: Pi-Hole 45 Sekunden deaktivieren
sequence:
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.pi_hole
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: binary_sensor.pi_hole_status
  - delay:
      hours: 0
      minutes: 0
      seconds: 45
      milliseconds: 0
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.pi_hole
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: binary_sensor.pi_hole_status
mode: single
icon: mdi:shield-lock-open

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).

1 Like

In that case, use switch.pihole's state in the Template Switch’s value_template.

    #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: "{{ is_state('switch.pi_hole', 'off') }}"
      turn_on:
        service: script.pi_hole_45_sekunden_deaktivieren
      turn_off:
        service: script.enable_pihole

EDIT

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.

As I wrote the turn_off action of the template doesn‘t get fired / executed.

So nothing(!) in the turn_off section is done by Home Assistant. It’s not(!) the script.

That is my problem.

I looked at the switch in the GUI and its set to toggle the switch on press.

Can you answer this: What are your expectations with this switch? When do you expect it to run the “turn off actions”?

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?

If that’s what you want then your script called ‘Pi-Hole 45 Sekunden deaktivieren’ does that.

Why are you also trying to use a Template Switch?

I use the switch:

Here is the reason:

No clue. Works prefectly, light switches on and off

- platform: template
  switches:
    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
pi_hole_45_sekunden_deaktivieren:
  sequence:
    - service: light.turn_on
      data: {}
      target:
        entity_id: light.cololight_hexa_2
    - delay: "00:00:45"
    - service: light.turn_off
      data: {}
      target:
        entity_id: light.cololight_hexa_2
pi_hole_2_minuten_deaktivieren:
  sequence:
    - service: light.turn_on
      data: {}
      target:
        entity_id: light.cololight_hexa_2
    - delay: "00:02:00"
    - service: light.turn_off
      data: {}
      target:
        entity_id: light.cololight_hexa_2
pi_hole_5_minuten_deaktivieren:
  sequence:
    - service: light.turn_on
      data: {}
      target:
        entity_id: light.cololight_hexa_2
    - delay: "00:05:00"
    - service: light.turn_off
      data: {}
      target:
        entity_id: light.cololight_hexa_2

enable_pihole:
  sequence:
    - choose:
        - conditions:
            - condition: state
              entity_id: script.pi_hole_45_sekunden_deaktivieren
              state: "on"
          sequence:
            - service: script.turn_off
              data: {}
              target:
                entity_id: script.pi_hole_45_sekunden_deaktivieren
        - conditions:
            - condition: state
              entity_id: script.pi_hole_2_minuten_deaktivieren
              state: "on"
          sequence:
            - service: script.turn_off
              data: {}
              target:
                entity_id: script.pi_hole_2_minuten_deaktivieren
        - conditions:
            - condition: state
              entity_id: script.pi_hole_5_minuten_deaktivieren
              state: "on"
          sequence:
            - service: script.turn_off
              data: {}
              target:
                entity_id: script.pi_hole_5_minuten_deaktivieren
    - service: light.turn_off
      data: {}
      target:
        entity_id: light.cololight_hexa_2
    - delay:
        hours: 0
        minutes: 0
        seconds: 1
        milliseconds: 0
    - service: homeassistant.update_entity
      data: {}
      target:
        entity_id:
          - light.cololight_hexa_2
  mode: single
  icon: mdi:pi-hole

Animation

Animation2

1 Like

Thank you so much. You‘re right. If I do it over the entity itself in the entity browser it works flawless.

So it must be the Button-Card:

type: horizontal-stack 
cards:
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: script.pi_hole_45_sekunden_deaktivieren
    icon_height: 50px
    show_state: true
    name: 45 Sekunden deaktivieren
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: script.pi_hole_2_minuten_deaktivieren
    show_state: true
    icon_height: 50px
    name: 2 Minuten deaktivieren
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: script.pi_hole_5_minuten_deaktivieren
    name: 5 Minuten deaktivieren
    icon_height: 50px
    show_state: true

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)

1 Like

Lol. I wondered what I had to do for you to tell us how you actually switched the switch :smiley:

1 Like

I just my error 🤦🏻

In the button card I call the script, not the switch…

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.