esphome pelmet LEDs, lounge scenes and other fixes

This commit is contained in:
root
2025-10-12 19:54:03 +13:00
parent e96344e3ad
commit bcf514a041
19 changed files with 951 additions and 1542 deletions
+11 -1
View File
@@ -8,7 +8,7 @@ substitutions:
# Debug helpers (loop time / heap / reset info)
debug:
update_interval: ${local_update_interval}
update_interval: 5min
sensor:
# Free heap via debug (replaces template-based ESP.getFreeHeap if you prefer)
@@ -16,12 +16,22 @@ sensor:
free:
name: "Free Heap"
entity_category: "diagnostic"
disabled_by_default: true
filters:
- delta: 1024 # only if heap changes by ≥1 KB
- heartbeat: 5min # but still publish every 5 min
# Loop time via debug (replaces broken template loop time)
- platform: debug
loop_time:
name: "Loop Time"
disabled_by_default: true
entity_category: "diagnostic"
filters:
- delta: 2 # only if loop time changes by ≥2 ms
- heartbeat: 5min
## If youd rather keep the legacy heap sensor, uncomment below and
## delete the debug "free" block above to avoid duplicates:
@@ -9,6 +9,7 @@ text_sensor:
- platform: version
name: "Version:"
entity_category: "diagnostic"
disabled_by_default: true
- platform: wifi_info
ip_address:
@@ -19,10 +20,12 @@ text_sensor:
name: "Connected SSID"
icon: mdi:wifi-strength-2
entity_category: diagnostic
disabled_by_default: true
mac_address:
name: "MAC Address:"
icon: mdi:network-pos
entity_category: diagnostic
disabled_by_default: true
#####################################################################################################
# Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds
-217
View File
@@ -1,217 +0,0 @@
#############################################
#############################################
# BYD ATTO3 12V Battery Monitor
# Monitoring the status of a vehicle 12V battery with
# an esp8266 (D1 Mini). It will obviously only
# transmit when the vehicle is within wifi range.
# Voltage is measured with a resistor voltage divider
# into the analogue GPIO on the esp8266.
# https://zorruno.com/2022/vehicle-12v-battery-monitoring/
##############################################
#############################################
#############################################
# Variable Substitutions
#############################################
substitutions:
devicename: "esp-attobat"
friendly_name: "Atto3 12V Battery Monitor"
description_comment: "Atto3 12V Battery Monitor (when home)"
api_key: !secret esp-attobat_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-attobat_ota_pass #unfortunately you can't use substitutions in secrets names
wifi_ssid: !secret wifi_ssid
wifi_password: !secret wifi_password
fallback_ap_password: !secret fallback_ap_password
#Add these if we are giving it a static ip, or remove them in the Wifi section
#A static IP will speed things up slightly in that it doesn't have to negotiate DHCP
static_ip_address: !secret esp-attobat_static_ip
static_ip_gateway: !secret esp-attobat_gateway
static_ip_subnet: !secret esp-attobat_subnet
mqtt_server: !secret mqtt_server
mqtt_username: !secret mqtt_username
mqtt_password: !secret mqtt_password
mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
mqtt_commandstopic: "viewroad-commands" #main topic for commands (ie sleep), call it what you like
#web_server_username: !secret web_server_username
#web_server_password: !secret web_server_password
update_time: 10s #update time for for general temp sensors etc
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${devicename}
friendly_name: ${friendly_name}
comment: ${description_comment} #appears on the esphome page in HA
min_version: 2024.6.0
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: d1_mini
framework:
version: latest #recommended, latest or dev
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: INFO #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)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
#############################################
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
encryption:
key: ${api_key}
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: esphome
password: ${ota_pass}
- platform: web_server # Uncomment if you want to be able to do OTA with the web interface
#############################################
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################
safe_mode:
#############################################
# Wifi Settings
# https://esphome.io/components/wifi.html
#
# Power Save mode (can reduce wifi reliability)
# NONE (least power saving, Default for ESP8266)
# LIGHT (Default for ESP32)
# HIGH (most power saving)
#############################################
wifi:
ssid: ${wifi_ssid}
password: ${wifi_password}
#power_save_mode: LIGHT #https://esphome.io/components/wifi.html#wifi-power-save-mode
manual_ip: #optional static IP address
static_ip: ${static_ip_address}
gateway: ${static_ip_gateway}
subnet: ${static_ip_subnet}
ap: #Details for fallback hotspot (captive portal) in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${devicename} fallback AP
password: ${fallback_ap_password}
ap_timeout: 30min #Time until it brings up fallback AP. default is 1min
#############################################
# Web Portal for display and monitoring
#############################################
web_server:
port: 80
#version: 2
#include_internal: true
#ota: false
#auth:
#username: ${web_server_username}
#password: ${web_server_password}
#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
mqtt:
broker: ${mqtt_server}
topic_prefix: ${mqtt_topic}/${devicename}
username: ${mqtt_username}
password: ${mqtt_password}
discovery: False # enable entity discovery (true is default, we don't want two HA Instances)
# Availability Topic
birth_message:
topic: ${mqtt_topic}/${devicename}/availability
payload: online
will_message:
topic: ${mqtt_topic}/${devicename}/availability
payload: offline
# A way to prevent deep sleep using MQTT command
on_message:
- topic: ${mqtt_commandstopic}/${devicename}/deepsleep
payload: "OFF"
then:
- deep_sleep.prevent: deep_sleep_1
- topic: ${mqtt_commandstopic}/${devicename}/deepsleep
payload: "ON"
then:
- deep_sleep.enter: deep_sleep_1
# on_message:
# - topic: ${mqtt_commandstopic}/${devicename}/deepsleepon
# payload: 'ON'
# then:
# - deep_sleep.prevent: deep_sleep_1
# - topic: ${mqtt_commandstopic}/${devicename}/deepsleepoff
# payload: 'OFF'
# then:
# - deep_sleep.enter: deep_sleep_1
########################################
# Deep Sleep
# https://esphome.io/components/deep_sleep.html
########################################
deep_sleep:
run_duration: 20s
sleep_duration: 5min
id: deep_sleep_1
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
sensor:
#Quality of Wifi in dBm
- platform: wifi_signal
name: "WiFi Signal"
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
#Analog sensor for voltage reading (A0)
- platform: uptime
name: "Uptime"
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
- platform: adc
pin: A0
name: "Battery Voltage"
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
filters:
- multiply: 3.3 #D1 mini V divider, 3.3V -> 1V on esp8266
- calibrate_linear: #Read values with voltmeter and bench supply
- 3.11 -> 14.00
- 3.00 -> 13.50
- 2.89 -> 13.00
- 2.77 -> 12.50
- 2.67 -> 12.00
- 2.55 -> 11.50
- 2.45 -> 11.00
- 2.34 -> 10.50
- 2.22 -> 10.00
- 2.11 -> 09.50
- 2.00 -> 09.00
+65 -35
View File
@@ -34,20 +34,21 @@ substitutions:
# MQTT LOCAL Controls
mqtt_device_name: "atto-battery"
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
mqtt_command_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_status_topic: "${mqtt_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "DEBUG" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
wake_interval: "20s"
sleep_interval: "300s"
mqtt_fail_sleep: "1h" # If MQTT is unavailable after boot wait, sleep this long
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
# Comment some of these out where not needed, eg MQTT, Web portal, SNTP, General Sensors
##########################################################################################
packages:
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
common_wifi: !include
@@ -74,12 +75,12 @@ packages:
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_debug: !include common/include_debug_diag_sensors.yaml
#diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
##########################################################################################
@@ -91,6 +92,29 @@ esphome:
friendly_name: ${friendly_name}
comment: ${description_comment} #appears on the esphome page in HA
min_version: 2024.6.0
on_boot:
priority: 600
then:
# Default: do NOT deep sleep until explicitly enabled via MQTT
- deep_sleep.prevent: deep_sleep_1
- logger.log: "Deep sleep prevented by default on boot."
# Fail-safe: wait up to 30s for MQTT; if not connected, sleep longer (backoff)
- wait_until:
condition:
mqtt.connected:
timeout: 30s
- if:
condition:
not:
mqtt.connected:
then:
- logger.log: "MQTT unreachable after 30s -> entering FALLBACK sleep for ${mqtt_fail_sleep}."
- deep_sleep.enter:
id: deep_sleep_1
sleep_duration: ${mqtt_fail_sleep} # override only this cycle
else:
- logger.log: "MQTT connected; awaiting '${mqtt_command_topic}/deepsleep' command."
##########################################################################################
# ESP Platform and Framework
@@ -124,58 +148,51 @@ ota:
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
# Availability Topic
birth_message:
topic: ${mqtt_local_status_topic}/availability
topic: ${mqtt_status_topic}/availability
payload: online
will_message:
topic: ${mqtt_local_status_topic}/availability
topic: ${mqtt_status_topic}/availability
payload: offline
# A way to prevent deep sleep using MQTT command
# Control deep sleep over MQTT
on_message:
- topic: ${mqtt_local_command_topic}/deepsleep
- topic: ${mqtt_command_topic}/deepsleep
payload: "ON"
then:
- deep_sleep.allow: deep_sleep_1
- logger.log: "Deep sleep ALLOWED via MQTT. Device will sleep when run_duration elapses."
- topic: ${mqtt_command_topic}/deepsleep
payload: "OFF"
then:
- deep_sleep.prevent: deep_sleep_1
- topic: ${mqtt_local_command_topic}/deepsleep
payload: "ON"
then:
- deep_sleep.enter: deep_sleep_1
- logger.log: "Deep sleep PREVENTED via MQTT."
##########################################################################################
##########################################################################################
# Deep Sleep
# https://esphome.io/components/deep_sleep.html
##########################################################################################
##########################################################################################
deep_sleep:
run_duration: 20s
sleep_duration: 5min
id: deep_sleep_1
run_duration: ${wake_interval}
sleep_duration: ${sleep_interval}
##########################################################################################
# SENSOR COMPONENT
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
#Quality of Wifi in dBm
- platform: wifi_signal
name: "WiFi Signal"
update_interval: ${update_interval}
retain: true #Retain this as you'll have no value between sleeps otherwise
#Analog sensor for voltage reading (A0)
- platform: uptime
name: "Uptime"
update_interval: ${update_interval}
retain: true #Retain this as you'll have no value between sleeps otherwise
- platform: adc
pin: A0
name: "Battery Voltage"
update_interval: ${update_interval}
retain: true #Retain this as you'll have no value between sleeps otherwise
id: battery_voltage
update_interval: 4s
retain: true
filters:
- multiply: 3.3 #D1 mini V divider, 3.3V -> 1V on esp8266
- calibrate_linear: #Read values with voltmeter and bench supply
- multiply: 3.3
- calibrate_linear:
- 3.11 -> 14.00
- 3.00 -> 13.50
- 2.89 -> 13.00
@@ -187,3 +204,16 @@ sensor:
- 2.22 -> 10.00
- 2.11 -> 09.50
- 2.00 -> 09.00
- delta: 0.05 # publish immediately if change >= 0.05 V
- throttle: 2s # avoid bursts if noisy
on_value:
then:
- mqtt.publish:
topic: "${mqtt_status_topic}/batteryvoltage"
qos: 1
retain: true
payload: !lambda |-
char buf[10];
snprintf(buf, sizeof(buf), "%.2f", x);
return std::string(buf);
+30 -29
View File
@@ -83,13 +83,13 @@ substitutions:
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: "1000hz" # PWM output Frequency. 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 # Can't really see if there is an LED
device_status_led: GPIO02
device_mosfet_out: GPIO12
device_usr_button: GPIO04 # this is just a fake/safeish choice so it compiles with my generic yaml
device_fading_led: GPIO13 # this is just a fake/safeish choice so it compiles with my generic yaml
device_usr_button: GPIO04 # 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
@@ -124,8 +124,8 @@ packages:
#### 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_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
##########################################################################################
@@ -180,7 +180,7 @@ esphome:
- lambda: 'id(booting) = false;'
# NEW: force indicator OFF after boot logic
- output.turn_off: green_led_out
# (Removed: output.turn_off: green_led_out)
# Only if you want to play with build flags...
# platformio_options:
@@ -228,6 +228,11 @@ globals:
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
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
@@ -389,7 +394,6 @@ button:
# Stop any pending scripts (and their delayed actions)
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- output.turn_off: green_led_out
- lambda: |-
// We are no longer ramping (up or down)
id(ramping_for_off) = false;
@@ -485,10 +489,11 @@ sensor:
unit_of_measurement: "%"
icon: mdi:percent
accuracy_decimals: 0
update_interval: 1s # consider 200ms if you want fewer updates
update_interval: 2s # consider 200ms if you want fewer updates
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
return cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f;
return cv.is_on() ? (float) (int)(cv.get_brightness() * 100.0f + 0.5f) : 0.0f;
on_value:
then:
- lambda: |-
@@ -515,18 +520,16 @@ sensor:
unit_of_measurement: "%"
icon: mdi:square-wave
accuracy_decimals: 1
update_interval: 1s
update_interval: 2s
lambda: |-
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
float pwm = powf(lin, gamma); // approx PWM duty after gamma
const float lin = cv.get_brightness();
float pwm = powf(lin, id(led_gamma_f));
if (pwm < 0.0f) pwm = 0.0f;
if (pwm > 1.0f) pwm = 1.0f;
return pwm * 100.0f;
##########################################################################################
# OUTPUT COMPONENT
# https://esphome.io/components/light/index.html
@@ -561,6 +564,7 @@ light:
# ON: publish state, track intent, arm watchdog if configured
on_turn_on:
- output.turn_on: green_led_out
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_ON}"
@@ -580,6 +584,7 @@ light:
# 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:
condition:
lambda: 'return !id(booting);'
@@ -600,13 +605,15 @@ light:
- lambda: |-
const float cap = id(max_brightness_pct) / 100.0f;
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on() && cv.get_brightness() > cap + 0.001f) {
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
}
if (!cv.is_on()) return;
const float b = cv.get_brightness();
if (b <= cap + 0.001f) return; // no call if already at/under cap
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
##########################################################################################
# NUMBER COMPONENT
@@ -768,7 +775,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
@@ -805,7 +811,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -820,7 +825,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at the floor cleanly.
- if:
condition:
@@ -863,7 +867,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -881,7 +884,6 @@ script:
id(is_ramping) = true;
id(ramping_for_off) = true;
- script.stop: ramp_on_script
- output.turn_on: green_led_out
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
@@ -902,7 +904,6 @@ script:
id: mosfet_leds
transition_length: 150ms
- delay: 150ms
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
id(ramping_for_off) = false;
@@ -962,4 +963,4 @@ script:
- light.turn_off:
id: mosfet_leds
transition_length: 0s
- output.turn_off: green_led_out # make sure LED is off if no ramp
# (Removed: output.turn_off: green_led_out) # make sure LED is off if no ramp
+4 -4
View File
@@ -42,15 +42,15 @@ substitutions:
# MQTT LOCAL Controls
mqtt_local_device_name: "stair-mainlights-lower"
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}/set" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}/state" # Topic we will use to view status locally without HA
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}" # Topic we will use to view status locally without HA
mqtt_local_device_command_ON: "ON"
mqtt_local_device_command_OFF: "OFF"
# MQTT REMOTE Controls
mqtt_remote_device1_name: "stair-footerlights"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/set"
mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}/state"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}"
mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}"
#mqtt_remote_device2_name: "stair-mainlights-upper"
#mqtt_remote_device2_command_topic: "${mqtt_local_command_main_topic}/${mqtt_remote_device2_name}/relay3/set"
#mqtt_remote_device2_status_topic: "${mqtt_local_status_main_topic}/${mqtt_remote_device2_name}/relay3/state"
+4 -4
View File
@@ -42,15 +42,15 @@ substitutions:
# MQTT LOCAL Controls
mqtt_local_device_name: "stair-footerlights"
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}/set" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}/state" # Topic we will use to view status locally without HA
mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}" # Topic we will use to command this locally without HA
mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}" # Topic we will use to view status locally without HA
mqtt_local_device_command_ON: "ON"
mqtt_local_device_command_OFF: "OFF"
# MQTT REMOTE Controls
mqtt_remote_device1_name: "stair-mainlights-lower"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/set"
mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}/state"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}"
mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}"
#mqtt_remote_device2_name: "stair-mainlights-upper"
#mqtt_remote_device2_command_topic: "${mqtt_local_command_main_topic}/${mqtt_remote_device2_name}/relay3/set"
#mqtt_remote_device2_status_topic: "${mqtt_local_status_main_topic}/${mqtt_remote_device2_name}/relay3/state"
+30 -30
View File
@@ -94,13 +94,13 @@ substitutions:
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 hz" # PWM output Frequency. 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_mosfet_out: GPIO04
device_usr_button: GPIO12
device_fading_led: GPIO13
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
@@ -117,8 +117,8 @@ packages:
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
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}"
@@ -137,7 +137,7 @@ packages:
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_resetcount: !include common/include_resetcount_diag_sensors.yaml
##########################################################################################
# ESPHome CORE CONFIGURATION
@@ -191,7 +191,7 @@ esphome:
- lambda: 'id(booting) = false;'
# NEW: force indicator OFF after boot logic
- output.turn_off: green_led_out
# (Removed: output.turn_off: green_led_out)
# Only if you want to play with build flags...
# platformio_options:
@@ -239,6 +239,11 @@ globals:
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
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
@@ -400,7 +405,6 @@ button:
# Stop any pending scripts (and their delayed actions)
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- output.turn_off: green_led_out
- lambda: |-
// We are no longer ramping (up or down)
id(ramping_for_off) = false;
@@ -496,10 +500,11 @@ sensor:
unit_of_measurement: "%"
icon: mdi:percent
accuracy_decimals: 0
update_interval: 1s # consider 200ms if you want fewer updates
update_interval: 2s # consider 200ms if you want fewer updates
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
return cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f;
return cv.is_on() ? (float) (int)(cv.get_brightness() * 100.0f + 0.5f) : 0.0f;
on_value:
then:
- lambda: |-
@@ -526,18 +531,16 @@ sensor:
unit_of_measurement: "%"
icon: mdi:square-wave
accuracy_decimals: 1
update_interval: 1s
update_interval: 2s
lambda: |-
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
float pwm = powf(lin, gamma); // approx PWM duty after gamma
const float lin = cv.get_brightness();
float pwm = powf(lin, id(led_gamma_f));
if (pwm < 0.0f) pwm = 0.0f;
if (pwm > 1.0f) pwm = 1.0f;
return pwm * 100.0f;
##########################################################################################
# OUTPUT COMPONENT
# https://esphome.io/components/light/index.html
@@ -572,6 +575,7 @@ light:
# ON: publish state, track intent, arm watchdog if configured
on_turn_on:
- output.turn_on: green_led_out
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_ON}"
@@ -591,6 +595,7 @@ light:
# 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:
condition:
lambda: 'return !id(booting);'
@@ -611,13 +616,15 @@ light:
- lambda: |-
const float cap = id(max_brightness_pct) / 100.0f;
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on() && cv.get_brightness() > cap + 0.001f) {
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
}
if (!cv.is_on()) return;
const float b = cv.get_brightness();
if (b <= cap + 0.001f) return; // no call if already at/under cap
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
##########################################################################################
# NUMBER COMPONENT
@@ -779,7 +786,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
@@ -816,7 +822,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -831,7 +836,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at the floor cleanly.
- if:
condition:
@@ -874,7 +878,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -892,7 +895,6 @@ script:
id(is_ramping) = true;
id(ramping_for_off) = true;
- script.stop: ramp_on_script
- output.turn_on: green_led_out
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
@@ -913,7 +915,6 @@ script:
id: mosfet_leds
transition_length: 150ms
- delay: 150ms
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
id(ramping_for_off) = false;
@@ -973,5 +974,4 @@ script:
- light.turn_off:
id: mosfet_leds
transition_length: 0s
- output.turn_off: green_led_out # make sure LED is off if no ramp
# (Removed: output.turn_off: green_led_out) # make sure LED is off if no ramp
-248
View File
@@ -1,248 +0,0 @@
#############################################
#############################################
# Nissan Leaf 12V Battery Monitor
# Monitoring the status of a vehicle 12V battery with
# an esp8266 (D1 Mini). It will obviously only
# transmit when the vehicle is within wifi range.
# Voltage is measured with a resistor voltage divider
# into the analogue GPIO on the esp8266.
# https://zorruno.com/2022/vehicle-12v-battery-monitoring/
##############################################
#############################################
#############################################
# Variable Substitutions
#############################################
substitutions:
devicename: "esp-leafbat"
friendly_name: "Nissan Leaf 12V Battery Monitor"
description_comment: "Nissan Leaf 12V Battery Monitor (when home)"
api_key: !secret esp-leafbat_api_key #unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-leafbat_ota_pass #unfortunately you can't use substitutions in secrets names
wifi_ssid: !secret wifi_ssid
wifi_password: !secret wifi_password
fallback_ap_password: !secret fallback_ap_password
#Add these if we are giving it a static ip, or remove them in the Wifi section
#A static IP will speed things up slightly in that it doesn't have to negotiate DHCP
static_ip_address: !secret esp-leafbat_static_ip
static_ip_gateway: !secret esp-leafbat_gateway
static_ip_subnet: !secret esp-leafbat_subnet
mqtt_server: !secret mqtt_server
mqtt_username: !secret mqtt_username
mqtt_password: !secret mqtt_password
mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
mqtt_commandstopic: "viewroad-commands" #main topic for commands (ie sleep), call it what you like
#web_server_username: !secret web_server_username
#web_server_password: !secret web_server_password
update_time: 10s #update time for for general temp sensors etc
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${devicename}
friendly_name: ${friendly_name}
comment: ${description_comment} #appears on the esphome page in HA
min_version: 2024.6.0
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: d1_mini
framework:
version: latest #recommended, latest or dev
#############################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
#############################################
logger:
level: INFO #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)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
#uart:
# id: uart_output
# tx_pin: GPIO12
# rx_pin: GPIO13
# baud_rate: 9600
#############################################
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
encryption:
key: ${api_key}
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: esphome
password: ${ota_pass}
#############################################
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################
safe_mode:
#############################################
# Wifi Settings
# https://esphome.io/components/wifi.html
#
# Power Save mode (can reduce wifi reliability)
# NONE (least power saving, Default for ESP8266)
# LIGHT (Default for ESP32)
# HIGH (most power saving)
#############################################
wifi:
ssid: ${wifi_ssid}
password: ${wifi_password}
#power_save_mode: LIGHT #https://esphome.io/components/wifi.html#wifi-power-save-mode
manual_ip: #optional static IP address
static_ip: ${static_ip_address}
gateway: ${static_ip_gateway}
subnet: ${static_ip_subnet}
ap: #Details for fallback hotspot (captive portal) in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${devicename} fallback AP
password: ${fallback_ap_password}
ap_timeout: 30min #Time until it brings up fallback AP. default is 1min
#############################################
# Web Portal for display and monitoring
#############################################
web_server:
port: 80
#version: 2
#include_internal: true
#ota: false
#auth:
#username: ${web_server_username}
#password: ${web_server_password}
#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
mqtt:
broker: ${mqtt_server}
topic_prefix: ${mqtt_topic}/${devicename}
username: ${mqtt_username}
password: ${mqtt_password}
discovery: False # enable entity discovery (true is default, we don't want two HA Instances)
# Availability Topic
birth_message:
topic: ${mqtt_topic}/${devicename}/availability
payload: online
will_message:
topic: ${mqtt_topic}/${devicename}/availability
payload: offline
# A way to prevent deep sleep using MQTT command
on_message:
- topic: ${mqtt_commandstopic}/${devicename}/deepsleep
payload: 'OFF'
then:
- deep_sleep.prevent: deep_sleep_1
- topic: ${mqtt_commandstopic}/${devicename}/deepsleep
payload: 'ON'
then:
- deep_sleep.enter: deep_sleep_1
########################################
# Deep Sleep
# https://esphome.io/components/deep_sleep.html
########################################
deep_sleep:
run_duration: 20s
sleep_duration: 5min
id: deep_sleep_1
#time:
# - platform: homeassistant
# id: ha_time
# timezone: Pacific/Auckland
#text_sensor:
# - platform: template
# name: "Current time"
# id: current_time
# lambda: return id(ha_time).now().strftime("%H%M");
# lambda: |-
# char str[17];
# time_t currTime = id(ha_time).now().timestamp;
# strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
# return { str };
# update_interval: ${update_time}
#############################################
#############################################
# MAIN SENSORS
#############################################
#############################################
sensor:
#Quality of Wifi in dBm
- platform: wifi_signal
name: "WiFi Signal"
id: wifi_strength
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
#Analog sensor for voltage reading (A0)
- platform: uptime
name: "Uptime"
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
- platform: adc
pin: A0
name: "Battery Voltage"
id: battery_voltage
#mqtt_id: battery_voltage
update_interval: ${update_time}
retain: true #Retain this as you'll have no value between sleeps otherwise
# This set used a voltage divider with a 100k and a 4.7k R value
filters:
- multiply: 3.3 #D1 mini V divider, 3.3V -> 1V on esp8266
- calibrate_linear: #Read values with voltmeter and bench supply
- 0.72 -> 16.00
- 0.68 -> 15.00
- 0.63 -> 14.00
- 0.59 -> 13.00
- 0.55 -> 12.00
- 0.51 -> 11.00
- 0.45 -> 10.00
- 0.40 -> 09.00
- 0.36 -> 08.00
- 0.32 -> 07.00
- 0.28 -> 06.00
- 0.23 -> 05.00
# on_value:
# - logger.log:
# level: INFO
# format: "time,%s,battery,%.1f,wifi,%.1f"
# args: [ 'id(ha_time).now()', 'id(battery_voltage).state', 'id(wifi_strength).state' ]
# - uart.write:
# id: uart_output
# data: !lambda char buf[128];
# sprintf(buf, "%s", id(battery_voltage).state);
# std::string s = buf;
# return std::vector<unsigned char>( s.begin(), s.end() );
# - logger.log:
# level: INFO
# format: "time,%.1f,battery,%.1f,wifi,%.1f"
# args: [ 'id(sntp_time).now()', 'id(battery_voltage).state', 'id(wifi_strength).state' ]
+242
View File
@@ -0,0 +1,242 @@
##########################################################################################
##########################################################################################
# Nissan Leaf 12V Battery Monitor
# Monitoring the status of a vehicle 12V battery with
# an esp8266 (D1 Mini). It will obviously only
# transmit when the vehicle is within wifi range.
# Voltage is measured with a resistor voltage divider
# into the analogue GPIO on the esp8266.
# https://zorruno.com/2022/vehicle-12v-battery-monitoring/
##########################################################################################
##########################################################################################
##########################################################################################
# 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-leafbattery"
friendly_name: "Nissan Leaf 12V Battery Monitor"
description_comment: "Nissan Leaf 12V Battery Monitor (when home)"
device_area: "Garage" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Generic ESP8266.D1 Mini" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-leafbattery_ip
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# MQTT LOCAL Controls
mqtt_device_name: "leaf-battery"
mqtt_command_topic: "${mqtt_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
mqtt_status_topic: "${mqtt_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
wake_interval: "20s"
sleep_interval: "300s"
mqtt_fail_sleep: "1h" # If MQTT is unavailable after boot wait, sleep this long
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
# Comment some of these out where not needed, eg MQTT, Web portal, SNTP, General Sensors
##########################################################################################
packages:
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
common_wifi: !include
file: common/network_common.yaml
vars:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
common_api: !include
file: common/api_common.yaml
#file: common/api_common_noencryption.yaml
vars:
local_api_key: "${api_key}"
#### MQTT ####
common_mqtt: !include
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
#### 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
##########################################################################################
# ESPHome
# https://esphome.io/components/esphome.html
##########################################################################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
comment: ${description_comment} #appears on the esphome page in HA
min_version: 2024.6.0
on_boot:
priority: 600
then:
# Default: do NOT deep sleep until explicitly enabled via MQTT
- deep_sleep.prevent: deep_sleep_1
- logger.log: "Deep sleep prevented by default on boot."
# Fail-safe: wait up to 30s for MQTT; if not connected, sleep longer (backoff)
- wait_until:
condition:
mqtt.connected:
timeout: 30s
- if:
condition:
not:
mqtt.connected:
then:
- logger.log: "MQTT unreachable after 30s -> entering FALLBACK sleep for ${mqtt_fail_sleep}."
- deep_sleep.enter:
id: deep_sleep_1
sleep_duration: ${mqtt_fail_sleep} # override only this cycle
else:
- logger.log: "MQTT connected; awaiting '${mqtt_command_topic}/deepsleep' command."
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
##########################################################################################
esp8266:
board: d1_mini
framework:
version: latest #recommended, latest or dev
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
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)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
#############################################
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################
ota:
- platform: web_server # Uncomment if you want to be able to do OTA with the web interface
##########################################################################################
# MQTT COMMANDS
# This adds device-specific MQTT command triggers to the common MQTT configuration.
##########################################################################################
mqtt:
# Availability Topic
birth_message:
topic: ${mqtt_status_topic}/availability
payload: online
will_message:
topic: ${mqtt_status_topic}/availability
payload: offline
# Control deep sleep over MQTT
on_message:
- topic: ${mqtt_command_topic}/deepsleep
payload: "ON"
then:
- deep_sleep.allow: deep_sleep_1
- logger.log: "Deep sleep ALLOWED via MQTT. Device will sleep when run_duration elapses."
- topic: ${mqtt_command_topic}/deepsleep
payload: "OFF"
then:
- deep_sleep.prevent: deep_sleep_1
- logger.log: "Deep sleep PREVENTED via MQTT."
##########################################################################################
# Deep Sleep
# https://esphome.io/components/deep_sleep.html
##########################################################################################
deep_sleep:
id: deep_sleep_1
run_duration: ${wake_interval}
sleep_duration: ${sleep_interval}
##########################################################################################
# SENSOR COMPONENT
# https://esphome.io/components/sensor/
##########################################################################################
sensor:
- platform: adc
pin: A0
name: "Battery Voltage"
id: battery_voltage
update_interval: 4s
retain: true
filters:
- multiply: 3.3
- calibrate_linear:
- 0.72 -> 16.00
- 0.68 -> 15.00
- 0.63 -> 14.00
- 0.59 -> 13.00
- 0.55 -> 12.00
- 0.51 -> 11.00
- 0.45 -> 10.00
- 0.40 -> 09.00
- 0.36 -> 08.00
- 0.32 -> 07.00
- 0.28 -> 06.00
- 0.23 -> 05.00
- delta: 0.05 # publish immediately if change >= 0.05 V
- throttle: 2s # avoid bursts if noisy
on_value:
then:
- mqtt.publish:
topic: "${mqtt_status_topic}/batteryvoltage"
qos: 1
retain: true
payload: !lambda |-
char buf[10];
snprintf(buf, sizeof(buf), "%.2f", x);
return std::string(buf);
##########################################################################################
# INTERVAL COMPONENT
##########################################################################################
interval:
- interval: 60s
then:
- mqtt.publish:
topic: "${mqtt_status_topic}/batteryvoltage"
qos: 1
retain: true
payload: !lambda |-
const float v = id(battery_voltage).state; // current filtered state
char buf[10];
snprintf(buf, sizeof(buf), "%.2f", v);
return std::string(buf);
+30 -30
View File
@@ -83,13 +83,13 @@ substitutions:
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: "1000hz" # PWM output Frequency. 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 # Can't really see if there is an LED
device_status_led: GPIO02
device_mosfet_out: GPIO12
device_usr_button: GPIO04 # this is just a fake/safeish choice so it compiles with my generic yaml
device_fading_led: GPIO13 # this is just a fake/safeish choice so it compiles with my generic yaml
device_usr_button: GPIO04 # 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
@@ -124,8 +124,8 @@ packages:
#### 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_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
##########################################################################################
@@ -180,7 +180,7 @@ esphome:
- lambda: 'id(booting) = false;'
# NEW: force indicator OFF after boot logic
- output.turn_off: green_led_out
# (Removed: output.turn_off: green_led_out)
# Only if you want to play with build flags...
# platformio_options:
@@ -228,6 +228,11 @@ globals:
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
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
@@ -389,7 +394,6 @@ button:
# Stop any pending scripts (and their delayed actions)
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- output.turn_off: green_led_out
- lambda: |-
// We are no longer ramping (up or down)
id(ramping_for_off) = false;
@@ -485,10 +489,11 @@ sensor:
unit_of_measurement: "%"
icon: mdi:percent
accuracy_decimals: 0
update_interval: 1s # consider 200ms if you want fewer updates
update_interval: 2s # consider 200ms if you want fewer updates
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
return cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f;
return cv.is_on() ? (float) (int)(cv.get_brightness() * 100.0f + 0.5f) : 0.0f;
on_value:
then:
- lambda: |-
@@ -515,18 +520,16 @@ sensor:
unit_of_measurement: "%"
icon: mdi:square-wave
accuracy_decimals: 1
update_interval: 1s
update_interval: 2s
lambda: |-
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
float pwm = powf(lin, gamma); // approx PWM duty after gamma
const float lin = cv.get_brightness();
float pwm = powf(lin, id(led_gamma_f));
if (pwm < 0.0f) pwm = 0.0f;
if (pwm > 1.0f) pwm = 1.0f;
return pwm * 100.0f;
##########################################################################################
# OUTPUT COMPONENT
# https://esphome.io/components/light/index.html
@@ -561,6 +564,7 @@ light:
# ON: publish state, track intent, arm watchdog if configured
on_turn_on:
- output.turn_on: green_led_out
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_ON}"
@@ -580,6 +584,7 @@ light:
# 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:
condition:
lambda: 'return !id(booting);'
@@ -600,13 +605,15 @@ light:
- lambda: |-
const float cap = id(max_brightness_pct) / 100.0f;
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on() && cv.get_brightness() > cap + 0.001f) {
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
}
if (!cv.is_on()) return;
const float b = cv.get_brightness();
if (b <= cap + 0.001f) return; // no call if already at/under cap
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
##########################################################################################
# NUMBER COMPONENT
@@ -768,7 +775,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
@@ -805,7 +811,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -820,7 +825,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at the floor cleanly.
- if:
condition:
@@ -863,7 +867,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -881,7 +884,6 @@ script:
id(is_ramping) = true;
id(ramping_for_off) = true;
- script.stop: ramp_on_script
- output.turn_on: green_led_out
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
@@ -902,7 +904,6 @@ script:
id: mosfet_leds
transition_length: 150ms
- delay: 150ms
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
id(ramping_for_off) = false;
@@ -962,5 +963,4 @@ script:
- light.turn_off:
id: mosfet_leds
transition_length: 0s
- output.turn_off: green_led_out # make sure LED is off if no ramp
# (Removed: output.turn_off: green_led_out) # make sure LED is off if no ramp
+28 -27
View File
@@ -83,18 +83,18 @@ substitutions:
# 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.
update_interval: "30s" # 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: "1000hz" # PWM output Frequency. 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_mosfet_out: GPIO04
device_usr_button: GPIO12
device_fading_led: GPIO13
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
@@ -185,7 +185,7 @@ esphome:
- lambda: 'id(booting) = false;'
# NEW: force indicator OFF after boot logic
- output.turn_off: green_led_out
# (Removed: output.turn_off: green_led_out)
# Only if you want to play with build flags...
# platformio_options:
@@ -233,6 +233,11 @@ globals:
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
# Default Fading Up Time (Selectable and will be retained)
- id: ramp_up_ms # fade-in when turned ON
@@ -394,7 +399,6 @@ button:
# Stop any pending scripts (and their delayed actions)
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- output.turn_off: green_led_out
- lambda: |-
// We are no longer ramping (up or down)
id(ramping_for_off) = false;
@@ -490,10 +494,11 @@ sensor:
unit_of_measurement: "%"
icon: mdi:percent
accuracy_decimals: 0
update_interval: 1s # consider 200ms if you want fewer updates
update_interval: 2s # consider 200ms if you want fewer updates
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
return cv.is_on() ? (cv.get_brightness() * 100.0f) : 0.0f;
return cv.is_on() ? (float) (int)(cv.get_brightness() * 100.0f + 0.5f) : 0.0f;
on_value:
then:
- lambda: |-
@@ -520,18 +525,16 @@ sensor:
unit_of_measurement: "%"
icon: mdi:square-wave
accuracy_decimals: 1
update_interval: 1s
update_interval: 2s
lambda: |-
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
float pwm = powf(lin, gamma); // approx PWM duty after gamma
const float lin = cv.get_brightness();
float pwm = powf(lin, id(led_gamma_f));
if (pwm < 0.0f) pwm = 0.0f;
if (pwm > 1.0f) pwm = 1.0f;
return pwm * 100.0f;
##########################################################################################
# OUTPUT COMPONENT
# https://esphome.io/components/light/index.html
@@ -566,6 +569,7 @@ light:
# ON: publish state, track intent, arm watchdog if configured
on_turn_on:
- output.turn_on: green_led_out
- mqtt.publish:
topic: "${mqtt_local_status_topic}/light/state"
payload: "${mqtt_local_device_command_ON}"
@@ -585,6 +589,7 @@ light:
# 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:
condition:
lambda: 'return !id(booting);'
@@ -605,13 +610,15 @@ light:
- lambda: |-
const float cap = id(max_brightness_pct) / 100.0f;
const auto &cv = id(mosfet_leds).current_values;
if (cv.is_on() && cv.get_brightness() > cap + 0.001f) {
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
}
if (!cv.is_on()) return;
const float b = cv.get_brightness();
if (b <= cap + 0.001f) return; // no call if already at/under cap
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(cap);
call.set_transition_length(0);
call.perform();
##########################################################################################
# NUMBER COMPONENT
@@ -773,7 +780,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
@@ -810,7 +816,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -825,7 +830,6 @@ script:
then:
- lambda: 'id(is_ramping) = true;'
- script.stop: ramp_off_script
- output.turn_on: green_led_out
# Ensure we start at the floor cleanly.
- if:
condition:
@@ -868,7 +872,6 @@ script:
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);'
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
const auto &cv = id(mosfet_leds).current_values;
@@ -886,7 +889,6 @@ script:
id(is_ramping) = true;
id(ramping_for_off) = true;
- script.stop: ramp_on_script
- output.turn_on: green_led_out
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
@@ -907,7 +909,6 @@ script:
id: mosfet_leds
transition_length: 150ms
- delay: 150ms
- output.turn_off: green_led_out
- lambda: |-
id(is_ramping) = false;
id(ramping_for_off) = false;
@@ -967,4 +968,4 @@ script:
- light.turn_off:
id: mosfet_leds
transition_length: 0s
- output.turn_off: green_led_out # make sure LED is off if no ramp
# (Removed: output.turn_off: green_led_out) # make sure LED is off if no ramp
File diff suppressed because it is too large Load Diff
+65 -25
View File
@@ -30,18 +30,20 @@ substitutions:
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-loungesouthleftswitch_ip
#mqtt_local_command_main_topic: !secret mqtt_local_command_main_topic
#mqtt_local_status_main_topic: !secret mqtt_local_status_main_topic
mqtt_command_main_topic: !secret mqtt_command_main_topic
mqtt_status_main_topic: !secret mqtt_status_main_topic
# Device Settings
#relay_icon: "mdi:lightbulb-group"
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
# MQTT LOCAL Controls
#mqtt_device_name: "bedroom2-lights"
#mqtt_local_command_topic: "${mqtt_local_command_main_topic}/${mqtt_device_name}" # Topic we will use to command this locally without HA
#mqtt_local_status_topic: "${mqtt_local_status_main_topic}/${mqtt_device_name}" # Topic we will use to view status locally without HA
# MQTT REMOTE Controls
mqtt_remote_device_name: "garage-corridor-lights"
mqtt_remote_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device_name}"
mqtt_remote_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device_name}"
mqtt_remote_device_command_ON: "ON"
mqtt_remote_device_command_OFF: "OFF"
# Switch Naming
switch_1_name: "Wall Washer Left"
@@ -98,10 +100,6 @@ esphome:
project:
name: "${project_name}"
version: "${project_version}"
#on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_2
#########################################################################################
# ESP Platform and Framework
@@ -122,6 +120,12 @@ logger:
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
globals:
- id: relay2_change_origin_remote
type: bool
restore_value: no
initial_value: "false"
#########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
@@ -154,15 +158,38 @@ binary_sensor:
on_press:
- switch.toggle: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: ${switch_3_name}"
# on_press:
# - switch.toggle: Relay_3
text_sensor:
- platform: mqtt_subscribe
id: remote2_status
topic: "${mqtt_remote_status_topic}"
on_value:
then:
- if:
condition:
lambda: |-
return x == "${mqtt_remote_device_command_ON}";
then:
- if:
condition:
not:
switch.is_on: Relay_2
then:
- lambda: |-
id(relay2_change_origin_remote) = true;
- switch.turn_on: Relay_2
- if:
condition:
lambda: |-
return x == "${mqtt_remote_device_command_OFF}";
then:
- if:
condition:
switch.is_on: Relay_2
then:
- lambda: |-
id(relay2_change_origin_remote) = true;
- switch.turn_off: Relay_2
#########################################################################################
# SWITCH COMPONENT
@@ -178,11 +205,24 @@ switch:
name: "Relay 2: ${switch_2_name}"
pin: GPIO12
id: Relay_2
# KS-811-2 is a double only
# - platform: gpio
# name: "Relay 3: ${switch_3_name}"
# pin: GPIO14
# id: Relay_3
on_turn_on:
- if:
condition:
lambda: 'return !id(relay2_change_origin_remote);'
then:
- mqtt.publish:
topic: "${mqtt_remote_command_topic}"
payload: "${mqtt_remote_device_command_ON}"
- lambda: 'id(relay2_change_origin_remote) = false;'
on_turn_off:
- if:
condition:
lambda: 'return !id(relay2_change_origin_remote);'
then:
- mqtt.publish:
topic: "${mqtt_remote_command_topic}"
payload: "${mqtt_remote_device_command_OFF}"
- lambda: 'id(relay2_change_origin_remote) = false;'
+1 -1
View File
@@ -44,7 +44,7 @@ substitutions:
# MQTT REMOTE Controls
mqtt_remote_device1_name: "stair-footerlights"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}/set"
mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}"
#mqtt_remote_device1_status_topic: "${mqtt_status_main_topic}/${mqtt_remote_device1_name}/state"
mqtt_remote_device_command_ON: "ON"
mqtt_remote_device_command_OFF: "OFF"