Help with Fan Template

Can anyone help with my fan configuration?

I have a Fan with 5 buttons, 25%, 50%, 75%, 100% and OFF. I have each of these buttons mapped to scripts which control the fan via an IR blaster. That part works. Notably, there is no explicit ON button, any of the 4 settings besides OFF turns the fan on.

I tried to use the fan template in my config to setup an appropriate controller for my dashboard. It is mostly working, except HA never seems to know if the fan is on. If i move the slider to 25%, 75% etc. the fan responds accordingly, but in HA the fan is still considered off. This is also creating problems for trying to toggle the fan on and off. Here is my configuration. Can anyone help me fix it?

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Living Room Fan"
        value_template: "{{ is_state('input_boolean.living_room_fan_power', 'on') }}"
        percentage_template: "{{ states('input_number.living_room_fan_speed') }}"
        turn_on:
          action: script.living_room_fan_25
        turn_off:
          action: script.living_room_fan_off
        set_percentage:
          action: >
            {% if percentage == 25 %}
              script.living_room_fan_25
            {% elif percentage == 50 %}
              script.living_room_fan_50
            {% elif percentage == 75 %}
              script.living_room_fan_75
            {% elif percentage == 100 %}
              script.living_room_fan_100
            {% endif %}
        speed_count: 4

Hello jacob-r-g,

Welcome to the forum. Let’s see if I can be of any assistance.

Isn’t that technically a 5 speed fan, including 0% (off)?
It seems to me to be an elif short.

1 Like

Hi @Sir_Goodenough Thanks for the suggestion!

I tried something like this when I first set things up and it didn’t work (though maybe I implemented it incorrectly?). I have tried this previously:

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Living Room Fan"
        value_template: "{{ is_state('input_boolean.living_room_fan_power', 'on') }}"
        percentage_template: "{{ states('input_number.living_room_fan_speed') }}"
        turn_on:
          action: script.living_room_fan_25
        turn_off:
          action: script.living_room_fan_off
        set_percentage:
          action: >
            {% if percentage == 0 %}
              script.living_room_fan_off
            {% elif percentage == 25 %}
              script.living_room_fan_25
            {% elif percentage == 50 %}
              script.living_room_fan_50
            {% elif percentage == 75 %}
              script.living_room_fan_75
            {% elif percentage == 100 %}
              script.living_room_fan_100
            {% endif %}
        speed_count: 5

But when i use that configuration for the template, setting any percent fails from my dashboard and i get the message

Failed to perform the action fan/set_percentage. Template rendered invalid service:

Well if that works, then this should as well


            {% if percentage == 25 %}
              script.living_room_fan_25
            {% elif percentage == 50 %}
              script.living_room_fan_50
            {% elif percentage == 75 %}
              script.living_room_fan_75
            {% elif percentage == 100 %}
              script.living_room_fan_100
            {% elif percentage == 0 %}
              script.living_room_fan_off
            {% endif %}

That is assuming this exists:
script.living_room_fan_off

But I haven’t done fans in a while like that. (Since the Tasmota Integration).