Trying to define PWM fan speeds using brightness_pct

I am trying to control 3 different fans and their speeds (fans are a 4 wire PWM computer fan, 12DCV) using brightness_pct and a data template based on certain temperatures. This is to regulate temperature inside of an indoor greenhouse. I am trying to create a data template that will turn a fan onto 20% power when the temperature is between 23-24C and so on for further temperature ranges. Here is what I have but not able to get it to work.

brightness_pct: >
  {% set temp = states('sensor.dht22_temperature')|float %} {% if temp > 23 or
  temp < 23.9 %} 20   {% elif temp > 24 or temp < 24.9 %} 25   {% elif temp > 25
  or temp < 25.9 %} 30   {% elif temp > 26 or temp < 28.9 %} 35   {% elif temp >
  29 %} 70   {% else %} 0   {% endif %}

You could also make the speed/dutycycle continuous? I put this together for a bathroom fan. Not exactly the same as your setup, but the principles for mapping a brightness to/from a dutycycle or RPM based on some sensor value should be transferable.
Edit: Just realised my example doesn’t have the temperature mapping. Just the RPM/brightness mapping. But still think a similar approach could be used for that.

light:
  - platform: template
    lights:
      pax_calima_boost:
        friendly_name: PAX Calima Boost
        value_template: >-
          {% if states('sensor.pax_calima_fan_speed') | float > 0 %}
            on
          {% else %}
            off
          {% endif %}
        level_template: >-
          {{ (states('sensor.pax_calima_fan_speed') | float / 2400 * 254) | round(0) }}
        icon_template: >-
          {% if states('sensor.pax_calima_fan_speed') | float > 0 %}
            mdi:fan
          {% else %}
            mdi:fan-off
          {% endif %}
        turn_on:
          service: scene.turn_on
          data:
            entity_id: scene.pax_calima_before
        turn_off:
        - service: scene.create
          data:
            scene_id: pax_calima_before
            snapshot_entities:
            - light.pax_calima_boost
        - service: rest_command.pax_calima_turn_off_boost_mode
        set_level:
          service: shell_command.pax_calima_boost_node_red
          data_template:
            boost_speed: >-
              {% if brightness | float == 0 %}
                0
              {% else %}
                {{ ((((2400-275)*(brightness) / 254) + 275 ) / 25) | int * 25 }}
              {% endif %}
            boost_seconds: >-
              900