"Smart" knob fan 4-Speed

Knob fan with 4 speeds that becomes “smart” with a Wemos D1 Mini and a 4-channel relay module using Home Assistant Fan Template Entity.

  1. The dumb FAN:

  2. The Wiring Diagram:

  3. The ESPHome code:

esphome:
  name: fan_speed_controller
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pass

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Fan Speed Controller"
    password: !secret ap_pass

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

web_server:
  port: 80

# Text sensors with general information.
text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: Study Smart Fan 1
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: Study Smart Fan IP
    ssid:
      name: Study Smart Fan SSID
    bssid:
      name: Study Smart Fan BSSID
      
# Sensors with general information.
sensor:
  # Uptime sensor.
  - platform: uptime
    name: Study Smart Fan Uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: Study Smart Fan WiFi Signal
    update_interval: 60s

time:
  - platform: sntp
    id: sntp_time
    timezone: America/Bogota
    servers: 192.168.111.1

switch:
  - platform: gpio
    pin: D0
    inverted: true
    name: "Speed 1"
    id: speed_one
    interlock: &interlock_group [speed_one, speed_two, speed_three, speed_four]
    interlock_wait_time: 500ms
  - platform: gpio
    pin: D5
    inverted: true
    name: "Speed 2"
    id: speed_two
    interlock: *interlock_group
    interlock_wait_time: 500ms
  - platform: gpio
    pin: D6
    inverted: true
    name: "Speed 3"
    id: speed_three
    interlock: *interlock_group
    interlock_wait_time: 500ms
  - platform: gpio
    pin: D7
    inverted: true
    name: "Speed 4"
    id: speed_four
    interlock: *interlock_group
    interlock_wait_time: 500ms
  1. The Home Assistant code:
script:
  study_fan_speed_action:
    alias: Study fan speed action
    sequence:
    - choose:
      - conditions:
        - condition: state
          entity_id: input_number.study_fan_speed_percentage
          state: '0.0'
        sequence:
        - choose:
          - conditions:
            - condition: state
              entity_id: switch.speed_1
              state: 'on'
            sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.speed_1
          - conditions:
            - condition: state
              entity_id: switch.speed_2
              state: 'on'
            sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.speed_2
          - conditions:
            - condition: state
              entity_id: switch.speed_3
              state: 'on'
            sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.speed_3
          - conditions:
            - condition: state
              entity_id: switch.speed_4
              state: 'on'
            sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.speed_4
          default: []
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.study_fan_state
      - conditions:
        - condition: state
          entity_id: input_number.study_fan_speed_percentage
          state: '25.0'
        sequence:
        - service: switch.turn_on
          target:
            entity_id: switch.speed_1
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.study_fan_state
      - conditions:
        - condition: state
          entity_id: input_number.study_fan_speed_percentage
          state: '50.0'
        sequence:
        - service: switch.turn_on
          target:
            entity_id: switch.speed_2
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.study_fan_state
      - conditions:
        - condition: state
          entity_id: input_number.study_fan_speed_percentage
          state: '75.0'
        sequence:
        - service: switch.turn_on
          target:
            entity_id: switch.speed_3
        - service: input_boolean.turn_on
      - conditions:
        - condition: state
          entity_id: input_number.study_fan_speed_percentage
          state: '100.0'
        sequence:
        - service: switch.turn_on
          target:
            entity_id: switch.speed_4
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.study_fan_state
      default: []
    mode: single
  study_fan_speed_set:
    alias: Study fan speed set
    sequence:
    - service: input_number.set_value
      target:
        entity_id: input_number.study_fan_speed_percentage
      data:
        value: '{{ percentage }}'
    - service: script.study_fan_speed_action
    mode: single

fan:
  - platform: template
    fans:
      study_fan:
        friendly_name: "Ventilador Estudio"
        value_template: "{{ states('input_boolean.study_fan_state') }}"
        percentage_template: "{{ states('input_number.study_fan_speed_percentage') }}"
        turn_on:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ 25 }}"
        turn_off:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ 0 }}"
        set_percentage:
          service: script.study_fan_speed_set
          data:
            percentage: "{{ percentage }}"
        speed_count: 4

All improvements and corrections are welcome.

4 Likes

I love it! It’s always exciting when someone takes an otherwise dumb appliance and smartens it up. That’s why I love tinkering with Arduinos. Nice job!

1 Like