Skip to content

Instantly share code, notes, and snippets.

@aceat64
Created October 22, 2021 18:50
Show Gist options
  • Save aceat64/0e59dff4e04f69034d4d961f5dc778b4 to your computer and use it in GitHub Desktop.
Save aceat64/0e59dff4e04f69034d4d961f5dc778b4 to your computer and use it in GitHub Desktop.
esphome light switch
substitutions:
device_name: dining-room-light
friendly_name: Dining Room Light
pwm_min_power: 15% # keep dimming functional at lowest levels
no_delay: 0s # transition when changing dimmer_lvl & relay delay
transition_length: .5s # transition when turning on/off
long_press_min: .4s # minimum time to activate long-press action
long_press_max: 2s # maximum time to activate long-press action
long_press_up: 100% # long press brightness
long_press_down: 33% # long press brightness
long_press_main: 50% # long press brightness
esphome:
name: ${device_name}
platform: ESP8266
board: esp01_1m
esp8266_restore_from_flash: true
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: ${device_name}
password: !secret fallback_password
captive_portal:
# Enable logging
logger:
# Make sure logging is not using the serial port
baud_rate: 0
# Enable Home Assistant API
api:
ota:
output:
- platform: gpio
pin: GPIO3
inverted: true
id: led5
- platform: gpio
pin: GPIO5
inverted: true
id: led4
- platform: gpio
pin: GPIO12
inverted: true
id: led3
- platform: gpio
pin: GPIO14
inverted: true
id: led2
- platform: esp8266_pwm
pin: GPIO13
id: pwm
power_supply: relay
min_power: ${pwm_min_power}
light:
- platform: monochromatic
name: ${friendly_name}
output: pwm
default_transition_length: ${no_delay}
id: dimmer
status_led:
pin:
number: GPIO4
inverted: true
power_supply:
- id: relay
pin:
number: GPIO16
inverted: True
enable_time: ${no_delay}
keep_on_time: ${no_delay}
binary_sensor:
- platform: gpio
pin:
number: GPIO0
inverted: True
mode: INPUT_PULLUP
name: ${friendly_name} Up Button
id: up_button
internal: True
on_press:
then:
- lambda: |-
if (id(dimmer_lvl) > .91) {
id(dimmer_lvl) = 1.0;
}
else if (id(dimmer_lvl) <= .91) {
id(dimmer_lvl) += .083;
};
id(apply_dimming).execute();
on_click:
min_length: ${long_press_min}
max_length: ${long_press_max}
then:
- light.turn_on:
id: dimmer
brightness: ${long_press_up}
- platform: gpio
pin:
number: GPIO1
inverted: True
mode: INPUT_PULLUP
name: ${friendly_name} Down Button
internal: True
on_press:
then:
- lambda: !lambda |-
if (id(dimmer_lvl) < .10) {
id(dimmer_lvl) = .01;
}
else if (id(dimmer_lvl) >= .10) {
id(dimmer_lvl) -= .083;
};
id(apply_dimming).execute();
on_click:
min_length: ${long_press_min}
max_length: ${long_press_max}
then:
- light.turn_on:
id: dimmer
brightness: ${long_press_down}
- platform: gpio
pin:
number: GPIO15
mode: INPUT_PULLUP
name: ${friendly_name} Main Button
internal: True
on_press:
- light.toggle: dimmer
on_click:
min_length: ${long_press_min}
max_length: ${long_press_max}
then:
- light.turn_on:
id: dimmer
brightness: ${long_press_main}
globals:
- id: dimmer_lvl
type: float
restore_value: no
initial_value: '1.0'
script:
- id: apply_dimming
then:
- lambda: |-
auto call = id(dimmer).turn_on();
call.set_brightness(id(dimmer_lvl));
call.perform();
- logger.log:
format: "dimmer_lvl = %.2f"
args: ['id(dimmer_lvl)']
interval:
- interval: 250ms
then:
- lambda: |-
auto dimmer_vals = id(dimmer).current_values;
if (dimmer_vals.is_on()) {
id(dimmer_lvl) = dimmer_vals.get_brightness();
}
if (id(dimmer_lvl) > .19) { id(led2).turn_on(); }
if (id(dimmer_lvl) < .20) { id(led2).turn_off(); }
if (id(dimmer_lvl) > .39) { id(led3).turn_on(); }
if (id(dimmer_lvl) < .40) { id(led3).turn_off(); }
if (id(dimmer_lvl) > .59) { id(led4).turn_on(); }
if (id(dimmer_lvl) < .60) { id(led4).turn_off(); }
if (id(dimmer_lvl) > .79) { id(led5).turn_on(); }
if (id(dimmer_lvl) < .80) { id(led5).turn_off(); }
if (!dimmer_vals.is_on()) {
id(led2).turn_off();
id(led3).turn_off();
id(led4).turn_off();
id(led5).turn_off();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment