Template fan set speed

Hey guys, I have a problem setting up fan speed in template fan. The documentation doesn’t mention anything about setting a fan speed.

I have 3 script: speed 1, speed 2, speed 3

How do I call those script to set fan speed?

My current code:

>     fan:
>       - platform: template
>         fans:
>           living_room_fan:
>             friendly_name: "Living Room fan"
>             value_template: "{{ states('input_boolean.fan.status') }}"
>             speed_template: "{{ states('input_select.fan.speed') }}"
>             turn_on:
>               service: script.1542198672511
>             turn_off:
>               service: script.1542198672611
>             speeds:
>               - 'high'
>               - 'medium'
>               - 'low'

Any help is always appreciated!

This maps a list of speed names to script names. Then looks in the list and executes the script if the speed is in the map (it always should). For safety, added an else condition to turn it off if an unknown speed is set (should never happen). In that case, it will turn off.

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Living Room fan"
        value_template: "{{ states('input_boolean.fan.status') }}"
        speed_template: "{{ states('input_select.fan.speed') }}"
        turn_on:
          service: script.1542198672511
        turn_off:
          service: script.1542198672611
        speeds:
          - 'high'
          - 'medium'
          - 'low'
        set_speed:
          service_template: >
            {% set mapper = { 'high':'script.1', 'medium':'script.2', 'low':'script.3' } %}
            {{ mapper[speed] if speed in mapper else script.1542198672611 }}

If you are having trouble understanding the code, this is identical functionality with more lines:

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Living Room fan"
        value_template: "{{ states('input_boolean.fan.status') }}"
        speed_template: "{{ states('input_select.fan.speed') }}"
        turn_on:
          service: script.1542198672511
        turn_off:
          service: script.1542198672611
        speeds:
          - 'high'
          - 'medium'
          - 'low'
        set_speed:
          service_template: >
            {% if speed == 'high' %}
              script.1
            {% elif speed == 'medium' %}
              script.2
            {% elif speed == 'low' %}
              script.3
            {% else %}
              script.1542198672611
            {% endif %}
2 Likes

I got a problem, why the set speed command activate the turn on/off script first then call the set speed script? Checked the script numbers, nothing wrong.

2 Likes