I have recently got a Shelly RGBW2 to control my fish tank’s RGBW led strip.
I want it to ramp up the brightness and dim during the course of the day - I’ve done something like that in arduino ages ago, however ESPHome/yaml is new to me.
Not sure how reliable it is, but I got the scheduling bit of code from ChatGPT and it seems to make sense. What i’m struggling now is integrating my current code with the scheduling code. Any chance someone could give me some pointers ?
Uh’oh…thanks for pointing this out. If I put ChatGPT aside, is there a way to achieve it using native functions ? E.g. Between 10:00 and 15:00 increase brightness from 0% to 80%, keep it at 80% for a few hours, then gradually fade out between, say, 20:00-22:00. I currently have HA automations for smth similar, however I don’t like that it’s network dependent.
Thanks again Mahko_mahko for your help, but I’m a little stuck now. I have managed to get the brightness value to behave as I want it, however I am struggling to feed that value as brightness of a light.
What confuses me is the on_action bit. I feel like on_value would make more sense there, however it’s forcing me to use optimistic: true, which does not allow me to use lambdas. I tried getting the brightness value from the scheduling lambda to get written into a global variable (to be used with on_state with return {global_brightness = brightness; } , but it wouldn’t compile.
Any pointers in how to get that return brightness value transferred into the brightness of my led ?
esphome:
name: esp-test
esp32:
board: esp32dev
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: 192.168.1.169
gateway: 192.168.1.1
subnet: 255.255.255.0
dns1: 192.168.1.1
dns2: 192.168.1.2
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp-Test Fallback Hotspot"
password: "ZsDsw6T6JnAp"
captive_portal:
web_server:
port: 80
time:
- platform: sntp
id: sntp_time
timezone: Europe/London
output:
- platform: ledc
id: led_output
pin: GPIO16
light:
- platform: monochromatic
name: LED
id: led
output: led_output
default_transition_length: 0s
number:
- platform: template
id: test_brightness
icon: mdi:target-variant
name: "Brightness"
update_interval: 1s
step: 1
min_value: 0
max_value: 100
set_action:
then:
- light.turn_on:
id: led
brightness: !lambda |-
return x / 100.0;
lambda: |-
auto time_now = id(sntp_time).now();
int total_minutes = time_now.second *60; //Comment this out after testing. It uses seconds so you can see changes quicker.
//int total_minutes = time_now.hour * 60 + time_now.minute;
float brightness = 0;
int start_minutes_1 = 0 * 60;
int end_minutes_1 = 10 * 60;
int start_minutes_2 = 10 * 60;
int end_minutes_2 = 30 * 60;
int start_minutes_3 = 30 * 60;
int end_minutes_3 = 60 * 60;
int duration_1 = end_minutes_1 - start_minutes_1;
int duration_2 = end_minutes_2 - start_minutes_2;
int duration_3 = end_minutes_3 - start_minutes_3;
float max_brightness_1 = 100.0;
float max_brightness_2 = 100.0;
if (total_minutes >= start_minutes_1 && total_minutes < end_minutes_1) {
brightness = total_minutes - start_minutes_1;
brightness = brightness / duration_1 * max_brightness_1;
} else if (total_minutes >= start_minutes_2 && total_minutes < end_minutes_2) {
brightness = max_brightness_2;
} else if (total_minutes >= start_minutes_3 && total_minutes < end_minutes_3) {
brightness = total_minutes - start_minutes_3 + duration_1 + duration_2;
brightness = (duration_1 + duration_2 + duration_3 - brightness) / duration_3 * max_brightness_2;
}
return brightness;
##### This bit is my guesswork. Doesn't compile###
- platform: template
id: bright
max_value: 100
min_value: 0
step: 1
optimistic: True
on_value:
then:
- light.turn_on:
id: led
brightness: !lambda |-
return id(number.brightness).state / 100.0;
#######
text_sensor:
- platform: template
name: "Current time"
lambda: |-
char str[17];
time_t currTime = id(sntp_time).now().timestamp;
strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
return { str };
update_interval: 30s
Thanks, I got there in the end. And managed to also incorporate a physical switch that lets me choose whether I want it to run a schedule or just keep the light up at 100%. Issue with the 100% and my current code is that I can’t control it from HA anymore, as it bounces back to 100% due to on_value.