Hi friends:
I’m trying to create a template fan for HomeKit based on an existing fan, the purpose is to expose oscillation as direction control (more user friendly in Homekit). The below is the template I created:
- platform: template
fans:
livingroom_homekit:
friendly_name: "Living Room Fan Homekit"
unique_id: livingroom_homekit
# 1. On/off detection
value_template: "{{ is_state('fan.zhimi_de_317588644_za4_s_2_fan', 'on') }}"
# 2. Turn on/off actions
turn_on:
- action: fan.turn_on
target:
entity_id: fan.zhimi_de_317588644_za4_s_2_fan
turn_off:
- action: fan.turn_off
target:
entity_id: fan.zhimi_de_317588644_za4_s_2_fan
# 3. Speed control
percentage_template: "{{ state_attr('fan.zhimi_de_317588644_za4_s_2_fan', 'percentage') }}"
set_percentage:
- action: fan.set_percentage
target:
entity_id: fan.zhimi_de_317588644_za4_s_2_fan
data:
percentage: "{{ percentage }}"
# 4. Preset modes support
preset_modes:
- "Direct Breeze"
- "Natural Wind"
preset_mode_template: "{{ state_attr('fan.zhimi_de_317588644_za4_s_2_fan', 'preset_mode') }}"
set_preset_mode:
- action: fan.set_preset_mode
target:
entity_id: fan.zhimi_de_317588644_za4_s_2_fan
data:
preset_mode: "{{ preset_mode }}"
# 5. Oscillation (native support)
oscillating_template: "{{ state_attr('fan.zhimi_de_317588644_za4_s_2_fan', 'oscillating') }}"
set_oscillating:
- action: fan.oscillate
target:
entity_id: fan.zhimi_de_317588644_za4_s_2_fan
data:
oscillating: "{{ oscillating }}"
# 6. Use "direction" control in HomeKit to also toggle oscillation
direction_template: >
{% if state_attr('fan.zhimi_de_317588644_za4_s_2_fan', 'oscillating') %}
forward
{% else %}
reverse
{% endif %}
set_direction:
- action: fan.oscillate
target:
entity_id: fan.zhimi_de_317588644_za4_s_2_fan
data:
oscillating: "{{ direction == 'forward' }}"
So the problem I have is with speed control which accepts percentage from user. In Homekit when I adjust speed, it sends multiple values, and I have to deal with the race condition (an example) below:
- HomeKit send command 20
- Template fan instruct the real fan to change speed to 20
- HomeKit send command 50
- The real fan now has changed speed to 20
- The percentage_template returns 20, and the value 50 is ignored and no further command sent to the real fan.
This works fine if I expose the fan directly to HomeKit, however with a template in-between it creates the race condition as the example above. I’d like to ask how to handle such race condition? Many thanks in advance!