yaml tidyups for esphome

This commit is contained in:
root
2026-02-05 21:28:48 +13:00
parent c104187d62
commit d7884770fe
15 changed files with 633 additions and 539 deletions
+124 -72
View File
@@ -17,14 +17,14 @@
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
substitutions:
# Device Naming
# Device Naming
device_name: "esp-loungemiddleswitch"
friendly_name: "Lounge Middle Lightswitch (3)"
description_comment: "Lounge Main Lightswitch using a Zemismart KS-811 Triple Push Button. Downlights South (1), Downlights Middle (2), Downlights North (3)"
device_area: "Lounge" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
device_area: "Lounge" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_name: "Zemismart Technologies.KS-811 Triple" # Project Details
project_version: "v3.5" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords & Secrets
@@ -76,7 +76,7 @@ packages:
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
#### WEB PORTAL ####
#common_webportal: !include common/webportal_common.yaml
#### SNTP (Only use if you want/need accurate timeclocks) ####
@@ -101,22 +101,35 @@ esphome:
name: "${project_name}"
version: "${project_version}"
#########################################################################################
# Home Assistant API
#########################################################################################
api:
on_client_connected:
then:
- logger.log:
level: INFO
format: "API connected: applying override (override=%s)"
args: ['id(ha_loss_override).state ? "true" : "false"']
format: "API connected: override=%s, ha_loss_test=%s"
args:
- 'id(ha_loss_override).state ? "true" : "false"'
- 'id(ha_loss_test).state ? "true" : "false"'
- if:
condition:
switch.is_on: ha_loss_override
switch.is_on: ha_loss_test
then:
- switch.turn_on: Relay_1
- switch.turn_on: Relay_2
- logger.log:
level: WARN
format: "HA Loss Test is ON: not applying relay override on connect"
else:
- switch.turn_off: Relay_1
- switch.turn_off: Relay_2
- if:
condition:
switch.is_on: ha_loss_override
then:
- switch.turn_on: Relay_1
- switch.turn_on: Relay_2
else:
- switch.turn_off: Relay_1
- switch.turn_off: Relay_2
on_client_disconnected:
then:
@@ -133,13 +146,13 @@ esp8266:
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)
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
@@ -164,15 +177,13 @@ binary_sensor:
number: GPIO16
mode: INPUT
inverted: True
use_interrupt: false # This pin doesn't support interrupts so use polling. Only supresses a warning.
use_interrupt: false # This pin doesn't support interrupts so use polling. Only suppresses a warning.
name: "Button 1: ${switch_1_name}"
on_multi_click:
# --- Single short click ---
# Online (and override ON): use HA's last scene to decide.
# If last != "All Off" → send "All Off"
# If last == "All Off" → send "Bright"
# Offline (and override ON): toggle physical Relay_1
# Online (and override ON, and NOT test mode): use HA's last scene to decide.
# Offline (override ON): toggle physical Relay_1
# Test mode (override ON): behave like offline even if HA is connected
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
@@ -182,6 +193,8 @@ binary_sensor:
and:
- switch.is_on: ha_loss_override
- api.connected:
- not:
switch.is_on: ha_loss_test
then:
- if:
condition:
@@ -197,7 +210,7 @@ binary_sensor:
scene_name: "All Off"
- logger.log:
level: INFO
format: "BTN1: Single click (online) Last scene was not 'All Off' Set 'All Off'"
format: "BTN1: Single click (online) -> Last scene was not 'All Off' -> Set 'All Off'"
else:
- homeassistant.service:
service: script.lounge_set_scene
@@ -205,15 +218,16 @@ binary_sensor:
scene_name: "Bright"
- logger.log:
level: INFO
format: "BTN1: Single click (online) Last scene was 'All Off' Set 'Bright'"
format: "BTN1: Single click (online) -> Last scene was 'All Off' -> Set 'Bright'"
else:
# OFFLINE path: only act if override ON; toggle physical relay
- if:
condition:
and:
- switch.is_on: ha_loss_override
- not:
api.connected:
- or:
- not:
api.connected:
- switch.is_on: ha_loss_test
then:
- if:
condition:
@@ -222,18 +236,18 @@ binary_sensor:
- switch.turn_off: Relay_1
- logger.log:
level: INFO
format: "BTN1: Single click (offline) → Relay_1 OFF"
format: "BTN1: Single click (offline/test) -> Relay_1 OFF"
else:
- switch.turn_on: Relay_1
- logger.log:
level: INFO
format: "BTN1: Single click (offline) → Relay_1 ON"
format: "BTN1: Single click (offline/test) -> Relay_1 ON"
else:
- logger.log:
level: INFO
format: "BTN1: Single click ignored (override OFF)"
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
# GPIO05 for second button (only for KS-811-2 Double or -3 Triple)
- platform: gpio
pin:
number: GPIO05
@@ -242,10 +256,8 @@ binary_sensor:
name: "Button 2: ${switch_2_name}"
on_multi_click:
# --- Single short click ---
# Online (and override ON): use HA's last scene to decide.
# If last != "All Off" → send "All Off"
# If last == "All Off" → send "Bright"
# Offline (and override ON): toggle physical Relay_1
# Online (override ON, NOT test mode): set scene
# Offline/test (override ON): toggle Relay_2
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
@@ -255,22 +267,25 @@ binary_sensor:
and:
- switch.is_on: ha_loss_override
- api.connected:
- not:
switch.is_on: ha_loss_test
then:
- homeassistant.service:
service: script.lounge_set_scene
data:
scene_name: "TV General"
- logger.log:
level: INFO
format: "BTN2: Single click (online) Set 'TV General' Scene"
- homeassistant.service:
service: script.lounge_set_scene
data:
scene_name: "Movie"
- logger.log:
level: INFO
format: "BTN2: Single click (online) -> Set 'Movie' Scene"
else:
# OFFLINE path: only act if override ON; toggle physical relay
- if:
condition:
and:
- switch.is_on: ha_loss_override
- not:
api.connected:
- or:
- not:
api.connected:
- switch.is_on: ha_loss_test
then:
- if:
condition:
@@ -279,18 +294,18 @@ binary_sensor:
- switch.turn_off: Relay_2
- logger.log:
level: INFO
format: "BTN2: Single click (offline) → Relay_2 OFF"
format: "BTN2: Single click (offline/test) -> Relay_2 OFF"
else:
- switch.turn_on: Relay_1
- switch.turn_on: Relay_2
- logger.log:
level: INFO
format: "BTN2: Single click (offline) → Relay_2 ON"
format: "BTN2: Single click (offline/test) -> Relay_2 ON"
else:
- logger.log:
level: INFO
format: "BTN: Single click ignored"
format: "BTN2: Single click ignored (override OFF)"
# GPIO04 for third button (only for KS-811-3 Triple)
# GPIO04 for third button (only for KS-811-3 Triple)
- platform: gpio
pin:
number: GPIO4
@@ -299,10 +314,8 @@ binary_sensor:
name: "Button 3: ${switch_3_name}"
on_multi_click:
# --- Single short click ---
# Online (and override ON): use HA's last scene to decide.
# If last != "All Off" → send "All Off"
# If last == "All Off" → send "Bright"
# Offline (and override ON): toggle physical Relay_1
# Online (override ON, NOT test mode): set scene
# Offline/test (override ON): toggle Relay_3
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
@@ -312,50 +325,51 @@ binary_sensor:
and:
- switch.is_on: ha_loss_override
- api.connected:
- not:
switch.is_on: ha_loss_test
then:
- homeassistant.service:
service: script.lounge_set_scene
data:
scene_name: "Reading On/Off"
- logger.log:
level: INFO
format: "BTN2: Single click (online) Set 'Reaading On/Off' Scene"
- homeassistant.service:
service: script.lounge_set_scene
data:
scene_name: "Reading On/Off"
- logger.log:
level: INFO
format: "BTN3: Single click (online) -> Set 'Reading On/Off' Scene"
else:
# OFFLINE path: only act if override ON; toggle physical relay
- if:
condition:
and:
- switch.is_on: ha_loss_override
- not:
api.connected:
- or:
- not:
api.connected:
- switch.is_on: ha_loss_test
then:
- if:
condition:
lambda: 'return id(Relay_2).state;'
lambda: 'return id(Relay_3).state;'
then:
- switch.turn_off: Relay_2
- switch.turn_off: Relay_3
- logger.log:
level: INFO
format: "BTN2: Single click (offline) → Relay_2 OFF"
format: "BTN3: Single click (offline/test) -> Relay_3 OFF"
else:
- switch.turn_on: Relay_1
- switch.turn_on: Relay_3
- logger.log:
level: INFO
format: "BTN2: Single click (offline) → Relay_2 ON"
format: "BTN3: Single click (offline/test) -> Relay_3 ON"
else:
- logger.log:
level: INFO
format: "BTN: Single click ignored"
format: "BTN3: Single click ignored (override OFF)"
#########################################################################################
# TEXT SENSOR COMPONENT
#
#########################################################################################
text_sensor:
- platform: homeassistant
id: ha_last_scene
entity_id: input_text.lounge_last_scene
# logging: helpful when testing
on_value:
- logger.log:
level: DEBUG
@@ -367,7 +381,7 @@ text_sensor:
# https://esphome.io/components/switch/
#########################################################################################
switch:
# GPIO13 for KS-811 first button UNLESS it is KS-811-1 then it is GIPO12
# GPIO13 for KS-811 first button UNLESS it is KS-811-1 then it is GPIO12
- platform: gpio
name: "Relay 1: ${switch_1_name}"
pin: GPIO13
@@ -392,5 +406,43 @@ switch:
name: "HA Loss manual override"
id: ha_loss_override
optimistic: true
restore_mode: RESTORE_DEFAULT_ON
restore_mode: ALWAYS_ON
# HA Loss Test (default OFF): when ON, force "offline behavior" even if HA is connected
- platform: template
name: "HA Loss Test"
id: ha_loss_test
optimistic: true
restore_mode: ALWAYS_OFF
on_turn_on:
- logger.log:
level: WARN
format: "HA Loss Test turned ON: forcing offline/test behavior for buttons"
on_turn_off:
- logger.log:
level: INFO
format: "HA Loss Test turned OFF: forcing Relay_1 and Relay_2 ON, then restoring last scene in 30s"
- switch.turn_on: Relay_1
- switch.turn_on: Relay_2
- delay: 30s
- if:
condition:
and:
- api.connected:
- lambda: |-
auto s = id(ha_last_scene).state;
return !(s.empty() || s == "unknown" || s == "unavailable");
then:
- homeassistant.service:
service: script.lounge_set_scene
data:
scene_name: !lambda 'return id(ha_last_scene).state;'
- logger.log:
level: INFO
format: "HA Loss Test OFF: restored last scene: %s"
args: ['id(ha_last_scene).state.c_str()']
else:
- logger.log:
level: WARN
format: "HA Loss Test OFF: cannot restore scene (API not connected or last scene unknown)"