Hi all,
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 ?
My current ESPHome code:
substitutions:
device_name: shelly
esphome:
name: ${device_name}
platform: ESP8266
board: esp01_1m
logger:
level: DEBUG
api:
ota:
web_server:
port: 80
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
reboot_timeout: 5min
ap:
ssid: "${device_name} FB"
password: !secret Fallback_Hotspot
# Optional manual IP
manual_ip:
static_ip: 192.168.1.181
gateway: 192.168.1.1
subnet: 255.255.255.0
time:
- platform: sntp
id: sntp_time
sensor:
- platform: wifi_signal
name: "${device_name} - WiFi Signal"
update_interval: 60s
- platform: uptime
name: "${device_name} - Uptime"
icon: mdi:clock-outline
update_interval: 60s
- platform: uptime
name: "${device_name} - Uptime Seconds"
id: uptime_sensor
update_interval: 60s
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(uptime_sensor).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? String(days) + "d " : "") +
(hours ? String(hours) + "h " : "") +
(minutes ? String(minutes) + "m " : "") +
(String(seconds) + "s")
).c_str();
- platform: adc
pin: A0
name: "${device_name} - adc"
update_interval: 1s
accuracy_decimals: 4
id: current_raw
internal: true
- platform: template
name: "${device_name} - Power"
unit_of_measurement: W
accuracy_decimals: 1
device_class: power
update_interval: 10s
id: my_power
lambda: return id(current_raw).state;
filters:
- calibrate_linear:
- 0.3701 -> 0.0
- 0.37891 -> 0.8687
- 0.4169 -> 1.7375
- multiply: 24 # 24V lamp
- platform: total_daily_energy
name: "${device_name} - Total Daily Energy"
power_id: my_power
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
unit_of_measurement: kW
light:
- platform: monochromatic
name: ${device_name}_R
id: ${device_name}_R
output: ${device_name}_out_R
restore_mode: RESTORE_DEFAULT_OFF
- platform: monochromatic
name: ${device_name}_G
id: ${device_name}_G
output: ${device_name}_out_G
restore_mode: RESTORE_DEFAULT_OFF
- platform: monochromatic
name: ${device_name}_B
id: ${device_name}_B
output: ${device_name}_out_B
restore_mode: RESTORE_DEFAULT_OFF
- platform: monochromatic
name: ${device_name}_W
id: ${device_name}_W
output: ${device_name}_out_W
restore_mode: RESTORE_DEFAULT_ON
output:
- platform: esp8266_pwm
id: ${device_name}_out_R
pin: GPIO12
frequency: 1000 Hz
- platform: esp8266_pwm
id: ${device_name}_out_G
pin: GPIO15
frequency: 1000 Hz
- platform: esp8266_pwm
id: ${device_name}_out_B
pin: GPIO14
frequency: 1000 Hz
- platform: esp8266_pwm
id: ${device_name}_out_W
pin: GPIO4
frequency: 1000 Hz
text_sensor:
- platform: template
name: "${device_name} - Uptime"
id: uptime_human
icon: mdi:clock-start
update_interval: 60s
- platform: version
name: "${device_name} - ESPHome Version"
- platform: wifi_info
ip_address:
name: "${device_name} - IP"
switch:
- platform: restart
name: "${device_name} - Restart"
binary_sensor:
- platform: gpio
name: '${device_name}_switch'
pin:
number: GPIO5
ChatGPT’s suggested implementation to control some lights ramping up/down brightness between 3 intervals:
esphome:
name: my_lights
platform: ESP8266
board: nodemcu
logger:
level: DEBUG
wifi:
ssid: "your_wifi_ssid"
password: "your_wifi_password"
api:
web_server:
light:
- platform: template
name: "Light A"
id: light_a
lambda: |-
auto now = time(nullptr);
int total_minutes = now.tm_hour * 60 + now.tm_min;
float brightness = 0;
int start_minutes_1 = 10 * 60;
int end_minutes_1 = 15 * 60;
int start_minutes_2 = 15 * 60;
int end_minutes_2 = 19 * 60;
int start_minutes_3 = 19 * 60;
int end_minutes_3 = 22 * 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 = 50.0;
float max_brightness_2 = 80.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 = total_minutes - start_minutes_2 + duration_1;
brightness = (brightness / duration_2) * (max_brightness_2 - max_brightness_1) + max_brightness_1;
} 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;
I tried putting the lambda in so many places, but keep getting yaml indentation errors.
Would really appreciate some help.
Thanks!