yaml layout updates to v1.x

This commit is contained in:
root
2026-02-25 21:05:21 +13:00
parent 61b0c64a1e
commit a8d2927274
44 changed files with 6521 additions and 4689 deletions
+231 -242
View File
@@ -1,83 +1,111 @@
##########################################################################################
##########################################################################################
# Title: DOWNSTAIRS KITCHEN - OVER PANTRY LEDS
# Repo: https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-downstairskitchleds.yaml
#:########################################################################################:#
# TITLE: DOWNSTAIRS KITCHEN - OVER PANTRY LEDS
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-downstairskitchleds.yaml
#:########################################################################################:#
# VERSIONS:
# v2.1 2026-02-25 Updated yaml to zorruno layout V1.1
# v2.0 2025-09-15 A bunch of fixes.
# v1.5 2025-09-04 Adopt ramped "Restore Brightness" (0->last non-zero), avoid boot blip (ALWAYS_OFF + high-priority on_boot),
# add flash-write coalescing (persist once at end of ramps), guards for edge cases, and flash_write_interval=5min
# v1.4 2025-08-22 Improved power loss/on actions
# v1.3 2025-08-22 Added a "max on time" setting (1-48 h, 0 = no limit)
# v1.2 2025-08-21 Added defaults to "Device Specific Settings" in substitutions & a PWM % view
# v1.1 2025-08-18 Full tidy-up as general-purpose LED strip controller
# v1.0 2025-08-17 First setup (and replacement of Tasmota)
#:########################################################################################:#
# HARDWARE:
# - Sinilink MOSFET Board XY-WFMS (ESP8266) - sometimes listed as "XY-VFMS"
# https://devices.esphome.io/devices/Sinilink-XY-WFMS
#
# v2.0 - 2025-09-15 A bunch of fixes.
# v1.5 - 2025-09-04 Adopt ramped "Restore Brightness" (0→last non-zero), avoid boot blip (ALWAYS_OFF + high-priority on_boot),
# add flash-write coalescing (persist once at end of ramps), guards for edge cases, and flash_write_interval=5min
# v1.4 - 2025-08-22 Improved power loss/on actions
# v1.3 - 2025-08-22 Added a "max on time” setting (1-48 h, 0 = no limit)
# v1.2 - 2025-08-21 Added defaults to “Device Specific Settings” in substitutions & a PWM % view
# v1.1 - 2025-08-18 Full tidy-up as general-purpose LED strip controller
# v1.0 - 2025-08-17 First setup (and replacement of Tasmota)
# - 3D-printed case for Sinilink XY-WFMS
# https://cults3d.com/en/3d-model/tool/snapfit-enclosure-for-esp8266-sinilink-xy-wfms-5v-36v-mosfet-switch-module
#
# ------------------------------------------
# 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. Green LED rule: ON while any ramp script is executing, otherwise OFF. 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
# - Normal On/Off switch (quick ramp up/down)
# - 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: Sinilink MOSFET Board XY-WFMS (ESP8266) - sometimes listed as "XY-VFMS"
# https://devices.esphome.io/devices/Sinilink-XY-WFMS
# ------------------------------------------
# DEVICE GPIO (Sinilink XY-WFMS)
# ------------------------------------------
# GPIO02 Blue LED, active-low (used for ESPHome status)
# GPIO04 MOSFET output (0V when switched) and Red LED
# GPIO12 Toggle button
# GPIO13 Green LED (used to display fading status)
# - GPIO02 Blue LED, active-low (used for ESPHome status)
# - GPIO04 MOSFET output (0V when switched) and Red LED
# - GPIO12 Toggle button
# - GPIO13 Green LED (used to display fading status)
#:########################################################################################:#
# OPERATION NOTES:
# - 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. Green LED rule: ON while any ramp script is executing, otherwise OFF. 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
# - Normal On/Off switch (quick ramp up/down)
# - 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)
#:########################################################################################:#
# MQTT COMMANDS:
# - Command topic:
# ${mqtt_local_command_topic}/light/set
# Payload "${mqtt_local_device_command_ON}" -> fade up (switch ON)
# Payload "${mqtt_local_device_command_OFF}" -> fade down (switch OFF)
#
# 3D-printed case for Sinilink XY-WFMS
# https://cults3d.com/en/3d-model/tool/snapfit-enclosure-for-esp8266-sinilink-xy-wfms-5v-36v-mosfet-switch-module
#
##########################################################################################
##########################################################################################
# - Status topic:
# ${mqtt_local_status_topic}/light/state
# Payload "${mqtt_local_device_command_ON}" / "${mqtt_local_device_command_OFF}"
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant OFFLINE, but Network and MQTT ONLINE
# - MQTT control continues to work
# - LED control logic continues locally
# b) MQTT OFFLINE (but WiFi/Network and HA API ONLINE)
# - HA/API control continues to work (if API enabled)
# - MQTT commands/status will not work while MQTT is down
# c) Entire WiFi/Network OFFLINE
# - No HA/API and no MQTT control available
# - Local button still controls fade up/down
# - Accurate time is NOT needed (SNTP not needed)
#:########################################################################################:#
##########################################################################################
# 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-downstairskitchleds" # yaml file should be device_name.yaml
friendly_name: "Downstairs Kitchen LEDs"
description_comment: "Downstairs Kitchen Over Pantry LEDs :: Sinilink XY-WFMS"
description_comment: "Downstairs Kitchen Over Pantry LEDs :: Sinilink XY-WFMS (Layout V1.1)"
device_area: "Downstairs Kitchen" # Allows the ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sinilink.XY-WFMS" # 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: "Sinilink.XY-WFMS" # 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)
# Passwords & Secrets (Unfortunately, you can't use substitutions inside secret names)
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-downstairskitchleds_ip # CHANGE THIS
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-downstairskitchleds_ip
# If we are changing IP addresses, you must update the current IP address here, otherwise it remains
# Don't forget to switch it back when changed.
current_ip_address: ${static_ip_address}
# MQTT LOCAL Controls
mqtt_local_device_name: "downstairskitchen-pantryleds"
mqtt_local_command_main_topic: !secret mqtt_command_main_topic
@@ -88,24 +116,24 @@ 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
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
log_level: "NONE" # 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
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
# Device Specific GPIO (so we can easily update for other devices)
device_status_led: GPIO02
device_mosfet_out: GPIO04
device_usr_button: GPIO12 # 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
device_usr_button: GPIO12 # 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
# https://esphome.io/components/packages.html
##########################################################################################
#:########################################################################################:#
packages:
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
common_wifi: !include
@@ -114,6 +142,7 @@ packages:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
local_current_ip_address: "${current_ip_address}"
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
common_api: !include
@@ -127,22 +156,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}"
@@ -190,25 +220,10 @@ esphome:
# Boot complete: allow OFF handlers again
- lambda: 'id(booting) = false;'
# NEW: force indicator OFF after boot logic
# (Removed: output.turn_off: green_led_out)
# Only if you want to play with build flags...
# platformio_options:
# build_unflags:
# - -flto
# build_flags:
# - -fno-lto
# - -Wl,--gc-sections
# - -ffunction-sections
# - -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
@@ -219,118 +234,127 @@ mdns:
preferences:
flash_write_interval: 2min # enough time to update values for reboots, but not enough to wear flash
##########################################################################################
# GLOBAL VARIABLES
#:########################################################################################:#
# GLOBALS
# https://esphome.io/components/globals.html
##########################################################################################
#:########################################################################################:#
globals:
# Minimum Brightness % for LEDs (will switch off if <=)
- id: min_brightness_pct
type: int
restore_value: true
initial_value: "${minimum_led_output}" # start/finish at X%
initial_value: "${minimum_led_output}" # start/finish at X%
# Maximum Brightness % for LEDs (should never go beyond this)
- id: max_brightness_pct
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
initial_value: "${maximum_led_output}" # hard cap; never exceed this
# The maximum time the lights will stay on, in hours. 0 = forever
- id: max_on_hours
type: int
restore_value: true
initial_value: "${max_on_default_hours}"
# Compile time Gamma
- id: led_gamma_f
type: float
restore_value: false
initial_value: ${led_gamma} # <-- numeric at compile time
initial_value: ${led_gamma}
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
- id: ramp_up_ms
type: int
restore_value: true
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
# Action on Restart. (0=Fade full, 1=Restore brightness, 2=Remain off)
- 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: "5000" # 5 s
# Determine last fade direction.
# true when you asked the light to end up ON (Ramp Up)
# false when you asked the light to end up OFF (Ramp Down)
# Default Fading Down Time (Selectable and will be retained)
- id: ramp_down_ms
type: int
restore_value: true
initial_value: "10000" # 10 s
# Action on Restart (0=Fade full, 1=Restore brightness, 2=Remain off)
- id: restart_mode
type: int
restore_value: true
initial_value: "0"
# true when you asked the light to end up ON (Ramp Up); false when OFF (Ramp Down)
- 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
#:########################################################################################:#
# LOGGING
# 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)
level: "${log_level}"
baud_rate: 0
#########################################################################################
#:########################################################################################:#
# 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: Device-specific command triggers
#:########################################################################################:#
mqtt:
on_message:
# Light control to ramp up
@@ -338,18 +362,19 @@ mqtt:
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: mosfet_ramp_switch
# Light control to ramp down
- topic: "${mqtt_local_command_topic}/light/set"
payload: "${mqtt_local_device_command_OFF}"
then:
- switch.turn_off: mosfet_ramp_switch
##########################################################################################
# SWITCH COMPONENT
#:########################################################################################:#
# SWITCH
# https://esphome.io/components/switch/
##########################################################################################
#:########################################################################################:#
switch:
# Ramp-aware ON/OFF for HA (asymmetric, eased; no bounce)
# Ramp-aware ON/OFF for HA (asymmetric; no bounce)
- platform: template
id: mosfet_ramp_switch
name: "${friendly_name} Fade Up-Down"
@@ -367,10 +392,10 @@ switch:
- script.stop: ramp_on_script
- script.execute: ramp_off_script
#################################################################################################
# BUTTON COMPONENT
#:########################################################################################:#
# BUTTON
# https://esphome.io/components/button/index.html
#################################################################################################
#:########################################################################################:#
button:
# Start ramping UP (from current level)
- platform: template
@@ -402,15 +427,12 @@ button:
name: "${friendly_name} Fade Stop"
icon: mdi:pause
on_press:
# Stop any pending scripts (and their delayed actions)
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- lambda: |-
// We are no longer ramping (up or down)
id(ramping_for_off) = false;
id(is_ramping) = false;
// 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();
@@ -420,18 +442,15 @@ button:
call.perform();
}
// Persist the exact frozen level so restore survives a reboot
if (cv.is_on()) {
float pct = cv.get_brightness() * 100.0f;
id(last_brightness_pct) = pct;
}
#########################################################################################
# SELECT COMPONENT
#:########################################################################################:#
# SELECT
# https://esphome.io/components/select/index.html
#########################################################################################
#:########################################################################################:#
select:
- platform: template
id: restart_action
@@ -453,10 +472,10 @@ select:
id(restart_mode) = 2;
}
#########################################################################################
# BINARY SENSORS
#:########################################################################################:#
# BINARY SENSOR
# https://esphome.io/components/binary_sensor/
#########################################################################################
#:########################################################################################:#
binary_sensor:
- platform: gpio
id: btn_gpio12
@@ -475,24 +494,22 @@ binary_sensor:
condition:
lambda: 'return id(ramp_switch_target_on);'
then:
# Target is currently ON -> press should go OFF (start ramp-down)
- lambda: |-
id(ramp_switch_target_on) = false;
id(mosfet_ramp_switch).publish_state(false); // reflect in HA immediately
- script.stop: ramp_on_script
- script.execute: ramp_off_script
else:
# Target is currently OFF -> press should go ON (start ramp-up)
- lambda: |-
id(ramp_switch_target_on) = true;
id(mosfet_ramp_switch).publish_state(true); // reflect in HA immediately
- script.stop: ramp_off_script
- script.execute: ramp_on_script
##########################################################################################
# SENSOR COMPONENT
#:########################################################################################:#
# SENSOR
# https://esphome.io/components/sensor/
##########################################################################################
#:########################################################################################:#
sensor:
- platform: template
id: mosfet_output_pct
@@ -500,17 +517,15 @@ 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;
on_value:
then:
- lambda: |-
// If not suppressing sync, update the 0..100 slider only when its INT changes
if (!id(suppress_slider_sync)) {
float actual = x; // x is current Output (%)
float actual = x;
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f;
@@ -541,28 +556,26 @@ 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
# https://esphome.io/components/output/
#:########################################################################################:#
output:
- platform: esp8266_pwm
id: mosfet_pwm
pin: ${device_mosfet_out}
frequency: ${pwm_frequency}
- platform: gpio
id: green_led_out # Green LED
id: green_led_out
pin:
number: ${device_fading_led}
inverted: false
##########################################################################################
# LIGHT COMPONENT
#:########################################################################################:#
# LIGHT
# https://esphome.io/components/light/
##########################################################################################
#:########################################################################################:#
light:
- platform: monochromatic
id: mosfet_leds
@@ -573,7 +586,6 @@ light:
icon: mdi:led-strip-variant
gamma_correct: "${led_gamma}"
# ON: publish state, track intent, arm watchdog if configured
on_turn_on:
- output.turn_on: green_led_out
- mqtt.publish:
@@ -581,7 +593,6 @@ 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
if (!id(ramping_for_off)) {
id(ramp_switch_target_on) = true;
}
@@ -592,8 +603,6 @@ light:
then:
- script.execute: max_on_watchdog
# OFF: publish state, track intent, clear watchdog, and persist OFF so Mode 1 will not restore
# Guarded by booting flag so the automatic OFF at boot from ALWAYS_OFF does not zero persistence.
on_turn_off:
- output.turn_off: green_led_out
- if:
@@ -611,31 +620,29 @@ light:
id(last_set_pos) = 0;
id(led_output_set_pct).publish_state(0);
# Cap brightness to max_brightness_pct without a visible step
on_state:
- lambda: |-
const float cap = id(max_brightness_pct) / 100.0f;
const auto &cv = id(mosfet_leds).current_values;
if (!cv.is_on()) return;
const float b = cv.get_brightness();
if (b <= cap + 0.001f) return; // no call if already at/under cap
if (b <= cap + 0.001f) return;
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
##########################################################################################
# NUMBER COMPONENT
#:########################################################################################:#
# NUMBER
# https://esphome.io/components/number/
##########################################################################################
#:########################################################################################:#
number:
- platform: template
id: cfg_ramp_up_s
name: "${friendly_name} Fade Up Time (s)"
entity_category: config
unit_of_measurement: s
unit_of_measurement: "s"
icon: mdi:timer-sand
mode: slider
min_value: 0
@@ -655,7 +662,7 @@ number:
id: cfg_ramp_down_s
name: "${friendly_name} Fade Down Time (s)"
entity_category: config
unit_of_measurement: s
unit_of_measurement: "s"
icon: mdi:timer-sand-complete
mode: slider
min_value: 0
@@ -679,24 +686,22 @@ number:
min_value: 0
max_value: 100
step: 1
# Show current position mapped into 0..100 across [min..max]
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
float actual = cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f; // 0..100 actual
float actual = cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f;
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f; // avoid div/0
if (actual <= 0.0f) return 0.0f; // when OFF, show 0
if (maxp <= minp) maxp = minp + 1.0f;
if (actual <= 0.0f) return 0.0f;
float pos = (actual - minp) * 100.0f / (maxp - minp);
if (pos < 0.0f) pos = 0.0f;
if (pos > 100.0f) pos = 100.0f;
return floorf(pos + 0.5f); // integer
return floorf(pos + 0.5f);
set_action:
- if:
condition:
lambda: 'return x <= 0.0f;'
then:
# 0 means OFF
- lambda: 'id(suppress_slider_sync) = true;'
- script.stop: ramp_on_script
- script.stop: ramp_off_script
@@ -706,16 +711,15 @@ number:
- lambda: |-
id(ramp_switch_target_on) = false;
id(led_output_set_pct).publish_state(0);
id(last_brightness_pct) = 0.0f; // persist OFF immediately
id(last_brightness_pct) = 0.0f;
id(last_set_pos) = 0;
- delay: 400ms
- lambda: 'id(suppress_slider_sync) = false;'
else:
# Map 1..100 - [min..max] and set ON
- lambda: |-
id(suppress_slider_sync) = true;
float pos = x; // 0..100
if (pos < 1.0f) pos = 1.0f; // 0 is OFF
float pos = x;
if (pos < 1.0f) pos = 1.0f;
if (pos > 100.0f) pos = 100.0f;
id(led_output_set_pct).publish_state((int) floorf(pos + 0.5f));
- script.stop: ramp_off_script
@@ -723,7 +727,7 @@ number:
- light.turn_on:
id: mosfet_leds
brightness: !lambda |-
float pos = id(led_output_set_pct).state; // 1..100
float pos = id(led_output_set_pct).state;
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f;
@@ -733,13 +737,13 @@ number:
transition_length: 250ms
- lambda: |-
id(ramp_switch_target_on) = true;
float pos = id(led_output_set_pct).state; // 1..100
float pos = id(led_output_set_pct).state;
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (maxp <= minp) maxp = minp + 1.0f;
float out_pct = minp + (pos * (maxp - minp) / 100.0f);
if (out_pct > maxp) out_pct = maxp;
id(last_brightness_pct) = out_pct; // persist exact target now
id(last_brightness_pct) = out_pct;
- delay: 400ms
- lambda: 'id(suppress_slider_sync) = false;'
@@ -747,7 +751,7 @@ number:
id: cfg_max_on_hours
name: "${friendly_name} Max On (h)"
entity_category: config
unit_of_measurement: h
unit_of_measurement: "h"
icon: mdi:timer-cog
mode: slider
min_value: 0
@@ -758,7 +762,7 @@ number:
set_action:
- lambda: |-
int hrs = (int) x;
if (hrs < 0) hrs = 0;
if (hrs < 0) hrs = 0;
if (hrs > 48) hrs = 48;
id(max_on_hours) = hrs;
id(cfg_max_on_hours).publish_state((float) hrs);
@@ -773,20 +777,16 @@ number:
then:
- script.execute: max_on_watchdog
##########################################################################################
# SCRIPT COMPONENT
#:########################################################################################:#
# SCRIPT
# 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)
- id: ramp_on_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
lambda: |-
@@ -798,7 +798,6 @@ script:
id: mosfet_leds
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;'
@@ -814,7 +813,7 @@ script:
id(last_ramp_ms) = id(ramp_up_ms);
return (uint32_t) id(last_ramp_ms);
}
if (curr > cap) curr = cap;
if (curr > cap) curr = cap;
if (curr < floor) curr = floor;
float frac = (cap - curr) / (cap - floor);
if (frac < 0.0f) frac = 0.0f;
@@ -827,16 +826,14 @@ script:
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on()) {
float pct = cv.get_brightness() * 100.0f;
id(last_brightness_pct) = pct; // persist final level
id(last_brightness_pct) = pct;
}
# Script: ramp to a specific target (Restore Brightness path). (LED ON while ramping)
- id: ramp_to_target_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
# Ensure we start at the floor cleanly.
- if:
condition:
lambda: |-
@@ -848,7 +845,6 @@ script:
id: mosfet_leds
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:
id: mosfet_leds
brightness: !lambda |-
@@ -870,7 +866,7 @@ script:
return (uint32_t) id(last_ramp_ms);
}
if (curr > cap) curr = cap;
if (curr > cap) curr = cap;
if (curr < floor) curr = floor;
float frac = (cap - curr) / (cap - floor);
if (frac < 0.0f) frac = 0.0f;
@@ -883,11 +879,9 @@ script:
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on()) {
float pct = cv.get_brightness() * 100.0f;
id(last_brightness_pct) = pct; // persist final level
id(last_brightness_pct) = pct;
}
# Script: ramp down from current level to floor, then cleanly cut to OFF. (LED ON while ramping)
- id: ramp_off_script
mode: restart
then:
@@ -918,7 +912,7 @@ script:
- lambda: |-
id(is_ramping) = false;
id(ramping_for_off) = false;
id(last_brightness_pct) = 0.0f; // persist OFF for restore path
id(last_brightness_pct) = 0.0f;
id(last_set_pos) = 0;
id(led_output_set_pct).publish_state(0);
@@ -940,20 +934,17 @@ script:
- script.stop: ramp_on_script
- script.execute: ramp_off_script
# Deferred restore to avoid early-boot races and ensure we apply the last saved level
- id: deferred_restore_brightness
mode: restart
then:
- delay: 5s # let preferences/globals load and light settle
- delay: 5s
- lambda: |-
// Use ONLY the exact last saved brightness.
float target = id(last_brightness_pct);
// Clamp into [min..max] only if we are restoring a non-zero target
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (target > 0.0f && target < minp) target = minp;
if (target > maxp) target = maxp;
if (target > maxp) target = maxp;
id(restore_target_pct) = target;
@@ -962,16 +953,14 @@ script:
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(ramp_switch_target_on) = 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;'
else:
# Exact last was 0 -> remain OFF
- lambda: 'id(ramp_switch_target_on) = false;'
- light.turn_off:
id: mosfet_leds
transition_length: 0s
# (Removed: output.turn_off: green_led_out) # make sure LED is off if no ramp
transition_length: 0s