Template Fan Speed

Hello,

I have a three speed fan that I converted using 3 relays with ESPHome. (Link to a previous post about that for reference: ESP Home 3 Speed Fan Conversion - #3 by vhip6439)

I currently have it in Lovelace as 3 buttons, but that isn’t ideal and sucks to use with google assistant.

So! I am struggling with the fan speed template. Theres a lot of posts on the forum, but most of them are now out of date. I am trying to avoid making a script for each and every command, and trigger them on in the template instead. Can anyone provide guidance? Currently I get errors when I use the fan entity to toggle, and changing speeds does nothing.

fan:
  - platform: template
    fans:
      box_fan_one:
        value_template: "{{states('input_boolean.box_fan_one')}}"
        percentage_template: "{{states('input_number.box_fan_one_speed')}}"
        speed_count: 3
        turn_on:
          - service: input_select.select_option
            data:
              entity_id: input_select.box_fan_one_speed
              value: '66'
          - service: switch.turn_on
            entity_id: input_boolean.box_fan_one
        turn_off:
          - service: switch.turn_off
            entity_id: input_boolean.box_fan_one
        set_percentage:
          service: >
            {% if percentage == 100 %}
              service: switch.turn_on.switch.box_fan_one_high
            {% elif percentage == 67 %}
              service: switch.turn_on.switch.box_fan_one_medium
            {% elif percentage == 33 %}
              service: siwtch.turn_on.entity_id: switch.box_fan_one_low
            {% else %}
              service: switch.turn_off.entity_id: input_boolean.box_fan_one
            {% endif %}

I paired it with a number select.

Error using on toggle:
image

Error changing speed:

I’ve tried both switch.turn_on and homeassistant.turn_on

Thanks!

        set_percentage:
          service: "homeassistant.turn_{{ 'off' if percentage < 33 else 'on' }}"
          entity_id: >
            {% if percentage == 100 %}
              switch.box_fan_one_high
            {% elif percentage == 67 %}
              switch.box_fan_one_medium
            {% elif percentage == 33 %}
              switch.box_fan_one_low
            {% else %}
              input_boolean.box_fan_one
            {% endif %}

You cant use the switch.turn_on/off service for input booleans. You can turn anything on or off with the home assistant service though.

That makes sense. I’ve swapped the info for the Boolean. Have this error coming up though and I can’t quiet tell why. Im very new to the templating process. Thank you for your help. Updated code below:

image

fan:
  - platform: template
    fans:
      box_fan_one:
        value_template: "{{states('input_boolean.box_fan_one')}}"
        percentage_template: "{{states('input_number.box_fan_one_speed')}}"
        speed_count: 3
        turn_on:
          - service: input_select.select_option
            data:
              entity_id: input_select.box_fan_one_speed
              value: '66'
          - service: homeassistant.turn_on
            entity_id: input_boolean.box_fan_one
        turn_off:
          - service: homeassistant.turn_off
            entity_id: input_boolean.box_fan_one
        set_percentage:
          service: "homeassistant.turn_{{ 'off' if percentage < 33 else 'on' }}"
          entity_id: >
            {% if percentage == 100 %}
              switch.box_fan_one_high
            {% elif percentage == 67 %}
              switch.box_fan_one_medium
            {% elif percentage == 33 %}
              switch.box_fan_one_low
            {% else %}
              input_boolean.box_fan_one
            {% endif %}

I have the same question, so watching this topic.

Does my post here help at all?

I see what you did, and I may end up going the scripting route. However, with how simple this fan SHOULD be ( I would think ) I was hoping to have everything including the turn on and off functionality within the template rather than having outside scripts in play as well. Looking at other examples (some of which are broken due to changes) it seems like scripting is more common but it should be doable within the template itself.

Not a bad idea. Good luck and post back!

1 Like

The mqtt fan templates usually can get the code all together but any working fan templates for non mqtt Usually need scripting.

Try looking at my template for a fan in this post [HELP] New Template Fan integration - percentage - #12 by mwav3

Got the solution for Tasmota 3-channel relay. So i have:
3 switch from Tasmota integration:
switch.tasmotakitchen_fan1
switch.tasmotakitchen_fan2
switch.tasmotakitchen_fan3

I was make 1 helper (Group->SwitchGroup) for get the status (it’s like OR for any of them):
switch.vytiazhkastatus

1 script:

kitchenfan_off:
  alias: KitchenFan_Off
  sequence:
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
      - switch.tasmotakitchen_fan2
      - switch.tasmotakitchen_fan1
      - switch.tasmotakitchen_fan3
  mode: single
  icon: mdi:fan-off

and so i get the fan template:
From configuration.yaml:
fan: !include includes/fan.yaml

fan.yaml:

- platform: template
  fans:
    kitchen_fan:
      friendly_name: "kitchen fan"
      #Generated by: https://www.uuidgenerator.net/
      unique_id: e0091e56-87cf-11ee-b9d1-0242ac120002
      value_template: "{{ states('switch.vytiazhkastatus') }}"
      turn_on:
        - service: switch.turn_on
          target:
            entity_id: switch.tasmotakitchen_fan1
      turn_off:
        - service: script.turn_on
          target:
            entity_id: script.kitchenfan_off
      percentage_template: >
        {{ states('input_number.percentage') if is_state('input_boolean.state', 'on') else 0 }}
      speed_count: 3
      set_percentage:
          - service: switch.turn_{{ 'on' if percentage > 0 else 'off' }}
            target:
            #Switch from Tasmota low speed
              entity_id: switch.tasmotakitchen_fan1
          - service: switch.turn_{{ 'on' if percentage > 33 else 'off' }}
            target:
            #Switch from Tasmota Med speed
              entity_id: switch.tasmotakitchen_fan2
          - service: switch.turn_{{ 'on' if percentage > 66 else 'off' }}
            target:
            #Switch from Tasmota Hi speed
              entity_id: switch.tasmotakitchen_fan3
          - service: script.turn_{{ 'on' if percentage < 1 else 'off' }}
            target:
            #Switch from Tasmota Hi speed
              entity_id: script.kitchenfan_off

          - service: input_number.set_value
            target:
              entity_id: input_number.percentage
            data:
              value: "{{ percentage }}"

i changed few things thinking because i have a 4 relay sonoff my 1 = off however i have made a mess of it and i can control the speed from the new Fan template but the fan template doesnt update if i switch the tasmota switches

- platform: template
  fans:
    zwift_fan:
      friendly_name: "zwift fan"
      value_template: "{{ states('switch.zwift_fan_group') }}"
      turn_on:
        - service: switch.turn_on
          target:
            entity_id: switch.zwift_fan_low_speed
      turn_off:
        - service: switch.turn_on
          target:
            entity_id: switch.zwift_fan_off
      percentage_template: >
        {{ states('input_number.percentage') if is_state('input_boolean.state', 'on') else 0 }}
      speed_count: 3
      set_percentage:
          - service: switch.turn_{{ 'on' if percentage == 33 else 'off' }}
            target:
            #Switch from Tasmota low speed
              entity_id: switch.zwift_fan_low_speed
          - service: switch.turn_{{ 'on' if percentage == 66 else 'off' }}
            target:
            #Switch from Tasmota Med speed
              entity_id: switch.zwift_fan_medium_speed
          - service: switch.turn_{{ 'on' if percentage == 100 else 'off' }}
            target:
            #Switch from Tasmota Hi speed
              entity_id: switch.zwift_fan_high_speed
          - service: switch.turn_{{ 'on' if percentage == 0 else 'off' }}
            target:
            #Switch from Tasmota OFF
             entity_id: switch.zwift_fan_off

          - service: input_number.set_value
            target:
              entity_id: input_number.percentage
            data:
              value: "{{ percentage }}"