How to configure template fan properly

Hi, I am trying to set up my fan as a template fan but am not having much luck. I send shell commands to turn my fan to different speeds and off. What happens now is that I can turn on the fan and it will go to low speed. When I change the speed nothing happens. I think I am doing something wrong with value template and speed template.

Code:

- platform: template
    fans:
      study_fan:
        friendly_name: "Study Fan"
        value_template: "{{ states('fan.study_fan', 'on') }}"
        speed_template: "{{ states('fan.study_fan.speeds') }}"
        turn_on:
          service: shell_command.study_fan_low
        turn_off:
          service: shell_command.study_fan_off
        set_speed:
          service: shell_command.study_fan_low
          data_template:
            entity_id: >
              {% if speed == 'High' %}
                shell_command.study_fan_high
              {% elif speed == 'Medium' %}
                shell_command.study_fan_med
              {% elif speed == 'Low' %}
                shell_command.study_fan_low
              {% else %}
                shell.command.study_fan_low
              {% endif %}
        speeds:
          - 'High'
          - 'Medium'
          - 'Low'

Your help would be appreciated.

The documentation for this is really poor and doesn’t properly spell out how to use the component. I tried helping someone a few weeks ago and we got no where. You may want to get in contact with the dev by checking out who wrote the component in github.

Do you have any simple information of how to use this component?

Not really, the only information I have is the documentation provided by the component.

I mean just looking at what you wrote, I can provide pointers.

  • Your shell services need to be called through a service_template without an entity id. In your code, you’re always running the ‘low’ shell service with different entity_id’s. Thats not how services work for shell commands. Each service command is unique and you need to account for that by using a service_template.

  • Your speed template seems wrong. The purpose of a speed_template is to provide the user interface with a set speed. From the way your code looks, ‘fan.study_fan.speeds’ appears to return a list of speeds, not a single speed that the fan is currently set to. You need a way, with templates, to determine the speed in which the fan is currently set. I’m not sure how this is done with your shell commands.

  • Your turn on service is kinda weak in my opinion. You could remember the state and attempt to return to it instead of just using low when you turn on the device.

Anyways, here are the changes that I can help with. Service template and possibly the turn_on rework:

- platform: template
    fans:
      study_fan:
        friendly_name: "Study Fan"
        value_template: "{{ states('fan.study_fan', 'on') }}"
        speed_template: "{{ states('fan.study_fan.speeds') }}"
        turn_on:
          service_template: >
            {% if speed == 'High' %}
              shell_command.study_fan_high
            {% elif speed == 'Medium' %}
              shell_command.study_fan_med
            {% elif speed == 'Low' %}
              shell_command.study_fan_low
            {% else %}
              shell.command.study_fan_low
            {% endif %}
        turn_off:
          service: shell_command.study_fan_off
        set_speed:
          service_template:
            {% if speed == 'High' %}
              shell_command.study_fan_high
            {% elif speed == 'Medium' %}
              shell_command.study_fan_med
            {% elif speed == 'Low' %}
              shell_command.study_fan_low
            {% else %}
              shell.command.study_fan_low
            {% endif %}
        speeds:
          - 'High'
          - 'Medium'
          - 'Low'
1 Like