The solution I have is probably not that much different than your existing one. Based on my observations, the Template Fan sends a power-on
command before each speed-change and that won’t work here because your fan doesn’t have separate power-on
and power-off
commands (it has a single power-toggle
command).
The solution I’m offering requires:
- Three inputs.
- Two automations.
- One python_script.
Inputs
There are three inputs:
- control fan’s power state
- control fan’s speed
- store fan’s last-known speed
input_boolean.fan_power
controls the fan’s power state. It appears as a toggle button in the Lovelace UI.
input_boolean:
fan_power:
name: Fan Power
input_select.fan_speed
controls the fan’s speed. It appears as a drop-down list in the Lovelace UI.
input_select:
fan_speed:
name: Fan Speed
options: ['1', '2', '3', '4', '5', '6', '7', '8']
input_text.tmp_fan_speed
maintains a record of the fan’s last-known speed. There’s no need to show this in the UI.
input_text:
tmp_fan_speed:
name: Tmp Fan Speed
Automations
There are two automations:
- manage the fan’s power state
- manage the fan’s speed
automation.fan_power_control
is triggered by changes to input_boolean.fan_power
. Set the IP address to the one used by your Broadlink device and set packet
to the RF code used by your fan for turning it on/off.
- alias: 'fan power control'
trigger:
platform: state
entity_id: input_boolean.fan_power
action:
- service: broadlink.send
data:
host: '192.168.1.10'
packet: 'JgBQAAABJZMSExE3EhMRExE4ETgRExERfODAAAAAAAAAA='
automation.fan_speed_control
is triggered by changes to input_select.fan_speed
. It will run only if input_boolean.fan_power
is on. In other words, you must turn the fan on, using input_boolean.fan_power
, before adjusting its speed with input_select.fan_speed
.
- alias: 'fan speed control'
trigger:
platform: state
entity_id: input_select.fan_speed
condition:
condition: state
entity_id: input_boolean.fan_power
state: 'on'
action:
- service: python_script.fan_speed_control
data_template:
speed: "{{ trigger.to_state.state }}"
- service: input_text.set_value
data_template:
entity_id: input_text.tmp_fan_speed
value: "{{ trigger.to_state.state }}"
Python_script
There is one python_script to calculate and transmit the correct number of speed-steps to reach a desired fan speed.
fan_speed_control.py
is used by automation.fan_speed_control
. It requires one parameter:
fan_speed
is the desired fan speed from 1 to 8.
You must customize this python_script to work with your devices. Enter a valid RF code for increasing the fan’s speed. In addition, provide the IP address of your Broadlink device.
Be sure to read about the python_script integration so you know what to add to your configuration file and where to put this file:
fan_speed_control.py
# Set code to whatever RF code your fan uses for increasing speed.
code = 'JgBQAAISEjcSEhISEhISExETETgRNhMTETgROBAAAAAAAAAA='
speed = data.get('fan_speed')
tmp_speed = hass.states.get('input_text.tmp_fan_speed')
if speed is not None:
speed = int(speed)
last_speed = int(tmp_speed.state) if tmp_speed.state else 1
if 1 <= speed <= 8:
if speed > last_speed:
loop = speed - last_speed
else:
loop = (8 - last_speed) + speed
# Set the IP address to match the one used by your Broadlink device
service_data = {'host':'192.168.1.10', 'packet':'{}'.format(code)}
for i in range(loop):
hass.services.call('broadlink', 'send', service_data, False)
time.sleep(0.5)
else:
logger.warning('<fan_speed_control> Received fan speed is invalid ({})'.format(fan_speed))
else:
logger.warning('<fan_speed_control> Received fan speed is invalid (None)')
Before attempting to control the fan with this solution, you will need to synchonize it. Using the fan’s remote-control, turn it on, set it to its lowest speed and then turn it off. This is the solution’s initial state (fan_power is off
and fan_speed is 1
). Now you can begin testing the solution by turning the fan on/off with input_boolean.fan_power
. If that works, proceed to controlling its speed with input_select.fan_speed
.