Template Fan configuration

Hi,

I’m working on a Template Fan. This is the config:

fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "Bedroom fan"
        value_template: "{{ states('input_boolean.fan_state') }}"
        speed_template: "{{ states('input_select.fan_speed') }}"
        turn_on:
          service: script.sovrum_takflakt_med
        turn_off:
          service: script.sovrum_takflakt_off
        set_speed:
          service_template: script.sovrum_takflakt_speed
          data_template:
            speed: "{{ speed }}"
        speeds:
          - 'Min'
          - 'Med'
          - 'Max'

The problem I am having is how to get hold of the speed value in the actual script:

sovrum_takflakt_speed:
  alias: Takfläkt (hastighet)
  sequence:
    - service_template: >-
        {% if speed == 'Min' %}
          script.sovrum_takflakt_min
        {% elif speed == 'Med' %}
          script.sovrum_takflakt_med
        {% elif speed == 'Max' %}
          script.sovrum_takflakt_max
        {% endif %}

This is what I got, and it is not working. Any help would be much appreciated :slight_smile:

try changing it to this:

fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "Bedroom fan"
        value_template: "{{ states('input_boolean.fan_state') }}"
        speed_template: "{{ states('input_select.fan_speed') }}"
        turn_on:
          service: script.sovrum_takflakt_med
        turn_off:
          service: script.sovrum_takflakt_off
        set_speed:
          service: script.turn_on
          data_template:
            entity_id: script.sovrum_takflakt_speed
            speed: "{{ speed }}"
        speeds:
          - 'Min'
          - 'Med'
          - 'Max'

I tried this but it didn’t make any difference. But thanks :slight_smile:

I got it working! :smile:
I had got this completely wrong, mostly due to poor documentation.

Here is what works:

In configuration.yaml:

# Keep track of the on/off state
input_boolean:
  bedroom_fan_state:

# Keep track of the current speed
input_select:
  bedroom_fan_speed:
    options:
      - 'Min'
      - 'Med'
      - 'Max'

fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "Bedroom Fan"
        value_template: "{{ states('input_boolean.bedroom_fan_state') }}"
        speed_template: "{{ states('input_select.bedroom_fan_speed') }}"
        turn_on:
          service: script.bedroom_fan_on
        turn_off:
          service: script.bedroom_fan_off
        set_speed:
          service: script.bedroom_fan_set_speed
          data_template:
            speed: "{{ speed }}"
        speeds:
          - 'Min'
          - 'Med'
          - 'Max'

The set_speed script (script.bedroom_fan_set_speed) is used to change the speed and nothing else. After the speed has been set, it automatically calls the turn_on script (this took a great deal of trouble shooting to understand). The delay between the calls to the scripts set_speed and turn_on was to small so I had to put a delay in the turn_on script.

In scripts.yaml:

bedroom_fan_off:
  alias: Takfläkt (Av)
  sequence:
    - service: switch.broadlink_send_packet_10_1_2_3
      data:
        packet: 'JgA0ACsMKw0PKA8pDygPKQ8oKw0PKA8oDykPAAEGKw0qDQ8pDygPKA8pDygrDQ8oDykPKA8ADQUAAAAA'
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.bedroom_fan_state

bedroom_fan_on:
  alias: Takfläkt (På)
  sequence:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.bedroom_fan_state
    - delay:
        seconds: 1
    - service_template: >
        {% if is_state("input_select.bedroom_fan_speed", "Min") %}
          script.bedroom_fan_min
        {% elif is_state("input_select.bedroom_fan_speed", "Med") %}
          script.bedroom_fan_med
        {% elif is_state("input_select.bedroom_fan_speed", "Max") %}
          script.bedroom_fan_max
        {% endif %}

bedroom_fan_min:
  alias: Takfläkt (Min)
  sequence:
    - service: switch.broadlink_send_packet_10_1_2_3
      data:
        packet: 'JgAyACoNKw0OKQ8pDikPKA8pDygPKQ8oDykq6ysMKw0PKA8pDygPKQ8oDykOKQ8pDikrAA0FAAAAAAAA'

bedroom_fan_med:
  alias: Takfläkt (Med)
  sequence:
    - service: switch.broadlink_send_packet_10_1_2_3
      data:
        packet: 'JgA0ACoOKQ4OKQ4qDikOKg0qDioNKioODSoPAAEGKw0qDQ8pDygPKQ4pDykOKQ8oKw0PKA8ADQUAAAAA'

bedroom_fan_max:
  alias: Takfläkt (Max)
  sequence:
    - service: switch.broadlink_send_packet_10_1_2_3
      data:
        packet: 'JgAyACsNKg0PKQ8oDykqDQ8pDikPKA8pKg0r6yoNKw0OKQ8oDykqDQ8pDygPKQ8oKw0qAA0FAAAAAAAA'

bedroom_fan_set_speed:
  alias: Takfläkt (hastighet)
  sequence:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.bedroom_fan_speed
        option: "{{ speed }}"

Now I have this working. Changing the speed at the bottom (Min) instantly changes the fan speed.
2018-07-21%2019_54_14-Home%20Assistant

4 Likes

Im trying to create my fan control through Broadlink rmpro similar like you. can you help me with your configuration code/ lovelace code/ script code please

How do I do a simple

fan:

  • platform: switch
    name: Living Room Ceiling Fan
    entity_id: switch.wemo_switch_lr_ceiling_fan

Like I can do with lights?

I just need my on / off switch to be a fan.

Never mind, I think I figured it out.

fan:
  - platform: template
    fans:
      dining_room_fan:
        friendly_name: "Living Room Fan"
        value_template: "{{ states('switch.wemo_switch_lr_ceiling_fan') }}"
        turn_on:
          service: homeassistant.turn_on
          entity_id: switch.wemo_switch_lr_ceiling_fan
        turn_off:
          service: homeassistant.turn_off
          entity_id: switch.wemo_switch_lr_ceiling_fan

Are you getting reliable speed settings with that code? I copied your scripts line by line (thank you!) but I was getting random speeds being selected until I added the following:

  master_fan_set_speed:
    alias: Master Fan Set Speed
    sequence:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.master_fan_set_speed
        option: '{{speed}}'
    - service: script.turn_on
      data_template:
        entity_id: >-
          {% if speed | string == "1"  %}
            script.bedroom_fan_1
          {% elif speed | string == "2" %}
            script.bedroom_fan_2
          {% elif speed | string == "3" %}
            script.bedroom_fan_3
          {% endif %}
1 Like

You can simplify that entity_id section

  master_fan_set_speed:
    alias: Master Fan Set Speed
    sequence:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.master_fan_set_speed
        option: '{{speed}}'
    - service: script.turn_on
      data_template:
        entity_id: script.bedroom_fan_{{ speed }}
2 Likes

Always the guru @petro, thank you! I’ll give that a try shortly.

sir

I cant get this to work. Any ideas?
Thanks

  • platform: template
    fans:
    bedroom4:
    friendly_name: “Bedroom 4 Ceiling Fan”
    value_template: “{{ states(‘input_boolean.bedroom4_fan_state’) }}”
    turn_on:
    service: script.rm4_ceiling_fan_on
    turn_off:
    service: script.rm4_ceiling_fan_off

it can start but cannot off. the toggle switch return back to off but fan is not off, when i click it again it start again but never off. lastly, when i toggle back quickly, it will off the fan.

It would be great if the documentation was updated with this info. It took me hours to sort this one out. @petro helped me the find the missing key!

Here is what I ended up with, I have tried to turn it in a generic setup, change names accordingly:

Fan component:

fan:
  - platform: template
    fans:
      your_template_fan_name:
        friendly_name: "Your Template Fan Friendly Name"
        value_template: "{{ states('input_boolean.template_fan_state') }}"
        speed_template: "{{ states('input_select.template_fan_speed') }}"
        turn_on:
          service: script.template_fan_on
        turn_off:
          service: script.template_fan_off
        set_speed:
          service: script.template_fan_speed
          data:
            speed: "{{ speed }}"
        # the following values are default, change to your liking:
        speeds:
          - 'low'
          - 'medium'
          - 'high'

Scripts:

template_fan_off:
  alias: Template Fan Off
  sequence:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.template_fan_state
        
template_fan_on:
  alias: Template Fan On
  sequence:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.template_fan_state

template_fan_low:
  alias: Template Fan Low
  sequence:
  - service: switch.turn_off
    data:
      entity_id:
        - switch.template_fan_mid
        - switch.template_fan_high
  - service: switch.turn_on
    data:
      entity_id:
        - switch.template_fan_low
        
template_fan_medium:
  alias: Template Fan Medium
  sequence:
  - service: switch.turn_off
    data:
      entity_id:
        - switch.template_fan_low
        - switch.template_fan_high
  - service: switch.turn_on
    data:
      entity_id:
        - switch.template_fan_medium
      
template_fan_high:
  alias: Template Fan High
  sequence:
  - service: switch.turn_off
    data:
      entity_id:
        - switch.template_fan_low
        - switch.template_fan_medium
  - service: switch.turn_on
    data:
      entity_id:
        - switch.template_fan_high
      
template_fan_speed:
  alias: Template Fan Speed
  sequence:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.template_fan_speed
        option: "{{ speed }}"
    - service: script.turn_on
      data_template:
        entity_id: script.template_fan_{{ speed }}

This example uses three switches to control the speed, merely as an example. Change to your setup.
Let me know if there is an error in this template and I will correct it.
Thanks to everyone in this thread for helping me out!

2 Likes

What kind of switches do you have that require you to turn them off when turning on the other speeds? Do the other switches report a state? Reason I’m asking is because this can be greatly simplified if your switches report on/off states. If they report states, you can reduce this to 1 template fan and 1 script, 0 input helpers.

The fan is equipped with two switches. The fan is always on. So it always runs at least on low speed.

If switch 1 is ‘on’ the fan runs at medium speed.
If switch 2 is ‘on’ the fan runs at high speed.

These two switches have to be interlocked, only one of the two can be active.
(I am not sure but I might damage the fan if I switch both on, don’t feel like trying it out :slight_smile:)

Indeed the switches return their ‘state’.

I am actually going to add a third switch to be able to switch the fan off completely.
I want to replace:

input_boolean.template_fan_state

with:

switch.template_fan_master

After that I think I will be able to control all features of the fan.

So these switches, how are you integrating them? You don’t really need an off switch, you just need to think this through with templates.

What’s happening when you turn on the fan? Your template fan only toggles a boolean. I"m assuming there’s an automation in place? If so, what is the automation doing?

I now replaced the input_boolean with a switch

So basically In the above fan template I changed:

input_boolean.template_fan_state

to:

switch.template_fan_on_off

And I changed the on and off script accordingly:

template_fan_off:
  alias: Template Fan Off
  sequence:
    - service: switch.turn_off
      data:
        entity_id: switch.template_fan_on_off
        
template_fan_on:
  alias: Template Fan On
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.template_fan_on_off

I’d love to know if I could simplify this! :smiley:

Basically it need to do the following:

On/off - switch.template_fan_on_off ON or OFF
Low Speed - switch.template_fan_medium OFF and switch.template_fan_high OFF
Medium Speed - switch.template_fan_medium ON and switch.template_fan_high OFF
High Speed - switch.template_fan_medium OFF and switch.template_fan_high ON

Well, how are you making your switches?

They are actual switches: “Shelly”

shelly.cloud

Even for the fan_on_off?