Template fan execution error

I created it through chatGPT

fan:
- platform: template
    fans:
      template_fan:
        friendly_name: "範本風扇"
        value_template: >
          {% if is_state('binary_sensor.led_state', 'on') or
                 is_state('binary_sensor.led_state2', 'on') or
                 is_state('binary_sensor.led_state3', 'on') or
                 is_state('binary_sensor.led_state4', 'on') or
                 is_state('binary_sensor.led_state5', 'on') or
                 is_state('binary_sensor.led_state6', 'on') %}
            on
          {% else %}
            off
          {% endif %}
        percentage_template: >
          {% if is_state('binary_sensor.led_state6', 'on') %}
            100
          {% elif is_state('binary_sensor.led_state5', 'on') %}
            83
          {% elif is_state('binary_sensor.led_state4', 'on') %}
            66
          {% elif is_state('binary_sensor.led_state3', 'on') %}
            50
          {% elif is_state('binary_sensor.led_state2', 'on') %}
            33
          {% elif is_state('binary_sensor.led_state', 'on') %}
            16
          {% else %}
            0
          {% endif %}
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.switch1_2
        turn_off:
          service: switch.turn_on
          target:
            entity_id: switch.switch1_2
        set_percentage:
          service: script.set_fan_speed
          data:
            percentage: "{{ percentage }}"
        speed_count: 6

script:
  set_fan_speed:
    sequence:
      - repeat:
          count: "{{ ((percentage - state_attr('fan.template_fan', 'percentage')) // 17) | abs }}"
          sequence:
            - choose:
                - conditions:
                    # 減少風速
                    - condition: template
                      value_template: "{{ percentage < state_attr('fan.template_fan', 'percentage') }}"
                  sequence:
                    - service: switch.turn_on
                      target:
                        entity_id: switch.switch2_2
                - conditions:
                    # 增加風速
                    - condition: template
                      value_template: "{{ percentage > state_attr('fan.template_fan', 'percentage') }}"
                  sequence:
                    - service: switch.turn_on
                      target:
                        entity_id: switch.switch3_2

There is a bug in the use of this yaml. When the wind speed is 83 or 100, the fan will be turned off. It must be adjusted to 66 to display the status normally. Can you help me improve the problem?

Hello pan80826,

Welcome to the forum. Let’s see if I can be of any assistance.

First Thanks for your question. Unfortunately very few of the people here have much success ‘fixing’ code written br ChatGPT. That AI is good at writing language. This stuff looks like code, but the AI does not understand syntax or how to program. You end up with code-like gibberish that most struggle to correct and generally has to be re-written.

For instance there a lot of binary sensors, switches, and other things that are called out there. Did you create any of those?

You will be much better served if you go to the UI and write some of this by hand. It will help you with list of available entities and devices so at least that part will work.

Maybe someone can look at this pseudo-code and see the problem, but I sure cannot.

Thanks for the reply Sir_Goodenough, I think I should provide the meaning of each id, maybe someone can help.

switch.switch1_2, switch.switch2_2 and switch.switch3_2 are short-term switches, which will automatically turn off 1 second after being turned on. switch.switch1_2 is the fan function of turning on and off the fan. Switch.switch2_2 is turned on to reduce the air volume, and switch.switch3_2 is turned on to increase the air volume. , binary_sensor.led_state is on, the wind speed is 1, binary_sensor.led_state2 is on, the wind speed is 2, and binary_sensor.led_state6 is on, the wind speed is 6.

I hope someone with this information can help me create a template fan.