I am trying to make a simple routine in ESPHome for my ESP32 boards, so that from the moment the ESPHome device starts, it begins fading a LED up and down continuously (0% to 100% in 5 sec, then 100% to 0% in 5 sec followed by 0% to 100% in 5 sec etc. etc.).
The LED is visible from outside the box, indicating that the unit is alive (not only powered on). (Fading is more elegant than simply on/off blinking ) .
I want this to run locally in my ESPHome box (if possible nothing extra transmitted to HA ( no extra lamps, buttons, messages etc in HA because of this routine)), and also if possible, I dont want this routine to write a lot of stuff to memory (that shortens the life of the memory)…
I tried the following routine but it does not start fading, ( unfortunately it adds a button and a slider in HA so i can adjust the intensity manually, but as i stated, i want an automatic, continuous fade up and down without manual intervention via HA )…
This is my code.:
output:
- platform: ledc
pin: GPIO2
id: pwm_output
frequency: 1000Hz
light:
- platform: monochromatic
output: pwm_output
name: "PWM Light"
id: pwm_light
effects:
- lambda:
name: Fade Effect
update_interval: 20ms
lambda: |-
static int state = 0;
static int brightness = 0;
if (state == 0) {
brightness += 5;
if (brightness >= 255) {
state = 1;
}
} else {
brightness -= 5;
if (brightness <= 0) {
state = 0;
}
}
auto call = id(pwm_light).turn_on();
call.set_brightness(float(brightness) / 255.0);
call.perform();