Skip to content

Instantly share code, notes, and snippets.

@adrianslabu
Created September 16, 2025 20:41
Show Gist options
  • Select an option

  • Save adrianslabu/d672b0462d56d8c99a38edc5bf50f7eb to your computer and use it in GitHub Desktop.

Select an option

Save adrianslabu/d672b0462d56d8c99a38edc5bf50f7eb to your computer and use it in GitHub Desktop.
02. Youtube - Turn ANY Device into a Smart Device with ESP8266
esphome:
name: dehumidifier
friendly_name: Dehumidifier
on_boot:
priority: -100
then:
- lambda: |-
if (id(state).state) {
id(button_power).press();
}
- script.execute: mode_default
esp8266:
board: d1_mini
globals:
- id: last_mode
type: std::string
initial_value: '"Default"' #Yes, you need single quotes + double quotes, it's not a misspelling
output:
- platform: gpio
id: power_pin
pin: D1
inverted: true
- platform: gpio
id: mode_pin
pin: D2
inverted: true
- platform: gpio
id: led_pin
pin: D3
inverted: true
#_________________________________________________________________________________# Buttons_start
button:
- platform: template
name: "Power"
id: button_power
on_press:
- logger.log: "Pressing Power button..."
- lambda: |-
if ((!id(state).state) && (id(mode_select).state != "Default")) {
id(mode_default).execute();
}
- delay: 300ms
- output.turn_on: power_pin
- delay: 100ms
- output.turn_off: power_pin
- delay: 500ms
- lambda: |-
if (id(state).state) {
ESP_LOGD("button_power", "Dehumidifer state is ON, turning OFF the leds...");
id(leds_off).execute();
} else {
ESP_LOGD("button_power", "Dehumidifer state is OFF, skipping leds...");
if (id(mode_select).state != "Default") {
ESP_LOGD("button_power", "Selecting default mode...");
id(mode_default).execute();
}
}
- platform: template
name: Leds
id: button_leds
on_press:
- logger.log: "Pressing Leds button..."
- output.turn_on: led_pin
- delay: 100ms
- output.turn_off: led_pin
#_________________________________________________________________________________# Buttons_end
#_________________________________________________________________________________# Selects_start
select:
- platform: template
name: "Mode"
id: mode_select
optimistic: true
options:
- "Default"
- "Night"
initial_option: "Default"
on_value:
then:
- lambda: |-
if (id(state).state) {
if (id(mode_select).state != id(last_mode)) {
id(last_mode) = id(mode_select).state;
if (id(mode_select).state == "Default") {
id(mode_nd).execute();
ESP_LOGI("mode_select", "Switched to Default mode");
} else if (id(mode_select).state == "Night") {
id(mode_dn).execute();
ESP_LOGI("mode_select", "Switched to Night mode");
}
}
}
# - lambda: |-
# if (id(state).state) {
# if (id(mode_select).state == "Default") {
# id(mode_nd).execute();
# ESP_LOGI("mode_select", "Switched to Default mode");
# } else if (id(mode_select).state == "Night") {
# id(mode_dn).execute();
# ESP_LOGI("mode_select", "Switched to Night mode");
# }
# }
#_________________________________________________________________________________# Selects_end
#_________________________________________________________________________________# Sensors_start
binary_sensor:
- platform: gpio
id: state
name: "State"
pin:
number: D5
mode: INPUT
device_class: power
filters:
- delayed_off: 100ms
#_________________________________________________________________________________# Sensors_end
#_________________________________________________________________________________# Scripts_start
script:
- id: leds_off
then:
- output.turn_on: led_pin
- delay: 100ms
- output.turn_off: led_pin
- delay: 300ms
- output.turn_on: led_pin
- delay: 100ms
- output.turn_off: led_pin
- id: mode_dn # From Default mode to Night mode
then:
- output.turn_on: mode_pin
- delay: 100ms
- output.turn_off: mode_pin
- id: mode_nd # From Night mode to Default mode
then:
- output.turn_on: mode_pin
- delay: 100ms
- output.turn_off: mode_pin
- delay: 300ms
- output.turn_on: mode_pin
- delay: 100ms
- output.turn_off: mode_pin
- id: mode_default
then:
- delay: 300ms
- select.set:
id: mode_select
option: "Default"
- globals.set:
id: last_mode
value: '"Default"' #Yes, you need single quotes '' + double quotes "", it's not a misspelling
#_________________________________________________________________________________# Scripts_end
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "1J7eq329j4RvW2ZPgdjX2n0aadaEH7FmL5kayZH6R/Y=" #this will be autogenerated by ESPHome Device Builder
ota:
- platform: esphome
password: "882c826788d116aaec0d8462fde5b1ce" #this will be autogenerated by ESPHome Device Builder
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Dehumidifier Fallback Hotspot" #this will be autogenerated by ESPHome Device Builder
password: "ByuGVFLSAIwf" #this will be autogenerated by ESPHome Device Builder
captive_portal:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment