yaml layout updates to v1.x

This commit is contained in:
root
2026-02-25 21:05:21 +13:00
parent 61b0c64a1e
commit a8d2927274
44 changed files with 6521 additions and 4689 deletions
+199 -155
View File
@@ -1,65 +1,109 @@
#############################################
#############################################
# GARAGE DB CONTROLS
# Controlled by a Sonoff 4Ch R2
# https://github.com/jvyoralek/homeassistant-config/blob/master/esphome/sonoff-4ch.yaml
#:########################################################################################:#
# TITLE: GARAGE DB CONTROLS
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-garagedbcontrols.yaml
#:########################################################################################:#
# VERSIONS:
# v1.1 2026-02-25 Updated yaml to zorruno layout V1.1
# v1.0 2025-10-23 Initial Version
#:########################################################################################:#
# HARDWARE:
# - Sonoff 4CH R2 (ESP8266 / ESP8285)
# - Reference YAML: https://github.com/jvyoralek/homeassistant-config/blob/master/esphome/sonoff-4ch.yaml
# - Relays (4CH R2): GPIO12, GPIO5, GPIO4, GPIO15
# - Buttons (4CH R2): GPIO0, GPIO9, GPIO10, GPIO14
# - Status LED: GPIO13 (active-low)
# - Extras (as used here):
# - EV pulse input: GPIO2 (must be HIGH at boot; INPUT_PULLUP used)
# - DHT sensor: GPIO3 (note: UART RX pin on ESP8266; ensure logger baud_rate=0)
#:########################################################################################:#
# OPERATION NOTES:
# - Provides 4 relay outputs with matching local buttons.
# - Relay restore modes are set per-output (some default ON, some default OFF).
# - Includes EV charger energy monitoring via pulse input (2.0 Wh per pulse):
# - "Power Now" derived from pulse rate (pulse_meter).
# - Lifetime kWh total from pulse total (pulse_meter total).
# - 1-hour sliding average for power.
# - Daily kWh using total_daily_energy, with "Energy Yesterday" snap at 23:59 when time is valid.
# - Publishes a Tasmota-like MQTT telemetry message once per 60s with windowed Wh delta.
#:########################################################################################:#
# MQTT COMMANDS:
# - Publishes windowed EV Wh count every 60s:
# Topic: stat/tasmo-s4chan-7594-garage-1/EnergyMeterCount
# Payload example: {"EVChargerWhCount":123.456}
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant OFFLINE, WiFi OK
# - Device continues relay/button operation and continues pulse metering locally.
# - MQTT telemetry publishing depends on MQTT broker availability.
#
# V1.0 2025-10-23 Initial Version
# b) MQTT OFFLINE (or broker unreachable)
# - Relays/buttons still operate locally and via HA API (if HA is available).
# - MQTT telemetry messages will not be delivered until broker returns.
#
##########################################################################################
##########################################################################################
# c) Entire WiFi/Network OFFLINE
# - Accurate time IS needed for the "Energy Yesterday" snapshot at 23:59 (SNTP required).
# - Power/energy totals still accumulate, but "Energy Yesterday" will not update without valid time.
#:########################################################################################:#
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
#:########################################################################################:#
# SUBSTITUTIONS: Specific device variable substitutions
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
##########################################################################################
#:########################################################################################:#
substitutions:
# Device Custom Naming
# Device Naming
device_name: "esp-garagedbcontrols"
friendly_name: "Garage DB Control"
description_comment: "Garage DB Power, EV Power & Mesure, Lighting :: Sonoff 4ch R2"
device_area: "Garage" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
description_comment: "Garage DB Power, EV Power & Measure, Lighting :: Sonoff 4ch R2 (Layout V1.1)"
device_area: "Garage"
# Project Naming
project_name: "Sonoff Technologies.Sonoff 4Ch R2"
project_version: "v1.1"
# Passwords & Secrets
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
static_ip_address: !secret esp-garagedbcontrols_ip
#mqtt_device_name_1: "garage-dbcontrols"
# If we are changing IP addresses, you must update the current IP address here, otherwise it remains
# Don't forget to switch it back when changed.
current_ip_address: ${static_ip_address}
# General Settings
log_level: "ERROR"
update_interval: "60s"
# I/O Naming
switch_1_name: "Small EV Charger"
button_1_name: "Button 1"
relay_1_icon: "mdi:power-socket-au"
relay_1_icon: "mdi:power-socket-au"
switch_2_name: "Garage West Power"
button_2_name: "Button 2"
relay_2_icon: "mdi:power-socket-au"
relay_2_icon: "mdi:power-socket-au"
switch_3_name: "Spare 3"
button_3_name: "Button 3"
relay_3_icon: "mdi:toggle-switch"
relay_3_icon: "mdi:toggle-switch"
switch_4_name: "Spare 4"
button_4_name: "Button 4"
relay_4_icon: "mdi:toggle-switch"
relay_4_icon: "mdi:toggle-switch"
# Project Naming
project_name: "Sonoff Technologies.Sonoff 4Ch R2" # Project Details
project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# EV pulse settings
ev_pulse_wh: "2.0" # Wh per pulse
ev_pulse_filter: "10ms" # debounce pulses
ev_pulse_timeout: "2min" # set power to 0W if no pulses
ev_tasmota_tele_topic: "stat/tasmo-s4chan-7594-garage-1/EnergyMeterCount"
# MQTT Controls
#mqtt_command_main_topic: !secret mqtt_command_main_topic
#mqtt_status_main_topic: !secret mqtt_status_main_topic
#mqtt_command_topic_1: "${mqtt_command_main_topic}/${mqtt_device_name_1}"
#mqtt_status_topic_1: "${mqtt_status_main_topic}/${mqtt_device_name_1}"
#mqtt_command_ON: "ON"
#mqtt_command_OFF: "OFF"
# Passwords & Secrets (unfortunately you can't use substitutions inside secrets names)
api_key: !secret esp-api_key
ota_pass: !secret esp-ota_pass
# General Settings
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "60s" # update time for for general sensors etc
##########################################################################################
#:########################################################################################:#
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
#:########################################################################################:#
packages:
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
common_wifi: !include
@@ -68,6 +112,7 @@ packages:
local_device_name: "${device_name}"
local_static_ip_address: "${static_ip_address}"
local_ota_pass: "${ota_pass}"
local_current_ip_address: "${current_ip_address}"
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
common_api: !include
@@ -81,27 +126,28 @@ packages:
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
#### WEB PORTAL ####
#common_webportal: !include common/webportal_common.yaml
#### SNTP (Only use if you want/need accurate timeclocks) ####
common_sntp: !include common/sntp_common.yaml
common_sntp: !include common/sntp_common.yaml
#### DIAGNOSTICS Sensors ####
diag_basic: !include common/include_basic_diag_sensors.yaml
diag_more: !include common/include_more_diag_sensors.yaml
#diag_debug: !include common/include_debug_diag_sensors.yaml
#diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
diag_basic: !include common/include_basic_diag_sensors.yaml
diag_more: !include common/include_more_diag_sensors.yaml
#diag_debug: !include common/include_debug_diag_sensors.yaml
#diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
##########################################################################################
# ESPHome
#:########################################################################################:#
# 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
comment: "${description_comment}"
area: "${device_area}"
project:
name: "${project_name}"
version: "${project_version}"
# On boot, initialise the window start so the first publish isn't huge
on_boot:
priority: -10
@@ -113,123 +159,149 @@ esphome:
id(ev_last_total_kwh) = 0.0f;
}
##########################################################################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
##########################################################################################
#:########################################################################################:#
# ESP PLATFORM AND FRAMEWORK
# https://esphome.io/components/esp8266.html
#:########################################################################################:#
esp8266:
board: esp8285
#board: esp01_1m
restore_from_flash: True # restore some values on reboot
restore_from_flash: true
preferences:
flash_write_interval: 5min
mdns:
disabled: False
disabled: false
##########################################################################################
# ESPHome Logging Enable
#:########################################################################################:#
# LOGGING
# 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
level: "${log_level}"
baud_rate: 0
##########################################################################################
# SWITCH COMPONENT
#:########################################################################################:#
# STATUS LED
# https://esphome.io/components/status_led.html
#:########################################################################################:#
status_led:
pin:
number: GPIO13
inverted: yes
#:########################################################################################:#
# GLOBALS
# https://esphome.io/components/globals.html
#:########################################################################################:#
globals:
# Store the last published total (kWh) for 60s delta windowing
- id: ev_last_total_kwh
type: float
restore_value: true
initial_value: "0.0"
- id: ev_energy_yesterday_kwh
type: float
restore_value: true
initial_value: "0.0"
# Track last day-of-year we snapped so it only happens once per day
- id: ev_last_snapshot_doy
type: int
restore_value: no
initial_value: "-1"
#:########################################################################################:#
# SWITCHES
# https://esphome.io/components/switch/
##########################################################################################
# Sonoff 4Ch R2 Relays are GPIO12,05,04,15
#############################################
#:########################################################################################:#
switch:
# Sonoff 4Ch R2 Relays are GPIO12, GPIO5, GPIO4, GPIO15
- platform: gpio
id: relay1
name: "${switch_1_name}"
restore_mode: RESTORE_DEFAULT_OFF
pin: GPIO12
icon: "${relay_1_icon}"
icon: "${relay_1_icon}"
- platform: gpio
id: relay2
name: "${switch_2_name}"
restore_mode: RESTORE_DEFAULT_ON
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO5
icon: "${relay_2_icon}"
icon: "${relay_2_icon}"
- platform: gpio
id: relay3
id: relay3
name: "${switch_3_name}"
restore_mode: RESTORE_DEFAULT_ON
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO4
icon: "${relay_3_icon}"
icon: "${relay_3_icon}"
- platform: gpio
id: relay4
name: "${switch_4_name}"
restore_mode: RESTORE_DEFAULT_OFF
pin: GPIO15
icon: "${relay_4_icon}"
icon: "${relay_4_icon}"
##########################################################################################
#:########################################################################################:#
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
# Sonoff 4Ch R2 Buttons are GPIO00,09,10,14
#############################################
#:########################################################################################:#
binary_sensor:
# Sonoff 4Ch R2 Buttons are GPIO0, GPIO9, GPIO10, GPIO14
- platform: gpio
id: button1
name: "${button_1_name}"
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
inverted: true
on_press:
- switch.toggle: relay1
- switch.toggle: relay1
- platform: gpio
id: button2
name: "${button_2_name}"
pin:
number: GPIO9
mode: INPUT_PULLUP
inverted: True
inverted: true
on_press:
- switch.toggle: relay2
- switch.toggle: relay2
- platform: gpio
id: button3
name: "${button_3_name}"
pin:
number: GPIO10
mode: INPUT_PULLUP
inverted: True
inverted: true
on_press:
- switch.toggle: relay3
- switch.toggle: relay3
- platform: gpio
id: button4
name: "${button_4_name}"
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: True
inverted: true
on_press:
- switch.toggle: relay4
- switch.toggle: relay4
##########################################################################################
# STATUS LED
# https://esphome.io/components/status_led.html
##########################################################################################
status_led:
pin:
number: GPIO13
inverted: yes
##########################################################################################
# EV CHARGER ENERGY (PULSE INPUT ON GPIO2, 2.0 Wh PER PULSE)
# - Uses pulse_meter for stable pulse rate and total count
# - Derives instantaneous Power (W), 1h average Power, Today kWh, and lifetime kWh
##########################################################################################
#:########################################################################################:#
# SENSORS
# https://esphome.io/components/sensor/
#:########################################################################################:#
sensor:
# Pulse rate -> Power Now (W)
########################################################################################
# EV CHARGER ENERGY (PULSE INPUT ON GPIO2, 2.0 Wh PER PULSE)
# - Uses pulse_meter for stable pulse rate and total count
# - Derives Power (W), 1h average Power, Today kWh, and lifetime kWh
########################################################################################
- platform: pulse_meter
id: ev_pulse_pm
pin:
@@ -237,17 +309,16 @@ sensor:
mode: INPUT_PULLUP # GPIO2 must be HIGH at boot
inverted: false
name: "${switch_1_name} Power Now"
# pulse_meter reports pulses/min; each pulse = 2 Wh => W = pulses/min * 2 * 60 = x120
unit_of_measurement: "W"
device_class: power
state_class: measurement
accuracy_decimals: 0
internal_filter: 10ms # debounce pulses
timeout: 2min # set to 0 W if no pulses for 2 minutes
internal_filter: ${ev_pulse_filter}
timeout: ${ev_pulse_timeout}
# pulse_meter reports pulses/min; each pulse = 2 Wh => W = pulses/min * 2 * 60 = x120
filters:
- multiply: 120.0 # convert pulses/min to Watts
- multiply: 120.0
# Lifetime energy total (monotonic)
total:
id: ev_energy_total_kwh
name: "${switch_1_name} Energy Cumulative Total"
@@ -256,9 +327,9 @@ sensor:
state_class: total_increasing
accuracy_decimals: 1
filters:
- multiply: 0.002 # kWh per pulse (2.0 Wh = 0.002 kWh)
# kWh per pulse (2.0 Wh = 0.002 kWh)
- multiply: 0.002
# 1-hour sliding average of power (W)
- platform: template
id: ev_power_avg_1h
name: "${switch_1_name} Power (1h avg)"
@@ -274,7 +345,6 @@ sensor:
window_size: 360 # 10 s * 360 ≈ 1 hour
send_every: 6 # update HA once per minute
# Today's energy (kWh), integrates "Power Now" and resets at local midnight
- platform: total_daily_energy
id: ev_energy_today_kwh
name: "${switch_1_name} Energy Today"
@@ -284,9 +354,9 @@ sensor:
state_class: total
accuracy_decimals: 1
filters:
- multiply: 0.001 # total_daily_energy outputs Wh; convert to kWh
# total_daily_energy outputs Wh; convert to kWh
- multiply: 0.001
# Expose "Energy Yesterday" as a normal sensor
- platform: template
id: ev_energy_yesterday_sensor
name: "${switch_1_name} Energy Yesterday"
@@ -296,85 +366,59 @@ sensor:
accuracy_decimals: 1
update_interval: 30s
lambda: |-
return id(ev_energy_yesterday_kwh);
return id(ev_energy_yesterday_kwh);
##########################################################################################
# DHT SENSOR
# https://esphome.io/components/sensor/dht/
##########################################################################################
#sensor:
########################################################################################
# DHT SENSOR (GPIO3)
# NOTE: GPIO3 is UART RX on ESP8266; logger baud_rate=0 avoids conflicts.
########################################################################################
- platform: dht
pin: GPIO3
update_interval: 60s
temperature:
name: "${friendly_name} Temperature"
humidity:
name: "${friendly_name} Humidity"
update_interval: 60s
##########################################################################################
# Tasmota-like teleperiod publish of windowed Wh count every 60 s
##########################################################################################
# Store the last published total (kWh)
globals:
- id: ev_last_total_kwh
type: float
restore_value: true
initial_value: "0.0"
- id: ev_energy_yesterday_kwh
type: float
restore_value: true
initial_value: "0.0"
# Track last day-of-year we snapped so it only happens once per day
- id: ev_last_snapshot_doy
type: int
restore_value: no
initial_value: '-1'
# Teleperiod = 60s: publish JSON and advance the window
#:########################################################################################:#
# INTERVALS
# https://esphome.io/components/interval.html
#:########################################################################################:#
interval:
# Teleperiod = 60s: publish JSON and advance the window
- interval: 60s
then:
- mqtt.publish:
topic: "stat/tasmo-s4chan-7594-garage-1/EnergyMeterCount"
topic: "${ev_tasmota_tele_topic}"
qos: 0
retain: false
payload: !lambda |-
// Current lifetime kWh from pulse_meter "total"
float current = id(ev_energy_total_kwh).state;
if (!isfinite(current)) current = id(ev_last_total_kwh);
// Delta since last publish (kWh -> Wh)
float delta_kwh = current - id(ev_last_total_kwh);
if (delta_kwh < 0.0f || !isfinite(delta_kwh)) delta_kwh = 0.0f;
float delta_wh = delta_kwh * 1000.0f;
// Advance the window (equivalent of "counter1 0")
id(ev_last_total_kwh) = current;
// Build JSON payload
char buf[64];
snprintf(buf, sizeof(buf), "{\"EVChargerWhCount\":%.3f}", delta_wh);
return std::string(buf);
# YESTERDAY ENERGY (device-side snapshot of today's total at end-of-day)
# Uses existing time id(sntp_time) from sntp_common.yaml
# YESTERDAY ENERGY snapshot (once per day at 23:59, when time is valid)
- interval: 60s
then:
- lambda: |-
auto now = id(sntp_time).now();
if (!now.is_valid()) return;
// Snapshot once per day at 23:59 (minute precision)
if (now.hour != 23 || now.minute != 59) return;
// Prevent repeats if this runs multiple times during 23:59
if (id(ev_last_snapshot_doy) == now.day_of_year) return;
if (isfinite(id(ev_energy_today_kwh).state)) {
id(ev_energy_yesterday_kwh) = id(ev_energy_today_kwh).state;
}
id(ev_last_snapshot_doy) = now.day_of_year;
id(ev_last_snapshot_doy) = now.day_of_year;