more esphome devices and fixes

This commit is contained in:
root
2026-04-03 21:34:23 +13:00
parent e09931360a
commit f7bfa78835
96 changed files with 4870 additions and 2523 deletions
+189 -165
View File
@@ -1,72 +1,97 @@
##########################################################################################
##########################################################################################
# Title: LOUNGE BOOKSHELF LEDS
# Repo: https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-loungebookshelfleds.yaml
#
# v2.0 - 2025-09-15 A bunch of fixes and made it more standard across my devices
# v1.0 - 2025-08-17 First setup (and replacement of Tasmota)
#:########################################################################################:#
# TITLE: LOUNGE BOOKSHELF LEDS
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-loungebookshelfleds.yaml
#:########################################################################################:#
# VERSIONS:
# V2.1 2026-03-11 Updated yaml to zorruno layout V1.1
# V2.0 2025-09-15 A bunch of fixes and made it more standard across my devices
# V1.0 2025-08-17 First setup (and replacement of Tasmota)
#:########################################################################################:#
# HARDWARE:
# - Magichome LED controller single channel (ESP8266, NOT the later ones with BL602)
# - Reference:
# https://devices.esphome.io/devices/MagicHome-ZJ-WFMN-C-Single-Color
# - Device GPIO (Magichome ESP8266):
# - GPIO2 Blue LED (used for ESPHome status)
# - GPIO12 MOSFET output (0 V when switched)
#:########################################################################################:#
# OPERATION NOTES:
# - General-purpose LED controller.
# - Designed for the Sinilink XY-WFMS board with a MOSFET output (claimed 5A, 5-36 V DC),
# but will suit similar MOSFET boards.
# - In SUBSTITUTIONS there is a maximum output setting to extend LED life.
# - In SUBSTITUTIONS there is a minimum output setting; switches fully OFF at/below the minimum
# to avoid low-PWM flicker.
# - PWM frequency is set to 1 kHz by default. You can increase it, but higher values caused
# resets on the Sinilink device. On ESP32 you can run much higher.
# - Min/max output settings are not exposed in Home Assistant/MQTT by default, but could be.
# - Packages include common items: network settings, diagnostic entities, MQTT, and optional SNTP.
# - Default restart behaviours:
# - Fade up to full
# - Restore Brightness
# - Remain Off
# - The green LED flashes while fading (different patterns for up/down). The red LED follows the
# output (it shares the MOSFET GPIO). This depends on hardware.
# - Fade timing scales with configured values proportionally when starting mid-brightness.
# - Exposed in Home Assistant/MQTT:
# - Startup action (On, Off, Restore)
# - Fade Up / Fade Down / Fade Stop buttons
# - Fade Up/Down toggle switch
# - Normal On/Off switch using quick ramp up/down
# - Fade up/down times (0-60 s)
# - Output % (pre-gamma) and PWM % (post-gamma)
# - Output Set (1-100, respects min/max)
# - Many device diagnostics from the included diagnostics package
# - Maximum ON time before automatic fade-down (1-48 h, 0 = no limit)
#
# ------------------------------------------
# OPERATION (as of v2.0)
# ------------------------------------------
# 1. General-purpose LED controller.
# 2. Designed for the Sinilink XY-WFMS board with a MOSFET output (claimed 5A, 5-36 V DC), but will suit similar mosfet boards.
# 3. (In SUBSTITUTIONS) setting for MAX % output to extend LED life (ie can ensure it is only ever 95%)
# 4. (In SUBSTITUTIONS) Minimum output setting; switches fully OFF at/below the minimum to avoid low-PWM flicker.
# 5. (In SUBSTITUTIONS) PWM frequency is set to 1kHz by default. You can increase it, but higher values caused
# resets on Sinilink device. On ESP32 you can run much higher (~40 kHz).
# 6. Min/Max output settings are not exposed in Home Assistant/MQTT by default, but could be.
# With a 1 MB flash, space is tight and only minimal optimisation has been done so far.
# 7. (PACKAGES) include common items: network settings, diagnostic entities, MQTT, and SNTP (optional).
# 8. Default behaviours, for recovery from lost power or reset:
# - "Fade up to full": fade from floor to max on boot.
# - "Restore Brightness": fade from floor to the last non-zero brightness on boot (should be no on/off blip/flash).
# - "Remain Off": stays off on boot.
# 9. The green LED flashes while fading (different patterns for up/down). The red LED follows the
# output (it shares the MOSFET GPIO). (Obviously this depends on hardware)
# 10. Fade timing scales with the configured values (proportionally when starting mid-brightness).
# 11. Exposed in Home Assistant/MQTT:
# - Startup action (On,Off,Restore)
# - Fade Up / Fade Down / Fade Stop buttons
# - Fade Up/Down toggle switch (keeps fading up to full, or down to off unless stopped)
# - Normal On/Off switch (quick ramp up/down, up to the level of previous brightness)
# - Fade up/down times (0-60s)
# - Output % (pre-gamma) and PWM % (post-gamma)
# - Output Set (1-100, respects min/max). This also changes with other output so reflects value.
# - Many device diagnostics (from the included 'diagnostics' package)
# - Maximum 'on' time before automatic fade-down (1-48 h, 0 = no limit)
##########################################################################################
##########################################################################################
# Hardware: Magishome LED controller single channel (ESP8266, NOT the later ones with BL602)
# https://devices.esphome.io/devices/MagicHome-ZJ-WFMN-C-Single-Color
# ------------------------------------------
# DEVICE GPIO (Magichome ESP8266)
# ------------------------------------------
# GPIO02 Blue LED (used for ESPHome status) - I think... can't see the board
# GPIO12 MOSFET output (0 V when switched)
##########################################################################################
##########################################################################################
# MQTT COMMANDS:
# - ${mqtt_local_command_topic}/light/set = ON
# - ${mqtt_local_command_topic}/light/set = OFF
# - State publishes to:
# - ${mqtt_local_status_topic}/light/state
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant offline (network and MQTT online):
# - LED control continues locally on the device
# - Direct local MQTT commands still work if the broker is available
# - Stored brightness/ramp settings and restart behaviour continue to work
#
# b) MQTT offline (or HA/MQTT offline):
# - LED control continues locally on the device
# - Physical button control and local ramp logic continue to work
# - No MQTT control/state publishing available
#
# c) Entire WiFi/Network offline:
# - LED control continues locally on the device
# - Physical button control and local ramp logic continue to work
# - Accurate time is not needed; SNTP is not required for operation
#:########################################################################################:#
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
#:########################################################################################:#
# SUBSTITUTIONS:
# Specific device variable substitutions
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
#:########################################################################################:#
substitutions:
# Device Naming
device_name: "esp-loungebookshelfleds" # yaml file should be device_name.yaml
friendly_name: "Lounge Bookshelf LEDs"
description_comment: "LED Strip around the bookshelf in the upstairs lounge :: Magichome ESP8266"
description_comment: "LED Strip around the bookshelf in the upstairs lounge :: Magichome ESP8266 (Layout V1.1)"
device_area: "Lounge" # Allows the ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Magichome Single Colour V1.1 LED Controller.Generic ESP8266" # Project details. Manufacturer before the dot, device after the dot.
project_version: "v2.0" # Project version denotes release of the YAML file, allowing checking of deployed vs latest version
project_name: "Magichome Single Colour V1.1 LED Controller.Generic ESP8266" # Project details. Manufacturer before the dot, device after the dot.
project_version: "v2.1" # Project version denotes release of the YAML file, allowing checking of deployed vs latest version
# Passwords & Secrets (Unfortunately, you can't use substitutions inside secret names)
api_key: !secret esp-api_key # If using HA API
api_key: !secret esp-api_key # If using HA API
ota_pass: !secret esp-ota_pass # If passwording OTA updates
static_ip_address: !secret esp-loungebookshelfleds_ip # CHANGE THIS
current_ip_address: ${static_ip_address}
# MQTT LOCAL Controls (This is just my local method of controlling via MQTT, I also use the built in method)
mqtt_local_device_name: "lounge-bookshelfleds"
mqtt_local_command_main_topic: !secret mqtt_command_main_topic
@@ -77,24 +102,25 @@ substitutions:
mqtt_local_device_command_OFF: "OFF"
# Device Specific Settings
log_level: "NONE" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (default), VERBOSE, VERY_VERBOSE
update_interval: "20s" # Update time for general sensors, etc.
led_gamma: "1.2" # Gamma from 1.2-3 is sensible to normalise the LED fading vs PWM
minimum_led_output: "1" # % If at this value or below, we'll switch it completely off
maximum_led_output: "90" # % Maximum output; it is sometimes nice to limit the output for longevity or aesthetics
log_level: "NONE" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (default), VERBOSE, VERY_VERBOSE
update_interval: "20s" # Update time for general sensors, etc.
led_gamma: "1.2" # Gamma from 1.2-3 is sensible to normalise the LED fading vs PWM
minimum_led_output: "1" # % If at this value or below, we will switch it completely off
maximum_led_output: "90" # % Maximum output; it is sometimes nice to limit the output for longevity or aesthetics
max_on_default_hours: "24" # The maximum time the LEDs will be on, in case they get left on. 0 = no automatic turn-off
pwm_frequency: "1000" # PWM output Frequency in Hz. High enough to avoid audible/visible artifacts
pwm_frequency: "1000" # PWM output Frequency in Hz. High enough to avoid audible/visible artifacts
# Device Specific GPIO (so we can easily update for other devices)
device_status_led: GPIO02
device_status_led: GPIO2
device_mosfet_out: GPIO12
device_usr_button: GPIO04 # if no button, this is just a fake/safeish choice so it compiles with the generic yaml
device_usr_button: GPIO4 # if no button, this is just a fake/safeish choice so it compiles with the generic yaml
device_fading_led: GPIO13 # if no LED, this is just a fake/safeish choice so it compiles with the generic yaml
##########################################################################################
# PACKAGES: Included Common Packages
#:########################################################################################:#
# PACKAGES:
# Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
#:########################################################################################:#
packages:
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
common_wifi: !include
@@ -102,6 +128,7 @@ packages:
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_current_ip_address: "${current_ip_address}"
local_ota_pass: "${ota_pass}"
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
@@ -116,22 +143,23 @@ packages:
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
#### WEB PORTAL ####
#common_webportal: !include common/webportal_common.yaml
#### SNTP (Only use if you want/need accurate timeclocks) ####
#common_sntp: !include common/sntp_common.yaml
#common_sntp: !include common/sntp_common.yaml
#### DIAGNOSTICS Sensors ####
diag_basic: !include common/include_basic_diag_sensors.yaml
diag_more: !include common/include_more_diag_sensors.yaml
diag_debug: !include common/include_debug_diag_sensors.yaml
#diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
diag_basic: !include common/include_basic_diag_sensors.yaml
diag_more: !include common/include_more_diag_sensors.yaml
diag_debug: !include common/include_debug_diag_sensors.yaml
#diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
##########################################################################################
# ESPHome CORE CONFIGURATION
#:########################################################################################:#
# ESPHOME CORE CONFIGURATION
# https://esphome.io/components/esphome.html
##########################################################################################
#:########################################################################################:#
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
@@ -152,32 +180,32 @@ esphome:
# Mode 0: Fade up to full (obeys fade settings; no on/off flicker)
- if:
condition:
lambda: 'return id(restart_mode) == 0;'
lambda: "return id(restart_mode) == 0;"
then:
- lambda: 'id(ramp_switch_target_on) = true;'
- lambda: "id(ramp_switch_target_on) = true;"
- script.execute: ramp_on_script
# Mode 1: Restore Brightness (deferred to avoid races)
- if:
condition:
lambda: 'return id(restart_mode) == 1;'
lambda: "return id(restart_mode) == 1;"
then:
- script.execute: deferred_restore_brightness
# Mode 2: Remain Off (no blip; stays off)
- if:
condition:
lambda: 'return id(restart_mode) == 2;'
lambda: "return id(restart_mode) == 2;"
then:
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- lambda: 'id(ramp_switch_target_on) = false;'
- lambda: "id(ramp_switch_target_on) = false;"
- light.turn_off:
id: mosfet_leds
transition_length: 0s
# Boot complete: allow OFF handlers again
- lambda: 'id(booting) = false;'
- lambda: "id(booting) = false;"
# NEW: force indicator OFF after boot logic
# (Removed: output.turn_off: green_led_out)
@@ -193,11 +221,11 @@ esphome:
# - -fdata-sections
# - -DNDEBUG
##########################################################################################
#:########################################################################################:#
# ESP PLATFORM AND FRAMEWORK
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
##########################################################################################
#:########################################################################################:#
esp8266:
board: esp01_1m
restore_from_flash: true # restore some values on reboot
@@ -208,10 +236,10 @@ mdns:
preferences:
flash_write_interval: 2min # enough time to update values for reboots, but not enough to wear flash
##########################################################################################
#:########################################################################################:#
# GLOBAL VARIABLES
# https://esphome.io/components/globals.html
##########################################################################################
#:########################################################################################:#
globals:
# Minimum Brightness % for LEDs (will switch off if <=)
- id: min_brightness_pct
@@ -223,7 +251,7 @@ globals:
type: int
restore_value: false
initial_value: "${maximum_led_output}" # hard cap; never exceed this
# The maximum time the lights will stay on, in hours. Just in case they are left on. 0 = forever
# The maximum time the lights will stay on, in hours. Just in case they are left on. 0 = forever
- id: max_on_hours
type: int
restore_value: true
@@ -232,23 +260,23 @@ globals:
- id: led_gamma_f
type: float
restore_value: false
initial_value: ${led_gamma} # <-- numeric at compile time
initial_value: ${led_gamma} # numeric at compile time
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
type: int
restore_value: true
initial_value: '5000' # 5 s
initial_value: "5000" # 5 s
# Default Fading Down Time (Selectable and will be retained)
- id: ramp_down_ms # fade-out when turned OFF
type: int
restore_value: true
initial_value: '10000' # 10 s
initial_value: "10000" # 10 s
# Action on Restart. (0=Fade full, 1=Restore brightness, 2=Remain off)
- id: restart_mode
- id: restart_mode
type: int
restore_value: true
initial_value: '0' # default = Fade Up to Full (so can be deployed with no other setup)
initial_value: "0" # default = Fade Up to Full (so can be deployed with no other setup)
# Determine last fade direction.
# true when you asked the light to end up ON (Ramp Up)
@@ -256,70 +284,70 @@ globals:
- id: ramp_switch_target_on
type: bool
restore_value: true
initial_value: 'false'
initial_value: "false"
# Prevent jitter when adjusting the slider
- id: suppress_slider_sync
type: bool
restore_value: false
initial_value: 'false'
# actual 0..100 seen last time, for restart
- id: last_brightness_pct
initial_value: "false"
# actual 0..100 seen last time, for restart
- id: last_brightness_pct
type: float
restore_value: true
initial_value: '0.0'
initial_value: "0.0"
# last published "Output Set (0-100)" integer
- id: last_set_pos
type: int
restore_value: false
initial_value: '-1'
# helper to keep blink time == transition time
initial_value: "-1"
# helper to keep blink time == transition time
- id: last_ramp_ms
type: int
restore_value: false
initial_value: '0'
initial_value: "0"
# target for "Restore Brightness" scripted ramp
- id: restore_target_pct
type: float
restore_value: false
initial_value: '0.0'
initial_value: "0.0"
# true while a scripted ramp is in progress (to limit flash writes)
- id: is_ramping
type: bool
restore_value: false
initial_value: 'false'
initial_value: "false"
# guards OFF persistence during startup caused by restore_mode: ALWAYS_OFF
- id: booting
type: bool
restore_value: false
initial_value: 'true'
initial_value: "true"
# true only while we are performing a ramp-down that briefly turns the light ON to hit the floor
- id: ramping_for_off
type: bool
restore_value: false
initial_value: 'false'
initial_value: "false"
##########################################################################################
#:########################################################################################:#
# LOGGER COMPONENT
# https://esphome.io/components/logger.html
# Logs all log messages through the serial port and through MQTT topics.
##########################################################################################
#:########################################################################################:#
logger:
level: "${log_level}" # INFO Level suggested, or DEBUG for testing
baud_rate: 0 # set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM, Serial control)
#########################################################################################
#:########################################################################################:#
# STATUS LED
# https://esphome.io/components/status_led.html
#########################################################################################
#:########################################################################################:#
status_led:
pin:
number: ${device_status_led}
inverted: true
##########################################################################################
#:########################################################################################:#
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
#:########################################################################################:#
mqtt:
on_message:
# Light control to ramp up
@@ -333,10 +361,10 @@ mqtt:
then:
- switch.turn_off: mosfet_ramp_switch
##########################################################################################
#:########################################################################################:#
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
#:########################################################################################:#
switch:
# Ramp-aware ON/OFF for HA (asymmetric, eased; no bounce)
- platform: template
@@ -356,10 +384,10 @@ switch:
- script.stop: ramp_on_script
- script.execute: ramp_off_script
#################################################################################################
#:########################################################################################:#
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
#:########################################################################################:#
button:
# Start ramping UP (from current level)
- platform: template
@@ -399,7 +427,7 @@ button:
id(ramping_for_off) = false;
id(is_ramping) = false;
// Freeze the light at its *current* level by overwriting the transition
// Freeze the light at its current level by overwriting the transition
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on()) {
auto call = id(mosfet_leds).make_call();
@@ -415,12 +443,10 @@ button:
id(last_brightness_pct) = pct;
}
#########################################################################################
#:########################################################################################:#
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#########################################################################################
#:########################################################################################:#
select:
- platform: template
id: restart_action
@@ -442,10 +468,10 @@ select:
id(restart_mode) = 2;
}
#########################################################################################
#:########################################################################################:#
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#########################################################################################
#:########################################################################################:#
binary_sensor:
- platform: gpio
id: btn_gpio12
@@ -462,7 +488,7 @@ binary_sensor:
on_press:
- if:
condition:
lambda: 'return id(ramp_switch_target_on);'
lambda: "return id(ramp_switch_target_on);"
then:
# Target is currently ON -> press should go OFF (start ramp-down)
- lambda: |-
@@ -478,10 +504,10 @@ binary_sensor:
- script.stop: ramp_off_script
- script.execute: ramp_on_script
##########################################################################################
#:########################################################################################:#
# SENSOR COMPONENT
# https://esphome.io/components/sensor/
##########################################################################################
#:########################################################################################:#
sensor:
- platform: template
id: mosfet_output_pct
@@ -489,7 +515,7 @@ sensor:
unit_of_measurement: "%"
icon: mdi:percent
accuracy_decimals: 0
update_interval: 2s # consider 200ms if you want fewer updates
update_interval: 2s
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
return cv.is_on() ? (float) (int)(cv.get_brightness() * 100.0f + 0.5f) : 0.0f;
@@ -530,28 +556,29 @@ sensor:
if (pwm > 1.0f) pwm = 1.0f;
return pwm * 100.0f;
##########################################################################################
#:########################################################################################:#
# OUTPUT COMPONENT
# https://esphome.io/components/light/index.html
##########################################################################################
#:########################################################################################:#
# An OUTPUT can be binary (0,1) or float, which is any value between 0 and 1.
# PWM Outputs such as "ledc" are float. https://esphome.io/components/output/ledc.html
##########################################################################################
#:########################################################################################:#
output:
- platform: esp8266_pwm
id: mosfet_pwm
pin: ${device_mosfet_out}
frequency: ${pwm_frequency}
- platform: gpio
id: green_led_out # Green LED
pin:
number: ${device_fading_led}
inverted: false
##########################################################################################
#:########################################################################################:#
# LIGHT COMPONENT
# https://esphome.io/components/light/
##########################################################################################
#:########################################################################################:#
light:
- platform: monochromatic
id: mosfet_leds
@@ -570,14 +597,14 @@ light:
payload: "${mqtt_local_device_command_ON}"
retain: true
- lambda: |-
// Do not flip "intent to ON" when we're inside an OFF ramp's floor-kick
// Do not flip "intent to ON" when we are inside an OFF ramp's floor-kick
if (!id(ramping_for_off)) {
id(ramp_switch_target_on) = true;
}
- script.stop: max_on_watchdog
- if:
condition:
lambda: 'return id(max_on_hours) > 0;'
lambda: "return id(max_on_hours) > 0;"
then:
- script.execute: max_on_watchdog
@@ -587,13 +614,13 @@ light:
- output.turn_off: green_led_out
- if:
condition:
lambda: 'return !id(booting);'
lambda: "return !id(booting);"
then:
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_OFF}"
retain: true
- lambda: 'id(ramp_switch_target_on) = false;'
- lambda: "id(ramp_switch_target_on) = false;"
- script.stop: max_on_watchdog
- lambda: |-
id(last_brightness_pct) = 0.0f;
@@ -614,11 +641,10 @@ light:
call.set_transition_length(0);
call.perform();
##########################################################################################
#:########################################################################################:#
# NUMBER COMPONENT
# https://esphome.io/components/number/
##########################################################################################
#:########################################################################################:#
number:
- platform: template
id: cfg_ramp_up_s
@@ -683,10 +709,10 @@ number:
set_action:
- if:
condition:
lambda: 'return x <= 0.0f;'
lambda: "return x <= 0.0f;"
then:
# 0 means OFF
- lambda: 'id(suppress_slider_sync) = true;'
- lambda: "id(suppress_slider_sync) = true;"
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- light.turn_off:
@@ -698,7 +724,7 @@ number:
id(last_brightness_pct) = 0.0f; // persist OFF immediately
id(last_set_pos) = 0;
- delay: 400ms
- lambda: 'id(suppress_slider_sync) = false;'
- lambda: "id(suppress_slider_sync) = false;"
else:
# Map 1..100 - [min..max] and set ON
- lambda: |-
@@ -730,7 +756,7 @@ number:
if (out_pct > maxp) out_pct = maxp;
id(last_brightness_pct) = out_pct; // persist exact target now
- delay: 400ms
- lambda: 'id(suppress_slider_sync) = false;'
- lambda: "id(suppress_slider_sync) = false;"
- platform: template
id: cfg_max_on_hours
@@ -753,27 +779,26 @@ number:
id(cfg_max_on_hours).publish_state((float) hrs);
- if:
condition:
lambda: 'return id(mosfet_leds).current_values.is_on();'
lambda: "return id(mosfet_leds).current_values.is_on();"
then:
- script.stop: max_on_watchdog
- if:
condition:
lambda: 'return id(max_on_hours) > 0;'
lambda: "return id(max_on_hours) > 0;"
then:
- script.execute: max_on_watchdog
##########################################################################################
#:########################################################################################:#
# SCRIPT COMPONENT
# https://esphome.io/components/script.html
# Scripts can be executed nearly anywhere in your device configuration with a single call.
##########################################################################################
#:########################################################################################:#
script:
# Script: ramp up from current level. Obey global max. (LED ON while ramping)
# Script: ramp up from current level. Obey global max. (LED ON while ramping)
- id: ramp_on_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;'
- lambda: "id(is_ramping) = true;"
- script.stop: ramp_off_script
# Ensure we start at at least the floor without a visible "pop".
- if:
@@ -785,12 +810,12 @@ script:
then:
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
brightness: !lambda "return id(min_brightness_pct) / 100.0f;"
transition_length: 80ms
# Ramp from current (>= floor) to cap over a fraction of ramp_up_ms.
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(max_brightness_pct) / 100.0f;'
brightness: !lambda "return id(max_brightness_pct) / 100.0f;"
transition_length: !lambda |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
@@ -810,7 +835,7 @@ script:
if (frac > 1.0f) frac = 1.0f;
id(last_ramp_ms) = (int) (id(ramp_up_ms) * frac);
return (uint32_t) id(last_ramp_ms);
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- delay: !lambda "return (uint32_t) id(last_ramp_ms);"
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -823,7 +848,7 @@ script:
- id: ramp_to_target_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;'
- lambda: "id(is_ramping) = true;"
- script.stop: ramp_off_script
# Ensure we start at the floor cleanly.
- if:
@@ -835,7 +860,7 @@ script:
then:
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
brightness: !lambda "return id(min_brightness_pct) / 100.0f;"
transition_length: 80ms
# Ramp from current (>= floor) to restore_target_pct over a fraction of ramp_up_ms.
- light.turn_on:
@@ -866,7 +891,7 @@ script:
if (frac > 1.0f) frac = 1.0f;
id(last_ramp_ms) = (int) (id(ramp_up_ms) * frac);
return (uint32_t) id(last_ramp_ms);
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- delay: !lambda "return (uint32_t) id(last_ramp_ms);"
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -875,7 +900,6 @@ script:
id(last_brightness_pct) = pct; // persist final level
}
# Script: ramp down from current level to floor, then cleanly cut to OFF. (LED ON while ramping)
- id: ramp_off_script
mode: restart
@@ -886,7 +910,7 @@ script:
- script.stop: ramp_on_script
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
brightness: !lambda "return id(min_brightness_pct) / 100.0f;"
transition_length: !lambda |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
@@ -899,7 +923,7 @@ script:
if (frac > 1.0f) frac = 1.0f;
id(last_ramp_ms) = (int) (id(ramp_down_ms) * frac);
return (uint32_t) id(last_ramp_ms);
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- delay: !lambda "return (uint32_t) id(last_ramp_ms);"
- light.turn_off:
id: mosfet_leds
transition_length: 150ms
@@ -916,12 +940,12 @@ script:
then:
- if:
condition:
lambda: 'return id(max_on_hours) > 0;'
lambda: "return id(max_on_hours) > 0;"
then:
- delay: !lambda 'return (uint32_t) (id(max_on_hours) * 3600000UL);'
- delay: !lambda "return (uint32_t) (id(max_on_hours) * 3600000UL);"
- if:
condition:
lambda: 'return id(mosfet_leds).current_values.is_on();'
lambda: "return id(mosfet_leds).current_values.is_on();"
then:
- lambda: |-
id(ramp_switch_target_on) = false;
@@ -948,18 +972,18 @@ script:
- if:
condition:
lambda: 'return id(restore_target_pct) > 0.5f;'
lambda: "return id(restore_target_pct) > 0.5f;"
then:
- lambda: |-
id(ramp_switch_target_on) = true; // reflect our intent
id(suppress_slider_sync) = true;
id(suppress_slider_sync) = true;
- script.stop: ramp_off_script
- script.execute: ramp_to_target_script
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- lambda: 'id(suppress_slider_sync) = false;'
- delay: !lambda "return (uint32_t) id(last_ramp_ms);"
- lambda: "id(suppress_slider_sync) = false;"
else:
# Exact last was 0 -> remain OFF
- lambda: 'id(ramp_switch_target_on) = false;'
- lambda: "id(ramp_switch_target_on) = false;"
- light.turn_off:
id: mosfet_leds
transition_length: 0s