Need help with Fan template + Scenes

How to choose dynamic scenes based on selected percentage value?

fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        turn_off:
          scene: scene.office_fan_power
        turn_on:
          scene: scene.office_fan_speed_1
        set_percentage:
          scene: {{ Change scene based on percentage value selected }}
        speed_count: 5
fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        turn_off:
          scene: scene.office_fan_power
        turn_on:
          scene: scene.office_fan_speed_1
        set_percentage:
          - scene: |
              {% set d = {
                0: 'scene.office_fan_power',
                20: 'scene.office_fan_speed_1',
                40: 'scene.office_fan_speed_2',
                60: 'scene.office_fan_speed_3',
                80: 'scene.office_fan_speed_4',
                100: 'scene.office_fan_speed_5'} %}
              {{ d[percentage] }}
        speed_count: 5
Configuration warnings
Invalid config for 'template' from integration 'fan' at configuration.yaml, line 26: Entity ID {% set d = {
  0: 'scene.office_fan_power' is an invalid entity ID for dictionary value 'fans->office_fan->set_percentage->0->scene', got "{% set d = {\n  0: 'scene.office_fan_power',\n  20: 'scene.twenty_percent_scene',\n  40: 'scene.forty_percent_scene',\n  60: 'scene.sixty_percent_scene',\n  80: 'scene.eighty_percent_scene',\n  100: 'scene.office_fan_speed_1'} %}\n{{ d[percentage] }}\n"

Thanks! appreciate your help
It throws warning

Try with the full action instead of the shorthand version:

        set_percentage:
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[percentage] }}

Thanks!!! It works
But it does not save the state; when i update the percentage of a fan from UI and close the window, it reset back to normal state

You need an additional action to set the value of the input number… you’ll need then for on and off as well:

fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        turn_off:
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.state
          - scene: scene.office_fan_power
        turn_on:
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.state
          - scene: scene.office_fan_speed_1
        set_percentage:
          - action: input_number.set_value
            target:
              entity_id: input_number.percentage
            data:
              value: "{{ percentage }}"
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[percentage] }}

Still not working