Thought I would write a guide as i’ve personally struggled for 2 days to figure this out. Somehow the template fan integration page made no mention of requiring additional entities to keep track of status or how to connect the attributes to the script.
I have an IR controlled fan that I wanted to control using a broadlink mini. The Fan switches on by default to speed 1. Also, it has an oscillating function.
Instead of switches, i created 3 input entities to keep track of its status. (ps: I tried an input_boolean with the oscillate but it didn’t work, so left it as input select)
# Keep track of the fan state
input_boolean:
bedroom_fan_state:
# Keep track of the fan oscillate
input_select:
bedroom_fan_osc:
options:
- "True"
- "False"
# Keep track of fan speed
input_number:
bedroom_fan_percentage:
name: Bedroom Fan Percentage
min: 0
max: 100
Put in the template fan format
fan:
- platform: template
fans:
bedroom_fan:
friendly_name: "Bedroom Fan"
unique_id: bedroom_fan
value_template: "{{ states('input_boolean.bedroom_fan_state') }}"
percentage_template: "{{ states('input_number.bedroom_fan_percentage') }}"
oscillating_template: "{{ states('input_select.bedroom_fan_osc') }}"
turn_on:
service: script.bedroom_fan_on
turn_off:
service: script.bedroom_fan_off
set_oscillating:
service: script.fan_oscillating
data:
oscillating: "{{ oscillating }}"
set_percentage:
service: script.bedroom_fan_set_speed
data:
percentage: "{{ percentage }}"
speed_count: 3
Prepare the scripts
POWER-ON
- This sends the power on command
- Updates the boolean state
- Updates fan percentage entity to 33% (Speed 1)
bedroom_fan_on:
alias: bedroom_fan_on
sequence:
- service: remote.send_command
target:
device_id: [YOUR BROADLINK DEVICE]
data:
command: 'b64: JgBoAAABJpIVEBMSExISExI3EzcSExITEjcUNhM3EzYUERMSEzcTNhQRExITNxI3FBETEhITEjgTNhQ2FBETEhI3FDYTNxMSEgAFDwABJkoTAAxAAAElShUADD4AASdJEwAMQAABJ0kTAA0F'
- service: input_boolean.turn_on
target:
entity_id: input_boolean.bedroom_fan_state
- service: input_number.set_value
target:
entity_id: input_number.bedroom_fan_percentage
data:
value: 33
mode: single
POWER-OFF
- This sends the power off command, sets fan state boolean to off, sets the speed to 0, sets oscillate to False
bedroom_fan_off:
alias: bedroom_fan_off
sequence:
- service: remote.send_command
target:
device_id: [YOUR BROADLINK DEVICE]
data:
command: 'b64: JgBoAAABJpIVEBMSExISExI3EzcSExITEjcUNhM3EzYUERMSEzcTNhQRExITNxI3FBETEhITEjgTNhQ2FBETEhI3FDYTNxMSEgAFDwABJkoTAAxAAAElShUADD4AASdJEwAMQAABJ0kTAA0F'
- service: input_boolean.turn_off
target:
entity_id: input_boolean.bedroom_fan_state
- service: input_number.set_value
target:
entity_id: input_number.bedroom_fan_percentage
data:
value: 0
- service: input_select.select_option
target:
entity_id: input_select.bedroom_fan_osc
data:
option: 'False'
mode: single
Fan speed script
- This script receives the percentage value, updates it in the input_number entity and directs it to another script that sends the actual command. You can add the actual command in this script but i chose to separate them for ease of modification if i get a different fan.
bedroom_fan_set_speed:
alias: bedroom_fan_set_speed
sequence:
- service: input_number.set_value
target:
entity_id: input_number.bedroom_fan_percentage
data:
value: '{{ percentage }}'
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 500
- choose:
- conditions:
- condition: state
entity_id: input_number.bedroom_fan_percentage
state: '66.0'
sequence:
- service: script.bedroom_fan_inc
- conditions:
- condition: state
entity_id: input_number.bedroom_fan_percentage
state: '100.0'
sequence:
- service: script.bedroom_fan_inc
- conditions:
- condition: state
entity_id: input_number.bedroom_fan_percentage
state: '33.0'
sequence:
- service: script.bedroom_fan_inc
default: []
mode: single
Fan speed command script
- I have one of those array fans so the commands are sent to each fan 1/2s apart
- You can of course customize this to however you like them
bedroom_fan_inc:
sequence:
- service: remote.send_command
data:
command: b64:JgBYAAABJ5EUERQQFRAVEBU0FTUVEBUQFDUVNRU0FTUVEBUQFDUVNRU0FTUVNRQRFBAVEBUQFTQVEBUQFRAVNBU1FTUUNRUQFQAFCAABK0gUAAw7AAEoSBUADQU
target:
device_id: [YOUR BROADLINK DEVICE]
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 500
- service: remote.send_command
data:
command: b64:JgBYAAABJ5EUERQQFRAVEBU1FDMXEBUQFDUVNRU1FDUVEBUQFDUVNRU1FBEUEBU1FBEUERQQFTUUERQ1FTUUERQ1FTUUNRUQFQAFCwABJ0gUAAw7AAEnSBUADQU=
target:
device_id: [YOUR BROADLINK DEVICE]
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 500
- service: remote.send_command
data:
command: b64:JgBYAAABJ5AVEBUQFRAUERQ1FTUUEBUQFTUUNRU1FDUVEBUQFTQVNRUQFDUVNRUQFBAVEBUQFTUUNRUQFRAUNRU1FDUVNRQRFAAFCgABKEgUAAw6AAEoSBQADQU
target:
device_id: [YOUR BROADLINK DEVICE]
mode: single
alias: bedroom_fan_inc