How to use if/else statements to calculate values?

I have an ESP8266-based air quality monitor sending PM2.5 data to my home assistant instance. I found the Arduino code to calculate US AQI, but I would like to understand how to do this with a template sensor.

How do I go about converting from Arduino to YAML?

// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
  if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
  else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
  else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
  else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
  else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
  else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
  else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
  else return 500;
};

Thanks!

Here is an example to help you from my template.yaml file:

  - name: "h reset sdb"
    state: >-
      {% set min = ((states('input_datetime.h_retour_sdb')) [3:5] | int(default=0)) - ((states('sensor.time')) [3:5] | int(default=0)) %}
      {% set hour = ((states('input_datetime.h_retour_sdb')) [:2] | int(default=0)) - ((states('sensor.time')) [:2] | int(default=0)) %}
      {% if (hour < 0) or ((hour == 0) and (min < 0)) %}
         {% set res = ((hour*60)+(24*60)+min) %}
      {% else %}
         {% set res = ((hour*60)+min) %}
      {% endif %}
      {{ res }}
    unit_of_measurement: "minutes"

What is the entity that will represent the value of pm02 in your formula?

Thanks for all the suggestions. The pm02 will be coming from the state value of sensor.airgradient_sensor_particulate - it is sending out number values for PM2.5, and I want to convert them to AQI.