Template switches get turned on when on?

Hi

I have recently discovered template switches and am rewriting a bunch of toggles/automations as switches and scripts to allow me to operate things from automations and from the UI if I need to.

I have some funny behaviour that I am trying to debug.

Let’s say I have a template switch switch.fun_mode, where the turn_on and turn_off functions are reasonably long scripts that take some time to execute.

Let’s now say that switch.fun_mode is already ON. If I call from another script the service:

- service: switch.turn_on
  target:
    entity_id: switch.fun_mode

does this execute the turn_on script again, or does it skip it because the switch state is on? If the former, is there some simple way to avoid an ON template switch from being turned on by another script?

I guess the explanations provided in this thread didn’t sink in?

Evidently no. I am asking a different question here, at least as far as I understand.

Test template switch:

    test_switch:
      friendly_name: Test Switch
      icon_template: mdi:test-tube
      value_template: "{{is_state('input_boolean.test_switch','on')}}"
      unique_id: "834ut9j83y498v345345346456t"
      turn_on:
        - service: notify.persistent_notification
          data:
            title: "Turn it on"
            message: "Switch was turned on"
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.test_switch
      turn_off:
        - service: notify.persistent_notification
          data:
            title: "Turn it off"
            message: "Switch was turned off"
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.test_switch

I can flip it by toggling in lovelace ON/OFF as much as I wish and it is working as I think it should – a sequence of on/off executions - on-off-on-off-on-off. But if I turn it on and then service call switch.turn_on from a script, it will run the on script again – on-on-on-on. I wasn’t expecting that.

I can think of wrapping the turn_on: script of the switch inside some choose condition which will check if is on already and skip it? As far as I can see, throwing it into an external script with a single execution mode isn’t going to help, since now the script is completing long before I re-trigger it.