Template switch: why actions are not performed?

Trying to learn template switches from here:

Here is my test switch:

  - platform: template
    switches:
      test_switch:
        value_template: "{{ is_state('input_boolean.test_boolean', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.test_boolean
          - service: notify.telegram_chat_sys
            data:
              message: "test: switch ON"
        turn_off:
          - service: input_boolean.turn_off
            entity_id: input_boolean.test_boolean
          - service: notify.telegram_chat_sys
            data:
              message: "test: switch OFF"

Here is a card:

type: 'custom:stack-in-card'
cards:
  - type: entities
    entities:
      - entity: input_boolean.test_boolean
  - type: button
    tap_action:
      action: toggle
    entity: switch.test_switch
    icon_height: 40px
    state_color: true
    show_state: true
  - type: logbook
    entities:
      - entity: switch.test_switch

There are two scenarios:

  1. Whenever the “test boolean” is toggled, the “test switch” is also toggled.
  2. Whenever the “test switch” is toggled, the “test boolean” is also toggled - and messages are sent via Telegram!

Why the messages are NOT sent in Scenario 1 ?
Does it mean that “turn_on” and “turn_off” actions from config.yaml are not supposed to be run in case of “switch is toggled because of some template, not because a user toggled it”?

Correct.

If you do something to change the value of the switch’s value_template (other than toggling the switch) - in this case changing the input_boolean state - then the new state will be reflected by the switch but the turn on or turn off services are not executed.

Consider what would happen if it did execute the services but there was a short delay before the template updated. The switch would oscillate. This would be bad.

1 Like

Thanks a lot for your reply!
I faced some lack of documentation about switches on home-assistant.io, found only these pages:

Currently trying to understand how to implement Wake-on-lan switch, and that test_switch was the 1st switch in my setup.

If you want to be notified when your WoL switch is on, use an automation.

trigger:
  platform: state
  entity_id: switch.your_wol_sw
action:
  service: notify.notify
  data:
    message: "The WoL switch is {{ trigger.to_state.state }]"

Thank you, but now I cannot understand a “physical meaning” of a switch.
I believe that a switch has 2 states - ON & OFF.
And every toggle causes changing ON->OF->ON … and so on.

Now let’s have a look at that template switch:

  - platform: template
    switches:
      test_switch:
        value_template: "{{ is_state('input_boolean.test_boolean', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.test_boolean
          - service: notify.telegram_chat_sys
            data:
              message: "test: switch ON"
        turn_off:
          - service: input_boolean.turn_off
            entity_id: input_boolean.test_boolean
          - service: notify.telegram_chat_sys
            data:
              message: "test: switch OFF"

Here:

  1. It is specified how the switch gets “initial state” and what causes it’s change:
        value_template: "{{ is_state('input_boolean.test_boolean', 'on') }}"
  1. Also it is specified what happens when the switch toggled by a user - the original input_boolean is toggled too:
          - service: input_boolean.turn_off
            entity_id: input_boolean.test_boolean

But is there any sense to specify a switch WITHOUT that “service: input_boolean.turn_off / on” ?
Let’s create another switch - similar to the 1st switch but without toggling the “input_boolean”:

  - platform: template
    switches:
      test_switch_2:
        value_template: "{{ is_state('input_boolean.test_boolean', 'on') }}"
        turn_on:
          - service: notify.telegram_chat_sys
            data:
              message: "test 2: switch ON"
        turn_off:
          - service: notify.telegram_chat_sys
            data:
              message: "test 2: switch OFF"

And a similar card:

type: 'custom:stack-in-card'
cards:
  - type: entities
    entities:
      - entity: input_boolean.test_boolean
  - type: horizontal-stack
    cards:
      - type: button
        tap_action:
          action: toggle
        entity: switch.test_switch
        icon_height: 40px
        state_color: true
        show_state: true
      - type: button
        tap_action:
          action: toggle
        entity: switch.test_switch_2
        icon_height: 40px
        state_color: true
        show_state: true
  - type: logbook
    entities:
      - entity: switch.test_switch
      - entity: switch.test_switch_2


And here only the 1st switch can be toggled to ON, the 2nd switch seems to always switch back to OFF automatically (since it depends on “input_boolean” value which is not changed).
What is a purpose of this behaviour?

You have defined the second switch incorrectly.

It is required that the service you define in the turn on/off actions will change the state of the switch. If this is not the case the switch will not operate correctly.

You are right, it makes sense.
Thank you very much for the support!