a bunch of esphome improvements

This commit is contained in:
root
2025-06-12 17:40:40 +12:00
parent dbd1342020
commit 362b8119b4
26 changed files with 4221 additions and 501 deletions

View File

@@ -0,0 +1,656 @@
##########################################################################################
##########################################################################################
# DOWNSTAIRS BATHROOM HEATED TOWEL RAIL
# Controlled by a Sonoff Basic
#
# V2.0 2025-06-11 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
# V1.1 2025-04-12 Fixes to timers and offline modes
# V1.0 2025-02-14 Initial Version
#
# INSTRUCTIONS
# - It allows a heated towel rail device to work in a standalone operation
# - On startup, it will turn on for (startup_duration) hours then go into timer mode (this allows you to just turn it on to get some heat immediately)
# - The timer has a morning and evening time (but no weekday/weekend setting)
# - Default values are 5am-7am and 9pm-Midnight (as this suits our use case)
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
# - If on a network and there is a MQTT server, you can set the 4 on/off times via MQTT (See below commands)
# - You can set 4 modes ON/OFF/TIMER/STARTUP via MQTT. That way, you can set to STARTUP for a short boost
# - Any new timer times set via MQTT will be remembered though a reboot
# - On a reboot, the device will always turn on for the Startup Duration (STARTUP mode, default 2 hours)
# - TIMER mode will always be switched on after startup mode is complete
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. Note there is no weekday/weekend setting
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for
# mqtt_timer_topic/operation/ON : Device permanently on
# mqtt_timer_topic/operation/OFF : Device permanently off
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
# mqtt_timer_topic/operation/BOOST : Turn on for (startup_duration) hours then BOOST (also on startup)
#
##########################################################################################
##########################################################################################
##########################################################################################
# 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-downstbathtowelrail"
friendly_name: "Downstairs Bathroom Towelrail"
description_comment: "Sonoff Basic controlling ON/OFF/Timer for the Heated Towel Rail in the Downstairs Bathroom"
device_area: "Downstairs Bathroom" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v2.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
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-downstbathtowelrail_ip
# Device Settings
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
# Timer Settings
mqtt_timer_topic: "viewroad-commands/downstbath-towelrail" # Topics you will use to change stuff
startup_duration: "120" # Minutes to stay ON in STARTUP mode before reverting to TIMER
morning_on_default: "420" # Default in minutes from midnight. Default 07:00 => 420
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
evening_off_default: "1320" # Default in minutes from midnight. Default 22:00 => 1320 => 1440 is midnight
#############################################
# Included Common Packages
# https://esphome.io/components/esphome.html
#############################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: "${api_key}"
# common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: "${friendly_name}"
local_update_interval: "${update_interval}"
#############################################
# 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
area: "${device_area}"
on_boot:
priority: 900 # High priority to run after globals are initialized
then:
- lambda: |-
// 1) Figure out the current time in "seconds from midnight"
// using SNTP if available, otherwise current_mins * 60.
bool have_sntp = id(sntp_time).now().is_valid();
int current_time_s = 0;
if (have_sntp) {
auto now = id(sntp_time).now();
current_time_s = now.hour * 3600 + now.minute * 60 + now.second;
} else {
// current_mins is in minutes; convert to seconds
current_time_s = id(current_mins) * 60;
}
// 2) Compare with the last boot time
int diff = current_time_s - id(last_boot_time_s);
// If within 30 seconds, increment boot_count; otherwise reset to 1
if (diff >= 0 && diff <= 30) {
id(boot_count)++;
} else {
id(boot_count) = 1;
}
// Update stored last boot time
id(last_boot_time_s) = current_time_s;
// 3) If we've booted 4+ times in 20s => force ON mode
if (id(boot_count) >= 4) {
id(operation_mode) = 1; // ON
ESP_LOGI("power_cycle", "Detected 4 power cycles in 20s => Forcing ON mode");
} else {
// Otherwise do your normal startup logic:
id(operation_mode) = 3; // on_boot -> sets operation_mode = 3 (STARTUP)
id(startup_timer) = 0; // and reset startup_timer = 0 (for time sync if no sntp)
ESP_LOGI("power_cycle", "Boot count=%d => STARTUP mode", id(boot_count));
}
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m # The original sonoff basic
restore_from_flash: true # restore some values on reboot
preferences:
flash_write_interval: 5min
mdns:
disabled: false
#############################################
# 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
#############################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
#############################################
globals:
# Tracks the time (in seconds from midnight) at the previous boot
- id: last_boot_time_s
type: int
restore_value: true
initial_value: "0"
# Counts how many consecutive boots have occurred (within X seconds)
- id: boot_count
type: int
restore_value: true
initial_value: "0"
# Morning On time (minutes from midnight),
- id: morning_on
type: int
restore_value: true
initial_value: "${morning_on_default}"
# Morning Off time (minutes from midnight),
- id: morning_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening On time (minutes from midnight),
- id: evening_on
type: int
restore_value: true
initial_value: "${morning_off_default}"
# Evening Off time (minutes from midnight),
- id: evening_off
type: int
restore_value: true
initial_value: "${morning_off_default}"
####################################################
# operation_mode:
# 0 = OFF
# 1 = ON
# 2 = TIMER
# 3 = STARTUP
####################################################
- id: operation_mode
type: int
restore_value: false
initial_value: "3"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
# Not restored, so it resets each boot.
####################################################
- id: current_mins
type: int
restore_value: false
initial_value: "720" # 720 is 12:00 Noon
####################################################
# startup_timer: counts minutes in STARTUP mode
# After 'startup_duration' minutes, revert to TIMER.
# Not restored, so each boot starts fresh at 0.
####################################################
- id: startup_timer
type: int
restore_value: false
initial_value: "0"
#############################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
#############################################
text_sensor:
############################
# MQTT Subscriptions
############################
####################################################
# Subscribe to the Morning On time, format "HH:MM"
# We check x.size() == 5 and x[2] == ':',
# then parse x.substr(0,2) and x.substr(3,2)
# std::string uses 'substr', not 'substring'.
####################################################
- platform: mqtt_subscribe
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
internal: True
on_value:
then:
- lambda: |-
// Expect "HH:MM" => total length = 5, with ':'
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str()); // "HH"
int minute = atoi(x.substr(3, 2).c_str()); // "MM"
id(morning_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
####################################################
# Morning Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(morning_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Morning Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
####################################################
# Evening On time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_on) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening On: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
####################################################
# Evening Off time => "HH:MM"
####################################################
- platform: mqtt_subscribe
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
if (x.size() == 5 && x[2] == ':') {
int hour = atoi(x.substr(0, 2).c_str());
int minute = atoi(x.substr(3, 2).c_str());
id(evening_off) = hour * 60 + minute;
ESP_LOGI("timer","Received new Evening Off: %02d:%02d", hour, minute);
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
####################################################
# Subscribe to operation mode:
# OFF, ON, TIMER, STARTUP
# We do case-insensitive compare using strcasecmp
# (Requires <strings.h> typically included in ESPHome)
####################################################
- platform: mqtt_subscribe
name: "Operation Mode Setting"
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation" # STARTUP,ON,OFF,TIMER
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
/*
* In standard C++ (ESPHome), no 'equalsIgnoreCase()'.
* We use 'strcasecmp' for case-insensitive compare.
* Returns 0 if they match ignoring case.
*/
if (strcasecmp(x.c_str(), "TIMER") == 0) {
id(operation_mode) = 2;
ESP_LOGI("timer","Operation mode set to TIMER");
} else if (strcasecmp(x.c_str(), "ON") == 0) {
id(operation_mode) = 1;
ESP_LOGI("timer","Operation mode set to ON");
} else if (strcasecmp(x.c_str(), "OFF") == 0) {
id(operation_mode) = 0;
ESP_LOGI("timer","Operation mode set to OFF");
} else if (strcasecmp(x.c_str(), "STARTUP") == 0) {
id(operation_mode) = 3;
id(startup_timer) = 0; // Reset the startup timer to zero
ESP_LOGI("timer","Operation mode set to STARTUP");
} else {
ESP_LOGW("timer","Invalid operation mode: %s", x.c_str());
}
######################################################
# Expose the current operation mode (OFF, ON, TIMER, STARTUP)
######################################################
- platform: template
name: "Operation Mode State"
lambda: |-
// 0=OFF, 1=ON, 2=TIMER, 3=STARTUP
switch (id(operation_mode)) {
case 0: return {"OFF"};
case 1: return {"ON"};
case 2: return {"TIMER"};
case 3: return {"STARTUP"};
default: return {"UNKNOWN"};
}
update_interval: 5s
######################################################
# Expose the "Morning On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning On Time"
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Morning Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Morning Off Time"
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening On" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening On Time"
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
######################################################
# Expose the "Evening Off" time as a text (HH:MM)
######################################################
- platform: template
name: "Timeclock: Evening Off Time"
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
update_interval: "${update_interval}"
#############################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
#############################################
sensor:
- platform: template
name: "Mins from Midnight"
unit_of_measurement: "mins"
accuracy_decimals: 0
update_interval: "${update_interval}"
internal: True
lambda: |-
return id(current_mins);
# A value in mins if a timer is running showing how many mins left
- platform: template
name: "Timer Minutes Remaining"
id: timer_minutes_remaining
unit_of_measurement: "Mins"
update_interval: 5s
accuracy_decimals: "0"
lambda: |-
// always zero if relay is off
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (startup)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
rem = ${startup_duration} - id(startup_timer);
}
// never return negative
return rem > 0 ? rem : 0;
####################################################
# Relay Switch (Sonoff Basic Relay on GPIO12)
####################################################
switch:
- platform: gpio
name: "Towel Rail Power"
pin: GPIO12
id: relay
restore_mode: RESTORE_DEFAULT_OFF
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
#################################################################################################
button:
- platform: template
name: "Startup: ${startup_duration} Minutes"
id: startup_button
icon: "mdi:play-circle-outline" # optional, pick any MaterialDesign icon you like
on_press:
then:
# 1) set the mode to STARTUP (3)
- lambda: |-
id(startup_timer) = 0; // Reset the startup timer to zero
id(operation_mode) = 3; // Set to STARTUP
ESP_LOGD("main", "operation_mode set to %d via STARTUP button", id(operation_mode));
# 2) turn on the relay switch
- switch.turn_on:
id: relay
#################################################################################################
# SELECT COMPONENT
# https://esphome.io/components/select/index.html
#################################################################################################
select:
- platform: template
name: "Operation Mode"
id: operation_mode_select
update_interval: 5s # poll every 5 s for external changes
options:
- "OFF"
- "ON"
- "TIMER"
- "STARTUP"
# Getter: maps your integer into one of the four strings
lambda: |-
switch (id(operation_mode)) {
case 1: return std::string("ON");
case 2: return std::string("TIMER");
case 3: return std::string("STARTUP");
default: return std::string("OFF");
}
# set_action: called when you pick an option in HA
set_action:
- lambda: |-
if (x == "OFF") id(operation_mode) = 0;
else if (x == "ON") id(operation_mode) = 1;
else if (x == "TIMER") id(operation_mode) = 2;
else {
id(startup_timer) = 0; // Reset the startup timer to zero
id(operation_mode) = 3; /* STARTUP */
}
ESP_LOGD("main", "operation_mode set to %d", id(operation_mode));
####################################################
# Check every minute to decide relay state
####################################################
interval:
- interval: "1min" # Must be 1min as this is used to calculate times
then:
- lambda: |-
// Do we have correct time from SNTP? If not...
if (!id(time_sync).has_state()) {
// Set minutes since midnight
id(current_mins) = id(current_mins) +1 ;
// wrap around at 1440 => next day
if (id(current_mins) >= 1440) {
id(current_mins) = 0;
}
// If we do have proper SNMP time...
} else {
// Use real time from SNTP
auto now = id(sntp_time).now();
id(current_mins) = now.hour * 60 + now.minute;
}
// operation_mode:
// 0 = OFF
// 1 = ON
// 2 = TIMER
// 3 = STARTUP
int mode = id(operation_mode);
//////////////////////////////////////////////////
// STARTUP MODE: Relay ON for 'startup_duration'
// minutes, then automatically revert to TIMER.
//////////////////////////////////////////////////
if (mode == 3) {
id(startup_timer) = id(startup_timer) + 1 ; // works as long as update_interval in seconds
// Compare with the substitution startup_duration
if (id(startup_timer) < (int) ${startup_duration}) {
// Still within the STARTUP period => turn relay on
id(relay).turn_on();
} else {
// After 'startup_duration' minutes => switch to TIMER
id(operation_mode) = 2;
id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
}
// Skip the rest of the logic
ESP_LOGI("startup_timer", "startup_timer=%d", id(startup_timer));
return;
}
//////////////////////////////////////////////////
// OFF MODE => always off
//////////////////////////////////////////////////
if (mode == 0) {
id(relay).turn_off();
return;
}
//////////////////////////////////////////////////
// ON MODE => always on
//////////////////////////////////////////////////
if (mode == 1) {
id(relay).turn_on();
return;
}
//////////////////////////////////////////////////
// TIMER MODE => follow morning/evening schedule
// using SNTP if valid, else current_mins
//////////////////////////////////////////////////
if (mode == 2)
{
bool should_on = false;
// Check morning window
// Example: morning_on=360 => 06:00, morning_off=480 => 08:00
// If current_mins in [360..480), should_on = true
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off) )
{
should_on = true;
}
// Check evening window
// Example: evening_on=1260 => 21:00, evening_off=1440 => midnight
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off) )
{
should_on = true;
}
// Final relay state based on schedule
if (should_on) {
id(relay).turn_on();
} else {
id(relay).turn_off();
}
}

View File

@@ -0,0 +1,205 @@
# This is a generic starter yaml file for
# espHome, by zorruno 2024
# <DEVICENAME> would be something like loungefancontroller
# yaml file would then be esp-loungefancontroller.yaml
#############################################
#############################################
# TITLE
# Details of what the device is plus
# any relevant web links
#############################################
#############################################
#############################################
# SECRETS
# Secrets file is usually at config/secrets.yaml
# or config/esphome/secrets.yaml
# depending on your setup with HA
#############################################
# SECRETS FILE needs these specific secrets:
# esp-<DEVICENAME>_api_key
# esp-<DEVICENAME>_ota_pass
# esp-<DEVICENAME>_static_ip (Optional)
# esp-<DEVICENAME>_gateway (Optional)
# esp-<DEVICENAME>_subnet (Optional)
#
# SECRETS FILE needs these generic Secrets:
# wifi_ssid
# wifi_password
# fallback_ap_password
# mqtt_server
# mqtt_username
# mqtt_password
# web_server_username (Optional)
# web_server_password (Optional)
#############################################
#############################################
# VARIABLE SUBSTITUTIONS
# Give the device a useful name & description here
# and change values accordingly.
#############################################
substitutions:
device_name: esp-<DEVICENAME>
friendly_name: "Device friendly name"
description_comment: "A Description of this device"
# if NOT using a secrets file, just replace these with the passwords etc (in quotes)
api_key: !secret esp-<DEVICENAME>_api_key # unfortunately you can't use substitutions in secrets names
ota_pass: !secret esp-<DEVICENAME>_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
#static_ip_address: !secret esp-<DEVICENAME>_static_ip
#static_ip_gateway: !secret esp-<DEVICENAME>_gateway
#static_ip_subnet: !secret esp-<DEVICENAME>_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
web_server_username: !secret web_server_username
web_server_password: !secret web_server_password
update_time: 60s # update time for for general temp sensors etc
#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
name: ${device_name}
friendly_name: ${friendly_name} # appears as main name on the esphome page in HA
comment: ${description_comment} # appears as comment on the esphome page in HA
#min_version: 2024.6.0
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp32:
board: <ESP-DEVICE>
framework:
type: esp-idf # Suggest using the ESP-IDF Framework. Changing from 'arduino' to 'esp-idf' needs a cabled flash to correct partitions
version: recommended # recommended, latest or dev
#############################################
# ESPHome Logging Enabl
# 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}
#############################################
# 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 in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
ssid: ${device_name} 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
# Turning this off is probably a good idea to save resources.
# https://esphome.io/components/web_server.html
#############################################
#web_server:
# port: 80
# auth:
# username: ${web_server_username} #probably a good idea to secure it
# 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}/${device_name}
username: ${mqtt_username}
password: ${mqtt_password}
#############################################
# Time Component
# https://esphome.io/components/time/index.html#time-component
# Sync with HA Time. Probably not really necessary.
#############################################
#time:
# - platform: homeassistant
# id: homeassistant_time
#############################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
#############################################
text_sensor:
- platform: version
name: ${friendly_name} Version
- platform: wifi_info
ip_address:
name: ${friendly_name} IP Address
#############################################
# General Sensors
# https://esphome.io/components/sensor/index.html
#############################################
sensor:
- platform: uptime # Uptime for this device
name: ${friendly_name} Uptime
update_interval: ${update_time}
- platform: wifi_signal # Wifi Strength
name: ${friendly_name} Wifi Signal
update_interval: ${update_time}
#############################################
# Binary Sensors
# https://esphome.io/components/binary_sensor/index.html
#############################################
#binary_sensor:

View File

@@ -0,0 +1,154 @@
#############################################
#############################################
# LAUNDRY MAIN LIGHTSWITCH
# V1.0 2025-05-30 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundrylights"
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Laundry" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Laundry Main Lightswitch (3)"
description_comment: "Laundry Main Lightswitch using a Zemismart KS-811 Triple Push Button. Laundry Lights (1), Spare (2), Laundry Power Signal (3)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-laundrylights_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-laundrylights_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundrylights_ip
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
#############################################
# Included Common Packages
# https://esphome.io/components/esphome.html
#############################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_static_ip_address: ${static_ip_address}
local_ota_pass: ${ota_pass}
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: ${api_key}
# common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: ${friendly_name}
local_update_interval: ${update_interval}
#############################################
# 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
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: Relay_3
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# 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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: Light Switch (Laundry)"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: Spare"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: Laundry Power Enable"
on_press:
- switch.toggle: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: Laundry Lights"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: Spare"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: Laundry Power Enable Signal"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,154 @@
#############################################
#############################################
# LAUNDRY MAIN LIGHTSWITCH
# V1.0 2025-05-30 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundrylights"
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Laundry" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Main Bathroom Lightswitch (3)"
description_comment: "Main Bathroom Lightswitch using a Zemismart KS-811 Triple Push Button. Main Lights (1), Shower Lights (2), Cabinet Lights (3)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-mainbathlights_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-mainbathlights_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-mainbathlights_ip
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
#############################################
# Included Common Packages
# https://esphome.io/components/esphome.html
#############################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_static_ip_address: ${static_ip_address}
local_ota_pass: ${ota_pass}
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: ${api_key}
# common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: ${friendly_name}
local_update_interval: ${update_interval}
#############################################
# 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
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# 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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: Main Lights"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: Shower Lights"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: Cabinet Lights"
on_press:
- switch.toggle: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: Main Lights"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: Shower Lights"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: Cabinet Lights"
pin: GPIO14
id: Relay_3

View File

@@ -0,0 +1,157 @@
#############################################
#############################################
# LAUNDRY MAIN LIGHTSWITCH
# V2.0 2025-06-01 Was initially a 3 button switch, now 2
# V1.0 2025-05-30 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-laundrylights2"
project_name: "Zemismart Technologies.KS-811 Double" # Project Details
project_version: "v2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Laundry" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Laundry Main Lightswitch (2)"
description_comment: "Laundry Main Lightswitch using a Zemismart KS-811 Double Push Button. Laundry Lights (1), Laundry Power Signal (2)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-laundrylights_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-laundrylights_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-laundrylights2_ip
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
#############################################
# Included Common Packages
# https://esphome.io/components/esphome.html
#############################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
local_static_ip_address: ${static_ip_address}
local_ota_pass: ${ota_pass}
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: ${api_key}
# common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: ${friendly_name}
local_update_interval: ${update_interval}
#############################################
# 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
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
on_boot:
priority: 200
then:
- switch.turn_on: Relay_2
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# 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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: Light Switch (Laundry)"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: Laundry Power Enable"
on_press:
- switch.toggle: Relay_2
# ONLY ON A KS-811-3
# - platform: gpio
# pin:
# number: GPIO4
# mode: INPUT
# inverted: True
# name: "Button 3: "
# on_press:
# - switch.toggle: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: Laundry Lights"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: Laundry Power Enable Signal"
pin: GPIO12
id: Relay_2
# ONLY ON A KS-811-3
# - platform: gpio
# name: "Relay 3: "
# pin: GPIO14
# id: Relay_3

View File

@@ -0,0 +1,157 @@
#############################################
#############################################
# LAUNDRY MAIN LIGHTSWITCH
# V1.0 2025-05-30 Initial Version
#############################################
# Zemismart KS-811 Triple push button
# pinout/schematic https://community.home-assistant.io/t/zemismart-ks-811-working-with-esphome/
#
# NOTES
# -
#
#############################################
#############################################
#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#############################################
substitutions:
device_name: "esp-mainbathlights"
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v1" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
entity_prefix: "Laundry" # Simple device name where we want to prefix a sensor or switch, eg "Load" Current.
friendly_name: "Main Bathroom Lightswitch (3)"
description_comment: "Main Bathroom Lightswitch using a Zemismart KS-811 Triple Push Button. Main Lights (1), Shower Lights (2), Cabinet Lights (3)"
device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
api_key: !secret esp-mainbathlights_api_key # unfortunately you can't use substitutions inside secrets names
ota_pass: !secret esp-mainbathlights_ota_pass # unfortunately you can't use substitutions inside secrets names
static_ip_address: !secret esp-mainbathlights_ip
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
#############################################
# Included Common Packages
# https://esphome.io/components/esphome.html
#############################################
packages:
common_wifi: !include
file: common/network_common.yaml
vars:
device_name: ${device_name}
local_static_ip_address: ${static_ip_address}
local_ota_pass: ${ota_pass}
common_api: !include
file: common/api_common.yaml
vars:
local_api_key: ${api_key}
# common_webportal: !include
# file: common/webportal_common.yaml
common_mqtt: !include
file: common/mqtt_common.yaml
vars:
device_name: ${device_name}
common_sntp: !include
file: common/sntp_common.yaml
common_general_sensors: !include
file: common/sensors_common.yaml
vars:
local_friendly_name: ${friendly_name}
local_update_interval: ${update_interval}
#############################################
# 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
area: ${device_area}
project:
name: "${project_name}"
version: "${project_version}"
# on_boot:
# priority: 200
# then:
# - switch.turn_on: Relay_3
#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
board: esp01_1m
early_pin_init: False # Initialise pins early to known values. Recommended false where switches are involved. Defaults to True.
board_flash_mode: dout # Default is dout
#############################################
# 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
#############################################
# STATUS LED
# https://esphome.io/components/status_led.html
#############################################
status_led:
pin:
number: GPIO2
inverted: yes
#############################################
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
#############################################
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT
inverted: True
name: "Button 1: Main Lights"
on_press:
- switch.toggle: Relay_1
- platform: gpio
pin:
number: GPIO05
mode: INPUT
inverted: True
name: "Button 2: Shower Lights"
on_press:
- switch.toggle: Relay_2
- platform: gpio
pin:
number: GPIO4
mode: INPUT
inverted: True
name: "Button 3: Cabinet Lights"
on_press:
- switch.toggle: Relay_3
#############################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#############################################
switch:
- platform: gpio
name: "Relay 1: Main Lights"
pin: GPIO13
id: Relay_1
- platform: gpio
name: "Relay 2: Shower Lights"
pin: GPIO12
id: Relay_2
- platform: gpio
name: "Relay 3: Cabinet Lights"
pin: GPIO14
id: Relay_3