[Script] Cycle fan speed

Hey all, off the bat, apologies if this has been posted before (it was fairly trivial, I’d be surprised it if hasn’t, although I wasn’t able to find anything when searching).

This is a simple script that can be called to increase or decrease the speed of a fan, simulating the traditional physical pull-chain. Since this is a script, you can choose how you trigger it from an automation (click a Zigbee or Z-Wave button, trigger by presence, sync with a light, whatever you want!)

You can use two different modes of stepping by toggling the “Start on High” option during setup:

  • off → high → medium → low (common on most pull chains)
  • off → low → medium → high (my personal preference)

While most fans are either on-off (no need for this script then) or three-step motors, this script makes no assumptions about step intervals and can be used on fans with any number of steps.

Blueprint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: "Cycle fan speed"
  description: >-
    A script that increments or decrements the speed of a fan.
  domain: script
  source_url: https://github.com/nwithan8/configs/blob/02229006680ccb20c654a745b907c768e9fee9d7/home_assistant/blueprints/scripts/cycle_fan_speed.yaml
  input:
    fan:
      name: "Fan"
      description: "The fan entity to change the speed of"
      selector:
        entity:
          domain:
            - fan
            - group
          multiple: false
    start_high:
      name: "Start on high"
      description: "Start with the highest speed from off (off -> 3 -> 2 -> 1 instead of off -> 1 -> 2 -> 3)"
      selector:
        boolean:

# Only single, otherwise could flood with attempts
mode: single

fields: {}

variables:
  fan: !input "fan"
  start_high: !input "start_high"

sequence:
  - variables:
      current_speed: '{{ state_attr(fan, "percentage") }}'

  - choose:
      - conditions:
          - alias: "Fan is off"
            condition: template
            value_template: "{{ current_speed <= 0 }}"
        sequence:
          - if:
              - alias: "Should start on highest speed"
                condition: template
                value_template: "{{ start_high }}"
            then:
              - service: fan.set_percentage
                data:
                  percentage: 100
                target:
                  entity_id: "{{ fan }}"
            else:
              - service: fan.increase_speed
                target:
                  entity_id: "{{ fan }}"
      - conditions:
          - alias: "Fan is at max speed"
            condition: template
            value_template: "{{ current_speed >= 100 }}"
        sequence:
          - if:
              - alias: "Started on highest speed"
                condition: template
                value_template: "{{ start_high }}"
            then:
              - service: fan.decrease_speed
                target:
                  entity_id: "{{ fan }}"
            else:
              - service: fan.set_percentage
                data:
                  percentage: 0
                target:
                  entity_id: "{{ fan }}"
    default:
      - if:
          - alias: "Started on highest speed"
            condition: template
            value_template: "{{ start_high }}"
        then:
          - service: fan.decrease_speed
            target:
              entity_id: "{{ fan }}"
        else:
          - service: fan.increase_speed
            target:
              entity_id: "{{ fan }}"


Install

  1. Click the “Import Blueprint” button above and follow the on-screen instructions to import the blueprint into your Home Assistant.
  2. Create a new script using the blueprint (Settings → Automations & Scenes → Scripts tab → “Add Script” → Select “Cycle fan speed” from the list)
  3. Fan: Select the fan or fan group entity you wish to control
  4. Start on high: Determine the fan speed order:
    • On: Off → High → Medium → Low
    • Off: Off → Low → Medium → High

Usage

Call the script manually or from an automation. No need to keep track of the fan speed in a separate entity or helper.

Release History:

1.0.0 (May 24, 2024):

  • Initial Release
2 Likes