How to use variables in script for conditional actions (Solved)

Use case:
I’m using an IR blaster to control a fan with three steps of speed under one button(same code), i.e. in a sequence of OFF->ON in Low speed -> Med speed -> Hi speed -> OFF. So I am planning to put conditional actions with the following idea:
Use a numeric variable to store the button status. After studying various post, I found one way is to use input_slider like this:

input_slider:
fan_speed:
name: Fan Speed
initial: 0
min: 0
max: 3
step: 1

Then my question is how can I update this variable in a script for the following purpose:
Speed_up script:
execute IR code only if fan_speed is less than 3, and then add 1 into fan_speed
Speed_down script:
execute IR code only if fan_speed is greater than 1, and then minus 1 from fan_speed
Fan_off script:
execute IR code (fire the right number of code) based on the value of fan_speed.

I’m trying to write the fan_speed_up script below:
fan_speed_up:
alias: Fan Speed Up
sequence:
- condition:
condition: numeric_state
entity_id: input_slider.fan_speed
below: 3
- service: input_slider.select_value
value_template:
entity_id: “{{fstates.input_slider.fan_speed.state | float + 1}}”
- service: Send the IR code*

I know the above format is not correct and the fan_off script is even more difficult in my project. But the first thing I would like to get help is on how to manipulate a variable in that way with correct format in such kind of script.

Many thanks!

I have done the first script successfully. Here it is:

script:
fan_speed_up:
alias: Fan Speed Up
sequence:
- condition: numeric_state
entity_id: input_slider.fan_speed
below: 3
- service: Send IR Code
- service: input_slider.select_value
data_template:
entity_id: input_slider.fan_speed
value: ‘{{ states.input_slider.fan_speed.state | float + 1}}’

So my next challenge is to work out the speed down script and then the most difficult one which is to execute different command based on the value of the fan speed.

I have completed the automation code eventually after studying some codes from others. Here it is:

trigger:
  platform: state
  entity_id: switch.fan
  to: 'off'
action:
  - service: script.turn_on
    data_template:
      entity_id: >
        {% if is_state('input_slider.fan_speed', '1.0') %}
          script.ir_fan_power3x
        {% elif is_state('input_slider.fan_speed','2.0') %}
          script.ir_fan_power2x
        {% elif is_state('input_slider.fan_speed', '3.0') %}
          script.ir_fan_power
        {% endif %}

HA is really a powerful tool for customization!