How to trigger a scene based on an a states if statement - Error is an invalid entity ID for dictionary value

I have two scenes that will either increase or decrease the fan speed. I am trying to create a condition on which scene will be triggered based on the current fan speed and the set fan speed.

I am currently getting the below error:

Invalid config for ‘fan’ from integration ‘template’ at configuration.yaml, line 47: Entity ID {% if states(‘input_number.percentage’) < percentage %} scene.fan_speed_up {% else %} scene.fan_speed_down {% endif %} is an invalid entity ID for dictionary value ‘fans->bedroom_fan->set_percentage->1->scene’, got “{% if states(‘input_number.percentage’) < percentage %}\n scene.fan_speed_up\n{% else %}\n scene.fan_speed_down\n{% endif %}”, please check the docs at Template - Home Assistant

My fan template below:

fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "Bedroom fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: >
          {{ states('input_number.percentage') if is_state('input_boolean.state', 'on') else 0 
          }}
        oscillating_template: "{{ states('input_select.osc') }}"
        turn_on:
          - scene: scene.fan_power
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.state
        turn_off:
          - scene: scene.fan_power 
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.state
        set_oscillating:
          scene: scene.fan_swing
        speed_count: 3
        set_percentage:
          - action: input_boolean.turn_{{ 'on' if percentage > 0 else 'off' }}
            target:
              entity_id: input_boolean.state
          - scene: >-
              {% if states('input_number.percentage') < percentage %}
                scene.fan_speed_up
              {% else %}
                scene.fan_speed_down
              {% endif %}
          - action: input_number.set_value
            target:
              entity_id: input_number.percentage
            data:
              value: "{{ percentage }}"
        set_percentage:
          - action: input_boolean.turn_{{ 'on' if percentage > 0 else 'off' }}
            target:
              entity_id: input_boolean.state
          - action: scene.turn_on
            target:
              entity_id: >
                {% if states('input_number.percentage')|float < percentage %}
                  scene.fan_speed_up
                {% else %}
                  scene.fan_speed_down
                {% endif %}

Thanks @tom_l

I get no more errors, however the scene doesn’t look like it’s triggering. I have also noticed that the first action of set_percentage doesn’t turn on the fan if I set a fan speed other than 0. I have created a second action to trigger my fan_power scene, but no luck. Have I missed defining ‘percentage’ somewhere, or is there another issue?

Updated template below:

fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "Bedroom fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: >
          {{ states('input_number.percentage') if is_state('input_boolean.state', 'on') else 0 
          }}
        oscillating_template: "{{ states('input_select.osc') }}"
        turn_on:
          - scene: scene.fan_power
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.state
        turn_off:
          - scene: scene.fan_power 
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.state
        set_oscillating:
          scene: scene.fan_swing
        speed_count: 3
# fan does not turn on  from off state when increasing percentage
        set_percentage:
          - action: input_boolean.turn_{{ 'on' if percentage > 0 else 'off' }}
            target:
              entity_id: input_boolean.state
# add scene to turn fan on if the input percetnage is > 0 and if the fan is currently off
          - action: scene.turn_on
            target:
              entity_id: >
                {% if percentage > 0 and is_state('input_boolean.state', 'off') %}
                  scene.fan_power
                {% endif %}
# no errors but this is not working. logs also dont show the scene being triggered
          - action: scene.turn_on
            target:
              entity_id: >
                {% if states('input_number.percentage'|float) < percentage %}
                  scene.fan_speed_up
                {% else %}
                  scene.fan_speed_down
                {% endif %}
          - action: input_number.set_value
            target:
              entity_id: input_number.percentage