experiments in AC dimming (moes and robotdyn)

This commit is contained in:
root
2025-09-05 18:51:42 +12:00
parent de2eee7bb6
commit 0dfef5ced1
5 changed files with 2212 additions and 37 deletions
+150 -37
View File
@@ -5,6 +5,8 @@
# https://devices.esphome.io/devices/Sinilink-XY-VFMS
# Repo: https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-downstairskitchleds.yaml
#
# 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
@@ -20,7 +22,7 @@
# GPIO13 Green LED (used to display fading status)
#
# ------------------------------------------
# OPERATION (as of v1.4)
# OPERATION (as of v1.5)
# ------------------------------------------
# 1. General-purpose LED controller.
# 2. Designed for the Sinilink XY-WFMS board with a MOSFET output (claimed 5A, 5-36 V DC).
@@ -31,7 +33,10 @@
# 6. Min/Max output settings are not exposed in Home Assistant/MQTT by default, but can 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 behaviour is to fade slowly up to full at power-up (so it can run with no network).
# 8. Default behaviour:
# - "Fade up to full": fade from floor to max on boot.
# - "Restore Brightness": fade from floor to the last non-zero brightness on boot (no on/off blip).
# - "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).
# 10. Fade timing scales with the configured values (proportionally when starting mid-brightness).
@@ -64,7 +69,7 @@ substitutions:
# Project Naming
project_name: "Sinilink.XY-WFMS" # Project details
project_version: "v1.4" # Project version denotes release of the YAML file, allowing checking of deployed vs latest version
project_version: "v1.5" # Project version denotes release of the YAML file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key
@@ -100,8 +105,8 @@ packages:
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
#file: common/api_common.yaml
file: common/api_common_noencryption.yaml
file: common/api_common.yaml
#file: common/api_common_noencryption.yaml
vars:
local_api_key: "${api_key}"
#common_webportal: !include
@@ -113,8 +118,8 @@ packages:
#common_sntp: !include
# file: common/sntp_common.yaml
common_general_sensors: !include
#file: common/sensors_common.yaml
file: common/sensors_common_lite.yaml
file: common/sensors_common.yaml
#file: common/sensors_common_lite.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
@@ -129,7 +134,7 @@ esphome:
comment: "${description_comment}"
area: "${device_area}"
on_boot:
priority: -200
priority: 600
then:
# Keep the HA dropdown in sync with the stored mode
- lambda: |-
@@ -147,14 +152,13 @@ esphome:
- lambda: 'id(ramp_switch_target_on) = true;'
- script.execute: ramp_on_script
# Mode 1: Restore Brightness using the light's normal defaults
# (uses default_transition_length and on_turn_on handlers)
# Mode 1: Restore Brightness using a scripted ramp 0 -> last non-zero brightness
- if:
condition:
lambda: 'return id(restart_mode) == 1;'
then:
- lambda: |-
float target = id(last_brightness_pct);
float target = id(last_nonzero_brightness_pct);
if (target < 0.0f) target = 0.0f;
if (target > 100.0f) target = 100.0f;
@@ -164,23 +168,18 @@ esphome:
if (target > 0.0f && target < minp) target = minp;
if (target > maxp) target = maxp;
id(suppress_slider_sync) = true;
if (target <= 0.0f) {
id(ramp_switch_target_on) = false;
auto call = id(mosfet_leds).make_call();
call.set_state(false);
call.set_transition_length(0);
call.perform();
} else {
id(ramp_switch_target_on) = true;
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(target / 100.0f);
// No transition_length here: use light.default_transition_length.
call.perform();
}
- delay: 300ms
- lambda: 'id(suppress_slider_sync) = false;'
id(restore_target_pct) = target; // set scripted target
id(ramp_switch_target_on) = (target > 0.0f);
- if:
condition:
lambda: 'return id(restore_target_pct) > 0.0f;'
then:
- script.stop: ramp_off_script
- script.execute: ramp_to_target_script
else:
- light.turn_off:
id: mosfet_leds
transition_length: 0s
# Mode 2: Remain Off (no blip; stays off)
- if:
@@ -219,6 +218,9 @@ esp8266:
mdns:
disabled: false # Disabling will make the build file smaller (and it is still available via static IP)
preferences:
flash_write_interval: 5min
##########################################################################################
# GLOBAL VARIABLES
# https://esphome.io/components/globals.html
@@ -282,6 +284,26 @@ globals:
type: int
restore_value: false
initial_value: '0'
# last non-zero actual 0..100 used, for stable "Restore Brightness"
- id: last_nonzero_brightness_pct
type: float
restore_value: true
initial_value: '0.0'
# target for "Restore Brightness" scripted ramp
- id: restore_target_pct
type: float
restore_value: false
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'
# volatile (non-persistent) last non-zero brightness during ramps
- id: last_nonzero_tmp
type: float
restore_value: false
initial_value: '0.0'
##########################################################################################
# LOGGER COMPONENT
@@ -303,7 +325,7 @@ mqtt:
payload: "${mqtt_local_device_command_ON}"
then:
- switch.turn_on: mosfet_ramp_switch
# Light control to ramp up
# Light control to ramp down
- topic: "${mqtt_local_command_topic}/light/set"
payload: "${mqtt_local_device_command_OFF}"
then:
@@ -333,11 +355,13 @@ switch:
lambda: |-
return id(ramp_switch_target_on);
turn_on_action:
- lambda: 'id(ramp_switch_target_on) = true;'
- lambda: |-
id(ramp_switch_target_on) = true;
- script.stop: ramp_off_script
- script.execute: ramp_on_script
turn_off_action:
- lambda: 'id(ramp_switch_target_on) = false;'
- lambda: |-
id(ramp_switch_target_on) = false;
- script.stop: ramp_on_script
- script.execute: ramp_off_script
@@ -475,6 +499,11 @@ sensor:
- lambda: |-
// Remember latest actual output (0..100) for "Restore Brightness"
id(last_brightness_pct) = x;
if (x > 0.5f) {
// Only persist when not ramping; always track a volatile copy
if (!id(is_ramping)) id(last_nonzero_brightness_pct) = x;
id(last_nonzero_tmp) = x;
}
// If not suppressing sync, update the 0..100 slider only when its INT changes
if (!id(suppress_slider_sync)) {
@@ -503,7 +532,7 @@ sensor:
const auto &cv = id(mosfet_leds).current_values;
if (!cv.is_on()) return 0.0f;
const float lin = cv.get_brightness(); // 0..1 (linear brightness)
const float gamma = atof("${led_gamma}"); // parse substitution string float
const float gamma = atof("${led_gamma}"); // parse substitution string -> float
float pwm = powf(lin, gamma); // approx PWM duty after gamma
if (pwm < 0.0f) pwm = 0.0f;
if (pwm > 1.0f) pwm = 1.0f;
@@ -520,7 +549,7 @@ output:
- platform: esp8266_pwm
id: mosfet_pwm
pin: GPIO4
frequency: 500 Hz # high frequency to avoid audible/visible artifacts
frequency: 1000 Hz # high frequency to avoid audible/visible artifacts
- platform: gpio
id: green_led_out # Green LED
pin:
@@ -536,7 +565,7 @@ light:
id: mosfet_leds
name: "${friendly_name}"
output: mosfet_pwm
restore_mode: RESTORE_DEFAULT_OFF
restore_mode: ALWAYS_OFF
default_transition_length: 2s
icon: mdi:led-strip-variant
gamma_correct: "${led_gamma}"
@@ -747,6 +776,7 @@ script:
- id: ramp_on_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;' # mark ramping
- script.stop: ramp_off_script
- script.stop: led_flash_down
- script.execute: led_flash_up
@@ -770,9 +800,19 @@ script:
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
const float cap = id(max_brightness_pct) / 100.0f;
if (cap <= floor + 0.0005f) { id(last_ramp_ms) = 0; return (uint32_t) 0; } // guard degenerate case
// IMPORTANT: when the light was OFF, treat current as 0 so we don't compute a 0ms jump to cap.
float curr = cv.is_on() ? cv.get_brightness() : 0.0f;
if (curr < floor) curr = floor;
// If we're below the floor (i.e. coming from OFF), we just did the "floor kick" above.
// Use full ramp_up_ms for a smooth 0 -> cap ramp.
if (curr + 0.0005f < floor) {
id(last_ramp_ms) = id(ramp_up_ms);
return (uint32_t) id(last_ramp_ms);
}
if (curr > cap) curr = cap;
if (curr < floor) curr = floor;
float frac = (cap - curr) / (cap - floor);
if (frac < 0.0f) frac = 0.0f;
if (frac > 1.0f) frac = 1.0f;
@@ -781,12 +821,82 @@ script:
- delay: !lambda 'return (uint32_t) id(last_ramp_ms);'
- script.stop: led_flash_up
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false; // ramp complete
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on()) {
float pct = cv.get_brightness() * 100.0f;
if (pct > 0.5f) id(last_nonzero_brightness_pct) = pct; // commit once
}
# Script: ramp to a specific target (Restore Brightness path)
- id: ramp_to_target_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;' # mark ramping
- script.stop: ramp_off_script
- script.stop: led_flash_down
- script.execute: led_flash_up
# Ensure we start at the floor cleanly.
- if:
condition:
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
return (!cv.is_on()) || (cv.get_brightness() + 0.0005f < floor);
then:
- light.turn_on:
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 |-
float cap_pct = id(restore_target_pct);
float maxp = (float) id(max_brightness_pct);
if (cap_pct > maxp) cap_pct = maxp;
return cap_pct / 100.0f;
transition_length: !lambda |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
float cap = id(restore_target_pct) / 100.0f;
const float maxp = id(max_brightness_pct) / 100.0f;
if (cap > maxp) cap = maxp;
if (cap <= floor + 0.0005f) { id(last_ramp_ms) = 0; return (uint32_t) 0; } // guard
// Same OFF case handling: if off, treat curr as 0 so we don't collapse the ramp.
float curr = cv.is_on() ? cv.get_brightness() : 0.0f;
// Coming from OFF/below floor? We just did the "floor kick" above — use full ramp_up_ms.
if (curr + 0.0005f < floor) {
id(last_ramp_ms) = id(ramp_up_ms);
return (uint32_t) id(last_ramp_ms);
}
if (curr > cap) curr = cap;
if (curr < floor) curr = floor;
float frac = (cap - curr) / (cap - floor);
if (frac < 0.0f) frac = 0.0f;
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);'
- script.stop: led_flash_up
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false; // ramp complete
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on()) {
float pct = cv.get_brightness() * 100.0f;
if (pct > 0.5f) id(last_nonzero_brightness_pct) = pct; // commit once
}
# Script: ramp down from current level to floor, then cleanly cut to OFF
- id: ramp_off_script
mode: restart
then:
- lambda: 'id(is_ramping) = true;' # mark ramping
- script.stop: ramp_on_script
- script.stop: led_flash_up
- script.execute: led_flash_down
@@ -796,9 +906,11 @@ script:
transition_length: !lambda |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
float curr = cv.is_on() ? cv.get_brightness() : 0.0f;
float curr = cv.get_brightness();
if (curr < floor) curr = floor;
float frac = (curr - floor) / (1.0f - floor);
const float denom = (1.0f - floor);
if (denom <= 0.0005f) { id(last_ramp_ms) = 0; return (uint32_t) 0; } // guard near-1.0 floor
float frac = (curr - floor) / denom;
if (frac < 0.0f) frac = 0.0f;
if (frac > 1.0f) frac = 1.0f;
id(last_ramp_ms) = (int) (id(ramp_down_ms) * frac);
@@ -811,6 +923,7 @@ script:
- script.stop: led_flash_down
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false; // ramp complete
auto call = id(mosfet_leds).make_call();
call.set_state(false);
call.set_brightness(id(max_brightness_pct) / 100.0f);