Skip to content

Instantly share code, notes, and snippets.

@ryanralph
Created July 7, 2025 12:10
Show Gist options
  • Select an option

  • Save ryanralph/d77c45ab71a5f36806d0d3f197a48bdf to your computer and use it in GitHub Desktop.

Select an option

Save ryanralph/d77c45ab71a5f36806d0d3f197a48bdf to your computer and use it in GitHub Desktop.
airextractionfan.yaml
substitutions:
devicename: "airextractionfan"
upper_devicename: "Air Extraction Fan"
esphome:
name: $devicename
friendly_name: $upper_devicename
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: ""
ota:
- platform: esphome
password: !secret api_password
wifi:
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
- ssid: !secret iot_wifi_ssid
password: !secret iot_wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "$upper_devicename fallback AP"
password: !secret api_password
captive_portal:
output:
- platform: gpio
pin: 5
id: fan_button
globals:
- id: fan_state
type: int
restore_value: no
initial_value: '0'
- id: last_state
type: int
restore_value: no
initial_value: '0'
- id: int_fan_speed
type: int
restore_value: no
initial_value: '0'
script:
- id: sync_fan_state
mode: restart
then:
- lambda: |-
int last_known_state = id(last_state);
int target_state = id(int_fan_speed);
int presses_needed = (target_state - last_known_state + 4) % 4;
ESP_LOGD("fan", "Syncing fan state: last=%d target=%d presses=%d", last_known_state, target_state, presses_needed);
id(last_state) = target_state;
if(presses_needed == 1) {
id(pulse_one).execute();
} else if(presses_needed == 2) {
id(pulse_two).execute();
} else if(presses_needed == 3) {
id(pulse_three).execute();
} else {
//Error
}
- id: pulse_one
then:
- output.turn_on: fan_button
- delay: 100ms
- output.turn_off: fan_button
- id: pulse_two
then:
- output.turn_on: fan_button
- delay: 100ms
- output.turn_off: fan_button
- delay: 150ms
- output.turn_on: fan_button
- delay: 100ms
- output.turn_off: fan_button
- id: pulse_three
then:
- output.turn_on: fan_button
- delay: 100ms
- output.turn_off: fan_button
- delay: 150ms
- output.turn_on: fan_button
- delay: 100ms
- output.turn_off: fan_button
- delay: 150ms
- output.turn_on: fan_button
- delay: 100ms
- output.turn_off: fan_button
fan:
- platform: template
name: "Fan"
id: extraction_fan
speed_count: 3 # 1=Low, 2=Med, 3=High
on_turn_on:
- if:
condition:
lambda: 'return id(fan_state) == 0;'
then:
- lambda: |-
id(fan_state) = 1;
id(int_fan_speed) = id(extraction_fan).speed;
- script.execute: sync_fan_state
on_speed_set:
- lambda: |-
id(int_fan_speed) = id(extraction_fan).speed;
- script.execute: sync_fan_state
on_turn_off:
- lambda: |-
id(int_fan_speed) = 0;
id(fan_state) = 0;
- script.execute: sync_fan_state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment