Help with fan template – dynamically select script based

I am trying to create a fan template that not only controls my Insteon fanlinc, but also updates the connected keypad with the correct light sequence.

I copied the template from the integrations page and it was able to properly control the fan. I then tried to add a service to the set_preset_mode configuration variable. This is what I have:

set_preset_mode:
  - service: fan.set_percentage
    target:
      entity_id: fan.percentage_fan
    data:
      percentage: >-
        {% if preset_mode == 'high' %}
          100
        {% elif preset_mode == 'medium' %}
          66
        {% elif preset_mode == 'low' %}
          33
        {% else %}
          0
        {% endif %} 
  - service: script.turn_on
    data: {}
    target:
      entity_id: >-
        {% if preset_mode == 'high' %}
          script.office_keypad_hi
        {% elif preset_mode == 'medium' %}
          script.office_keypad_med
        {% elif preset_mode == 'low' %}
          script.office_keypad_low
        {% else %}
          script.office_keypad_off
        {% endif %} 

The service call fan.set_percentage works. The service call to script.turn_on does not appear to be called at all. I have tested the scripts individually through the webUI and they also work. For completeness, my full package is below. Any help on this would be appreciated.

fan:
  - platform: template
    fans:
      office:
        friendly_name: "office"
        value_template: "{{ states('fan.office_fanlinc_fan') }}"
        percentage_template: "{{ state_attr('fan.office_fanlinc_fan', 'percentage') }}"
        preset_mode_template: >
          {% if is_state('fan.percentage_fan', 'on') %}
            {% if state_attr('fan.percentage_fan', 'percentage') == 100  %}
              high
            {% elif state_attr('fan.percentage_fan', 'percentage') == 66 %}
              medium
            {% else %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}      
        turn_on:
          - service: fan.turn_on
            target:
              entity_id: fan.office_fanlinc_fan
        turn_off:
          - service: fan.turn_off
            target:
              entity_id: fan.office_fanlinc_fan
        set_percentage:
          - service: fan.set_percentage
            target:
              entity_id: fan.office_fanlinc_fan
            data:
              percentage: "{{ percentage }}"
        set_preset_mode:
          - service: fan.set_percentage
            target:
              entity_id: fan.percentage_fan
            data:
              percentage: >-
                {% if preset_mode == 'high' %}
                  100
                {% elif preset_mode == 'medium' %}
                  66
                {% elif preset_mode == 'low' %}
                  33
                {% else %}
                  0
                {% endif %} 
          - service: script.turn_on
            data: {}
            target:
              entity_id: >-
                {% if preset_mode == 'high' %}
                  script.office_keypad_hi
                {% elif preset_mode == 'medium' %}
                  script.office_keypad_med
                {% elif preset_mode == 'low' %}
                  script.office_keypad_low
                {% else %}
                  script.office_keypad_off
                {% endif %}               
        preset_modes:
          - "off"
          - "low"
          - "medium"
          - "high"
        speed_count: 3

script:
  office_keypad_off:
    sequence:
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_6

  office_keypad_low:
    sequence:
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_6

  office_keypad_med:
    sequence:
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_6
        
  office_keypad_hi:
    sequence:
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_6

I managed to figure this out and will share here for future reference. Essentially, the dynamic call to the scripts was in the wrong section. It should have ben in set_percentage. Here is my working package:

fan:
  - platform: template
    fans:
      office:
        friendly_name: "office"
        value_template: "{{ states('fan.office_fanlinc_fan') }}"
        percentage_template: "{{ state_attr('fan.office_fanlinc_fan', 'percentage') }}"
        preset_mode_template: >
          {% if is_state('fan.percentage_fan', 'on') %}
            {% if state_attr('fan.percentage_fan', 'percentage') == 100  %}
              high
            {% elif state_attr('fan.percentage_fan', 'percentage') == 66 %}
              medium
            {% else %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}      
        turn_on:
          - service: fan.turn_on
            target:
              entity_id: fan.office_fanlinc_fan
        turn_off:
          - service: fan.turn_off
            target:
              entity_id: fan.office_fanlinc_fan
        set_percentage:
          - service: fan.set_percentage
            target:
              entity_id: fan.office_fanlinc_fan
            data:
              percentage: "{{ percentage }}"
          - service: script.turn_on
            data: {}
            target:
              entity_id: >-
                {% if percentage == 100 %}
                  script.office_keypad_hi
                {% elif percentage > 60 %}
                  script.office_keypad_med
                {% elif percentage > 30 %}
                  script.office_keypad_low
                {% else %}
                  script.office_keypad_off
                {% endif %}               
        set_preset_mode:
          - service: fan.set_percentage
            target:
              entity_id: fan.percentage_fan
            data:
              percentage: >-
                {% if preset_mode == 'high' %}
                  100
                {% elif preset_mode == 'medium' %}
                  66
                {% elif preset_mode == 'low' %}
                  33
                {% else %}
                  0
                {% endif %} 
        preset_modes:
          - "off"
          - "low"
          - "medium"
          - "high"
        speed_count: 3

script:
  office_keypad_off:
    sequence:
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_6

  office_keypad_low:
    sequence:
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_6

  office_keypad_med:
    sequence:
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_6
        
  office_keypad_hi:
    sequence:
      - service: switch.turn_on
        entity_id: switch.office_keypadlinc_btn_3
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_4
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_5
      - delay: 0.5
      - service: switch.turn_off
        entity_id: switch.office_keypadlinc_btn_6

Thank you for posting this! It helped me get very close to a solution for my whole house fan (controlled with a Shelly 3 Pro). However, did you find a way to deal with what happens if you select two speeds right after each other? The delay in the script (which is a good idea) unfortunately causes it to abort turning off the original speed before the new speed is turned on.