Template - Fan - Convert light device

I have a Fibaro light dimmer that uses a 0-10V signal to dim lights, but I use it to control a central ventilation fan.

In order to benefit from the new automation syntax I want to convert the fan from the light domain to the fan domain via the configuration file, and the usage of the fan template.

Reporting the preset mode works, so when I execute my automatons that set the three brightnesses (180, 126 and 41) the new fan entity reports the correct preset.
Setting a new preset does nothing. Setting a new percentage does nothing.
If possible, I’d prefer to avoid going via a script to set the target speed, but that is mainly because I only have one fan, and it feels over-administrative to need a script as well in order manipulate it.

template:

  - fan:
      - name: 'Takfläkt'
        unique_id: takflakten
        preset_mode: >
          {% if is_state('light.takflakt', 'on') %}
            {% if state_attr('light.takflakt', 'brightness') == 180  %}
              high
            {% elif state_attr('light.takflakt', 'brightness') == 126 %}
              medium
            {% elif state_attr('light.takflakt', 'brightness') == 41 %}
              low
            {% else %}
              undef
            {% endif %}
          {% else %}
            off
          {% endif %}
        turn_on:
          service: homeassistant.turn_on
          entity_id: switch.takflakt
        turn_off:
          service: homeassistant.turn_off
          entity_id: switch.takflakt
        preset_modes:
          - 'low'
          - 'medium'
          - 'high'
          - 'undef'
        percentage: >
          {{ (state_attr('ligh.takflakt', 'brightness')|int * 0.4)|round(0) }}
        set_percentage:
          - action: fan.takflakten
            target:
              entity_id: fan.takflakten
            data:
              percentage: >-
                {% if preset_mode == 'high' %}
                  70
                {% elif preset_mode == 'medium' %}
                  50
                {% elif preset_mode == 'low' %}
                  20
                {% else %}
                  10
                {% endif %}
        set_preset_mode:
          - action: fan.set_percentage
            target:
              entity_id: fan.takflakten
            data:
              percentage: >-
                {% if preset_mode == 'high' %}
                  70
                {% elif preset_mode == 'medium' %}
                  50
                {% elif preset_mode == 'low' %}
                  20
                {% else %}
                  10
                {% endif %}

That section should be setting the value of the light entity’s brightness.

...
        set_preset_mode:
          - action: light.turn_on
            target:
              entity_id: light.takflakt
            data:
              brightness: |
                {% if preset_mode == 'high' %}
                  180
                {% elif preset_mode == 'medium' %}
                  126
                {% elif preset_mode == 'low' %}
                  41
                {% else %}
                  10
                {% endif %}

The set_percentage portion of the config is the actions that should happen when someone sets the percentage from the dashboard or an automation… IIRC, it only has access to the percentage variable, the variable preset_mode has no defined value in that section.

Also, fan.takflakten is not a valid action. Just like set_preset_mode, you can target it at the light entity:

        set_percentage:
          - action: light.turn_on
            target:
              entity_id: light.takflakt
            data:
              brightness: |
                {% if percentage >= 70 %}
                  180
                {% elif  50 <= percentage < 70 %}
                  126
                {% elif 20 <= percentage < 50 %}
                  41
                {% else %}
                  10
                {% endif %}

Lastly, your percentage template has an invalid entity ID 'ligh.takflakt'… it’s missing a “t”.

Putting it all together
template:

  - fan:
      - name: 'Takfläkt'
        unique_id: takflakten
        preset_mode: >
          {% set brightness = state_attr('light.takflakt', 'brightness')|default(0,1) %}
          {% if is_state('light.takflakt', 'on') %}
            {% if brightness == 180  %}
              high
            {% elif brightness == 126 %}
              medium
            {% elif brightness == 41 %}
              low
            {% else %}
              undef
            {% endif %}
          {% else %}
            off
          {% endif %}
        turn_on:
          service: homeassistant.turn_on
          entity_id: switch.takflakt
        turn_off:
          service: homeassistant.turn_off
          entity_id: 
            - switch.takflakt
            - light.takflakt
        preset_modes:
          - 'low'
          - 'medium'
          - 'high'
          - 'undef'
          - 'off'
        percentage: >
          {{ (state_attr('light.takflakt', 'brightness')|int * 0.4)|round(0) }}
        set_percentage:
          - action: light.turn_on
            target:
              entity_id: light.takflakt
            data:
              brightness: |
                {% if percentage >= 70 %}
                  180
                {% elif  50 <= percentage < 70 %}
                  126
                {% elif 20 <= percentage < 50 %}
                  41
                {% else %}
                  10
                {% endif %}
        set_preset_mode:
          - action: light.turn_on
            target:
              entity_id: light.takflakt
            data:
              brightness: |
                {% if preset_mode == 'high' %}
                  180
                {% elif preset_mode == 'medium' %}
                  126
                {% elif preset_mode == 'low' %}
                  41
                {% else %}
                  10
                {% endif %}