Cycling Fan Speeds in Home Assistant
I have been putting physical buttons around the house to control lights and ceiling fans. I setup the buttons to turn the fan on/off with the last set speed. This is fine but I thought it would be nice to have the option to cycle through the fan speeds (like pulling the chain).
I wrote a script in Home Assistant that takes in the fan entity and cycles through 0, 33, 66, and 100 percentage speeds. It will check if the next speed is in the list and set the speed, if not it will reset to 0.
sequence:
- variables:
next_speed: |
{% set speeds = [0,33,66,100] %}
{% set current_speed = state_attr(entity, 'percentage') %}
{% set next_index = speeds.index(current_speed) + 1 %}
{{speeds[next_index if next_index <= (speeds|length - 1) else 0]}}
- action: fan.set_percentage
metadata: {}
data:
percentage: |
{{next_speed | int}}
target:
entity_id: "{{entity}}"
alias: Cycle Fan Speeds
description: "Cycle through fan speeds for a given entity."
fields: {}