yaml layout updates to v1.1 and minor fixes
This commit is contained in:
+458
-136
@@ -6,6 +6,10 @@
|
||||
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-garagedbcontrols.yaml
|
||||
#:########################################################################################:#
|
||||
# VERSIONS:
|
||||
# v1.2 2026-03-03 EV monitoring updated to match Device A:
|
||||
# - Rolling average EV power (default 5 min @ 10s)
|
||||
# - EV charging binary sensor (threshold configurable)
|
||||
# - EV totals and energy summaries (today/yesterday/month/year)
|
||||
# v1.1 2026-02-25 Updated yaml to zorruno layout V1.1
|
||||
# v1.0 2025-10-23 Initial Version
|
||||
#:########################################################################################:#
|
||||
@@ -22,36 +26,27 @@
|
||||
# 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}
|
||||
# - EV charger monitoring (2 Wh per pulse on GPIO2):
|
||||
# - Lifetime total energy (kWh) from persisted absolute pulse total.
|
||||
# - Rolling average power (default 5 min) to reduce spiky readings at low load.
|
||||
# - Charging binary sensor based on averaged power threshold.
|
||||
# - Energy summaries: today, yesterday, this month, last month, this year, last year.
|
||||
#:########################################################################################:#
|
||||
# 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.
|
||||
#
|
||||
# - Device continues relay/button operation and continues pulse counting locally.
|
||||
# - API/MQTT publishing will fail until HA (and/or broker) is back.
|
||||
# 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.
|
||||
#
|
||||
# - Relays/buttons still operate locally and via HA API (if HA is available).
|
||||
# - MQTT state publishing depends on broker.
|
||||
# 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.
|
||||
# - Accurate time IS needed for daily/monthly/year rollovers (SNTP required for correct period boundaries).
|
||||
# - Loss of network will drift the timeclocks; time of day can be reset by powering off then on again at 12pm (midday)
|
||||
#:########################################################################################:#
|
||||
|
||||
|
||||
#:########################################################################################:#
|
||||
# SUBSTITUTIONS: Specific device variable substitutions
|
||||
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
|
||||
#:########################################################################################:#
|
||||
substitutions:
|
||||
# Device Naming
|
||||
@@ -62,7 +57,7 @@ substitutions:
|
||||
|
||||
# Project Naming
|
||||
project_name: "Sonoff Technologies.Sonoff 4Ch R2"
|
||||
project_version: "v1.1"
|
||||
project_version: "v1.2"
|
||||
|
||||
# Passwords & Secrets
|
||||
api_key: !secret esp-api_key
|
||||
@@ -77,8 +72,12 @@ substitutions:
|
||||
log_level: "ERROR"
|
||||
update_interval: "60s"
|
||||
|
||||
# Globals persistence tuning (gentler on flash)
|
||||
globals_save_interval_slow: "10min"
|
||||
globals_save_interval_pulses: "2min"
|
||||
|
||||
# I/O Naming
|
||||
switch_1_name: "Small EV Charger"
|
||||
switch_1_name: "16A Wallcharger Operation"
|
||||
button_1_name: "Button 1"
|
||||
relay_1_icon: "mdi:power-socket-au"
|
||||
|
||||
@@ -94,15 +93,43 @@ substitutions:
|
||||
button_4_name: "Button 4"
|
||||
relay_4_icon: "mdi:toggle-switch"
|
||||
|
||||
# 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"
|
||||
########################################################################################
|
||||
# EV Pulse Settings (Device A logic)
|
||||
########################################################################################
|
||||
ev_pulse_wh: "2.0" # Each pulse is 2Wh
|
||||
ev_pulse_filter_ms: "10ms" # Debounce / filter
|
||||
ev_pulse_pin_mode: "INPUT_PULLUP"
|
||||
|
||||
# EV Charging Detection (Averaged power + binary sensor)
|
||||
# Minimum EV charging power threshold (W) for binary sensor
|
||||
ev_charging_min_power_w: "200"
|
||||
|
||||
# Rolling average window - configuration notes:
|
||||
# - Substitutions are string replacements, so window samples must be updated manually if you change times.
|
||||
# - window_samples = (avg_minutes * 60) / sample_interval_seconds
|
||||
ev_power_avg_window_minutes: "5" # informational
|
||||
ev_power_avg_sample_interval: "10s"
|
||||
ev_power_avg_sample_interval_seconds: "10" # must match the value above
|
||||
ev_power_avg_window_samples: "15" # 5 min @ 10s = 30 samples
|
||||
ev_power_avg_buffer_size: "60" # ring buffer >= window_samples (180 @ 10s = 30 minutes)
|
||||
|
||||
# Rollover check interval (time-valid day/month/year updates)
|
||||
rollover_check_interval: "30s"
|
||||
|
||||
# Names - EV Wallcharger (Device B)
|
||||
ev_charger_name: "16A Wallcharger"
|
||||
ev_power_name: "${ev_charger_name} Power"
|
||||
ev_charging_name: "${ev_charger_name} Charging"
|
||||
ev_total_energy_name: "${ev_charger_name} Total Energy"
|
||||
ev_today_name: "${ev_charger_name} Energy Today"
|
||||
ev_yesterday_name: "${ev_charger_name} Energy Yesterday"
|
||||
ev_month_name: "${ev_charger_name} Energy This Month"
|
||||
ev_last_month_name: "${ev_charger_name} Energy Last Month"
|
||||
ev_year_name: "${ev_charger_name} Energy This Year"
|
||||
ev_last_year_name: "${ev_charger_name} Energy Last Year"
|
||||
|
||||
#:########################################################################################:#
|
||||
# PACKAGES: Included Common Packages
|
||||
# https://esphome.io/components/packages.html
|
||||
#:########################################################################################:#
|
||||
packages:
|
||||
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
|
||||
@@ -114,10 +141,9 @@ packages:
|
||||
local_ota_pass: "${ota_pass}"
|
||||
local_current_ip_address: "${current_ip_address}"
|
||||
|
||||
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
|
||||
#### HOME ASSISTANT API ####
|
||||
common_api: !include
|
||||
file: common/api_common.yaml
|
||||
#file: common/api_common_noencryption.yaml
|
||||
vars:
|
||||
local_api_key: "${api_key}"
|
||||
|
||||
@@ -127,18 +153,15 @@ packages:
|
||||
vars:
|
||||
local_device_name: "${device_name}"
|
||||
|
||||
#### SNTP (Only use if you want/need accurate timeclocks) ####
|
||||
#### SNTP ####
|
||||
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}"
|
||||
@@ -148,20 +171,15 @@ esphome:
|
||||
project:
|
||||
name: "${project_name}"
|
||||
version: "${project_version}"
|
||||
# On boot, initialise the window start so the first publish isn't huge
|
||||
on_boot:
|
||||
priority: -10
|
||||
priority: -100
|
||||
then:
|
||||
- lambda: |-
|
||||
if (isfinite(id(ev_energy_total_kwh).state)) {
|
||||
id(ev_last_total_kwh) = id(ev_energy_total_kwh).state;
|
||||
} else {
|
||||
id(ev_last_total_kwh) = 0.0f;
|
||||
}
|
||||
- script.execute: init_ev_history_arrays
|
||||
- delay: 3s
|
||||
- script.execute: ev_rollover_check
|
||||
|
||||
#:########################################################################################:#
|
||||
# ESP PLATFORM AND FRAMEWORK
|
||||
# https://esphome.io/components/esp8266.html
|
||||
#:########################################################################################:#
|
||||
esp8266:
|
||||
board: esp8285
|
||||
@@ -175,7 +193,6 @@ mdns:
|
||||
|
||||
#:########################################################################################:#
|
||||
# LOGGING
|
||||
# https://esphome.io/components/logger.html
|
||||
#:########################################################################################:#
|
||||
logger:
|
||||
level: "${log_level}"
|
||||
@@ -183,7 +200,6 @@ logger:
|
||||
|
||||
#:########################################################################################:#
|
||||
# STATUS LED
|
||||
# https://esphome.io/components/status_led.html
|
||||
#:########################################################################################:#
|
||||
status_led:
|
||||
pin:
|
||||
@@ -191,30 +207,93 @@ status_led:
|
||||
inverted: yes
|
||||
|
||||
#:########################################################################################:#
|
||||
# GLOBALS
|
||||
# https://esphome.io/components/globals.html
|
||||
# GLOBALS (EV totals + rollovers + rolling average history)
|
||||
#:########################################################################################:#
|
||||
globals:
|
||||
# Store the last published total (kWh) for 60s delta windowing
|
||||
- id: ev_last_total_kwh
|
||||
# Date tracking for rollovers (restored)
|
||||
- id: g_last_day
|
||||
type: int
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "0"
|
||||
- id: g_last_month
|
||||
type: int
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "0"
|
||||
- id: g_last_year
|
||||
type: int
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "0"
|
||||
|
||||
# EV baseline totals for period calculations (restored)
|
||||
- id: g_ev_day_start_kwh
|
||||
type: float
|
||||
restore_value: true
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "-1.0"
|
||||
- id: g_ev_month_start_kwh
|
||||
type: float
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "-1.0"
|
||||
- id: g_ev_year_start_kwh
|
||||
type: float
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "-1.0"
|
||||
|
||||
# EV stored prior periods (restored)
|
||||
- id: g_ev_yesterday_kwh
|
||||
type: float
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "0.0"
|
||||
- id: g_ev_last_month_kwh
|
||||
type: float
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "0.0"
|
||||
- id: g_ev_last_year_kwh
|
||||
type: float
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_slow}
|
||||
initial_value: "0.0"
|
||||
|
||||
- id: ev_energy_yesterday_kwh
|
||||
# EV pulse accumulation (persisted across reboots) (restored)
|
||||
- id: g_ev_pulses_total_abs
|
||||
type: uint32_t
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_pulses}
|
||||
initial_value: "0"
|
||||
- id: g_ev_pulses_raw_last
|
||||
type: uint32_t
|
||||
restore_value: yes
|
||||
update_interval: ${globals_save_interval_pulses}
|
||||
initial_value: "0"
|
||||
|
||||
# EV rolling average power (RAM only)
|
||||
- id: g_ev_power_avg_w
|
||||
type: float
|
||||
restore_value: true
|
||||
restore_value: no
|
||||
initial_value: "0.0"
|
||||
|
||||
# Track last day-of-year we snapped so it only happens once per day
|
||||
- id: ev_last_snapshot_doy
|
||||
# EV pulse history ring buffer for rolling average (RAM only)
|
||||
- id: g_ev_pulses_hist
|
||||
type: uint32_t[${ev_power_avg_buffer_size}]
|
||||
restore_value: no
|
||||
- id: g_ev_pulses_hist_index
|
||||
type: int
|
||||
restore_value: no
|
||||
initial_value: "-1"
|
||||
initial_value: "0"
|
||||
- id: g_ev_pulses_hist_count
|
||||
type: int
|
||||
restore_value: no
|
||||
initial_value: "0"
|
||||
|
||||
#:########################################################################################:#
|
||||
# SWITCHES
|
||||
# https://esphome.io/components/switch/
|
||||
#:########################################################################################:#
|
||||
switch:
|
||||
# Sonoff 4Ch R2 Relays are GPIO12, GPIO5, GPIO4, GPIO15
|
||||
@@ -248,7 +327,6 @@ switch:
|
||||
|
||||
#:########################################################################################:#
|
||||
# BINARY SENSORS
|
||||
# https://esphome.io/components/binary_sensor/
|
||||
#:########################################################################################:#
|
||||
binary_sensor:
|
||||
# Sonoff 4Ch R2 Buttons are GPIO0, GPIO9, GPIO10, GPIO14
|
||||
@@ -292,133 +370,377 @@ binary_sensor:
|
||||
on_press:
|
||||
- switch.toggle: relay4
|
||||
|
||||
# EV charging detection (Device A logic: averaged power threshold)
|
||||
- platform: template
|
||||
name: "${ev_charging_name}"
|
||||
id: ev_wallcharger_charging
|
||||
device_class: power
|
||||
icon: "mdi:ev-station"
|
||||
lambda: |-
|
||||
const float p = id(ev_power_avg_w).state;
|
||||
if (isnan(p)) return false;
|
||||
return (p >= ${ev_charging_min_power_w});
|
||||
|
||||
#:########################################################################################:#
|
||||
# SENSORS
|
||||
# https://esphome.io/components/sensor/
|
||||
#:########################################################################################:#
|
||||
sensor:
|
||||
########################################################################################
|
||||
# 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
|
||||
# EV WALLCHARGER PULSE ENERGY (GPIO2)
|
||||
########################################################################################
|
||||
- platform: pulse_meter
|
||||
id: ev_pulse_pm
|
||||
- platform: pulse_counter
|
||||
id: ev_pulse_rate_ppm
|
||||
internal: true
|
||||
pin:
|
||||
number: GPIO2
|
||||
mode: INPUT_PULLUP # GPIO2 must be HIGH at boot
|
||||
mode: ${ev_pulse_pin_mode} # GPIO2 must be HIGH at boot
|
||||
inverted: false
|
||||
name: "${switch_1_name} Power Now"
|
||||
update_interval: ${ev_power_avg_sample_interval}
|
||||
internal_filter: ${ev_pulse_filter_ms}
|
||||
total:
|
||||
id: ev_pulses_raw_total
|
||||
internal: true
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
uint32_t raw = (uint32_t) x;
|
||||
uint32_t last = id(g_ev_pulses_raw_last);
|
||||
if (raw < last) {
|
||||
last = 0;
|
||||
}
|
||||
id(g_ev_pulses_total_abs) += (raw - last);
|
||||
id(g_ev_pulses_raw_last) = raw;
|
||||
|
||||
- platform: template
|
||||
name: "${ev_power_name}"
|
||||
id: ev_power_avg_w
|
||||
unit_of_measurement: "W"
|
||||
device_class: power
|
||||
state_class: measurement
|
||||
accuracy_decimals: 0
|
||||
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
|
||||
|
||||
total:
|
||||
id: ev_energy_total_kwh
|
||||
name: "${switch_1_name} Energy Cumulative Total"
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total_increasing
|
||||
accuracy_decimals: 1
|
||||
filters:
|
||||
# kWh per pulse (2.0 Wh = 0.002 kWh)
|
||||
- multiply: 0.002
|
||||
update_interval: "${ev_power_avg_sample_interval}"
|
||||
lambda: |-
|
||||
return id(g_ev_power_avg_w);
|
||||
|
||||
- platform: template
|
||||
id: ev_power_avg_1h
|
||||
name: "${switch_1_name} Power (1h avg)"
|
||||
unit_of_measurement: "W"
|
||||
device_class: power
|
||||
state_class: measurement
|
||||
accuracy_decimals: 1
|
||||
update_interval: 10s
|
||||
name: "${ev_total_energy_name}"
|
||||
id: ev_total_energy_kwh
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total_increasing
|
||||
accuracy_decimals: 3
|
||||
update_interval: ${ev_power_avg_sample_interval}
|
||||
lambda: |-
|
||||
return id(ev_pulse_pm).state;
|
||||
filters:
|
||||
- sliding_window_moving_average:
|
||||
window_size: 360 # 10 s * 360 ≈ 1 hour
|
||||
send_every: 6 # update HA once per minute
|
||||
return ((float) id(g_ev_pulses_total_abs)) * (${ev_pulse_wh} / 1000.0f);
|
||||
|
||||
- platform: total_daily_energy
|
||||
- platform: template
|
||||
name: "${ev_today_name}"
|
||||
id: ev_energy_today_kwh
|
||||
name: "${switch_1_name} Energy Today"
|
||||
power_id: ev_pulse_pm
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total
|
||||
accuracy_decimals: 1
|
||||
filters:
|
||||
# total_daily_energy outputs Wh; convert to kWh
|
||||
- multiply: 0.001
|
||||
accuracy_decimals: 3
|
||||
update_interval: 60s
|
||||
lambda: |-
|
||||
const float total = id(ev_total_energy_kwh).state;
|
||||
if (isnan(total) || id(g_ev_day_start_kwh) < 0.0f) return NAN;
|
||||
float v = total - id(g_ev_day_start_kwh);
|
||||
if (v < 0.0f) v = 0.0f;
|
||||
return v;
|
||||
|
||||
- platform: template
|
||||
id: ev_energy_yesterday_sensor
|
||||
name: "${switch_1_name} Energy Yesterday"
|
||||
name: "${ev_yesterday_name}"
|
||||
id: ev_energy_yesterday_kwh
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total
|
||||
accuracy_decimals: 1
|
||||
update_interval: 30s
|
||||
accuracy_decimals: 3
|
||||
update_interval: 10min
|
||||
lambda: |-
|
||||
return id(ev_energy_yesterday_kwh);
|
||||
return id(g_ev_yesterday_kwh);
|
||||
|
||||
- platform: template
|
||||
name: "${ev_month_name}"
|
||||
id: ev_energy_this_month_kwh
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total
|
||||
accuracy_decimals: 3
|
||||
update_interval: 10min
|
||||
lambda: |-
|
||||
const float total = id(ev_total_energy_kwh).state;
|
||||
if (isnan(total) || id(g_ev_month_start_kwh) < 0.0f) return NAN;
|
||||
float v = total - id(g_ev_month_start_kwh);
|
||||
if (v < 0.0f) v = 0.0f;
|
||||
return v;
|
||||
|
||||
- platform: template
|
||||
name: "${ev_last_month_name}"
|
||||
id: ev_energy_last_month_kwh
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total
|
||||
accuracy_decimals: 3
|
||||
update_interval: 30min
|
||||
lambda: |-
|
||||
return id(g_ev_last_month_kwh);
|
||||
|
||||
- platform: template
|
||||
name: "${ev_year_name}"
|
||||
id: ev_energy_this_year_kwh
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total
|
||||
accuracy_decimals: 3
|
||||
update_interval: 30min
|
||||
lambda: |-
|
||||
const float total = id(ev_total_energy_kwh).state;
|
||||
if (isnan(total) || id(g_ev_year_start_kwh) < 0.0f) return NAN;
|
||||
float v = total - id(g_ev_year_start_kwh);
|
||||
if (v < 0.0f) v = 0.0f;
|
||||
return v;
|
||||
|
||||
- platform: template
|
||||
name: "${ev_last_year_name}"
|
||||
id: ev_energy_last_year_kwh
|
||||
unit_of_measurement: "kWh"
|
||||
device_class: energy
|
||||
state_class: total
|
||||
accuracy_decimals: 3
|
||||
update_interval: 60min
|
||||
lambda: |-
|
||||
return id(g_ev_last_year_kwh);
|
||||
|
||||
########################################################################################
|
||||
# DHT SENSOR (GPIO3)
|
||||
# NOTE: GPIO3 is UART RX on ESP8266; logger baud_rate=0 avoids conflicts.
|
||||
########################################################################################
|
||||
- platform: dht
|
||||
pin: GPIO3
|
||||
update_interval: 60s
|
||||
update_interval: ${update_interval}
|
||||
temperature:
|
||||
name: "${friendly_name} Temperature"
|
||||
unit_of_measurement: "°C"
|
||||
accuracy_decimals: 1
|
||||
humidity:
|
||||
name: "${friendly_name} Humidity"
|
||||
unit_of_measurement: "%"
|
||||
accuracy_decimals: 0
|
||||
|
||||
#:########################################################################################:#
|
||||
# INTERVALS
|
||||
# https://esphome.io/components/interval.html
|
||||
# INTERVALS (EV rolling average + rollover checks)
|
||||
#:########################################################################################:#
|
||||
interval:
|
||||
# Teleperiod = 60s: publish JSON and advance the window
|
||||
- interval: 60s
|
||||
- interval: "${rollover_check_interval}"
|
||||
then:
|
||||
- mqtt.publish:
|
||||
topic: "${ev_tasmota_tele_topic}"
|
||||
qos: 0
|
||||
retain: false
|
||||
payload: !lambda |-
|
||||
float current = id(ev_energy_total_kwh).state;
|
||||
if (!isfinite(current)) current = id(ev_last_total_kwh);
|
||||
- script.execute: ev_rollover_check
|
||||
|
||||
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;
|
||||
- interval: "${ev_power_avg_sample_interval}"
|
||||
then:
|
||||
- script.execute: ev_power_avg_update
|
||||
|
||||
id(ev_last_total_kwh) = current;
|
||||
#:########################################################################################:#
|
||||
# SCRIPTS (EV)
|
||||
#:########################################################################################:#
|
||||
script:
|
||||
# Initialize EV pulse history buffer so rolling average starts stable after boot
|
||||
- id: init_ev_history_arrays
|
||||
mode: queued
|
||||
then:
|
||||
- lambda: |-
|
||||
const int ev_buf = ${ev_power_avg_buffer_size};
|
||||
const uint32_t p0 = id(g_ev_pulses_total_abs);
|
||||
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "{\"EVChargerWhCount\":%.3f}", delta_wh);
|
||||
return std::string(buf);
|
||||
for (int i = 0; i < ev_buf; i++) {
|
||||
id(g_ev_pulses_hist)[i] = p0;
|
||||
}
|
||||
id(g_ev_pulses_hist_index) = 0;
|
||||
id(g_ev_pulses_hist_count) = ev_buf;
|
||||
id(g_ev_power_avg_w) = 0.0f;
|
||||
|
||||
# YESTERDAY ENERGY snapshot (once per day at 23:59, when time is valid)
|
||||
- interval: 60s
|
||||
# EV rolling average update (based on absolute pulse total)
|
||||
- id: ev_power_avg_update
|
||||
mode: queued
|
||||
then:
|
||||
- lambda: |-
|
||||
const int buf = ${ev_power_avg_buffer_size};
|
||||
int idx = id(g_ev_pulses_hist_index);
|
||||
if (idx < 0 || idx >= buf) idx = 0;
|
||||
|
||||
const uint32_t p_now = id(g_ev_pulses_total_abs);
|
||||
id(g_ev_pulses_hist)[idx] = p_now;
|
||||
|
||||
idx = (idx + 1) % buf;
|
||||
id(g_ev_pulses_hist_index) = idx;
|
||||
|
||||
if (id(g_ev_pulses_hist_count) < buf) id(g_ev_pulses_hist_count)++;
|
||||
|
||||
int win = ${ev_power_avg_window_samples};
|
||||
if (win < 1) win = 1;
|
||||
if (win > buf) win = buf;
|
||||
|
||||
if (id(g_ev_pulses_hist_count) < win) {
|
||||
return;
|
||||
}
|
||||
|
||||
int old_idx = idx - win;
|
||||
if (old_idx < 0) old_idx += buf;
|
||||
|
||||
const uint32_t p_old = id(g_ev_pulses_hist)[old_idx];
|
||||
const uint32_t dp = (p_now >= p_old) ? (p_now - p_old) : 0;
|
||||
|
||||
const float wh = ((float) dp) * (${ev_pulse_wh});
|
||||
const float seconds = ((float) win) * (${ev_power_avg_sample_interval_seconds});
|
||||
|
||||
if (seconds <= 0.0f) {
|
||||
id(g_ev_power_avg_w) = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
// W = Wh * 3600 / seconds
|
||||
id(g_ev_power_avg_w) = wh * 3600.0f / seconds;
|
||||
|
||||
# Period rollover check (day/month/year) for EV totals
|
||||
- id: ev_rollover_check
|
||||
mode: restart
|
||||
then:
|
||||
- lambda: |-
|
||||
auto now = id(sntp_time).now();
|
||||
if (!now.is_valid()) return;
|
||||
|
||||
if (now.hour != 23 || now.minute != 59) return;
|
||||
|
||||
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;
|
||||
if (!now.is_valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
id(ev_last_snapshot_doy) = now.day_of_year;
|
||||
const int d = now.day_of_month;
|
||||
const int m = now.month;
|
||||
const int y = now.year;
|
||||
|
||||
const float ev_total = id(ev_total_energy_kwh).state;
|
||||
|
||||
// Initialize date markers on first valid time
|
||||
if (id(g_last_year) == 0) {
|
||||
id(g_last_day) = d;
|
||||
id(g_last_month) = m;
|
||||
id(g_last_year) = y;
|
||||
|
||||
if (!isnan(ev_total)) {
|
||||
id(g_ev_day_start_kwh) = ev_total;
|
||||
id(g_ev_month_start_kwh) = ev_total;
|
||||
id(g_ev_year_start_kwh) = ev_total;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Protect against meter reset / wrap (total < baseline)
|
||||
if (!isnan(ev_total)) {
|
||||
if (id(g_ev_day_start_kwh) < 0.0f) id(g_ev_day_start_kwh) = ev_total;
|
||||
if (id(g_ev_month_start_kwh) < 0.0f) id(g_ev_month_start_kwh) = ev_total;
|
||||
if (id(g_ev_year_start_kwh) < 0.0f) id(g_ev_year_start_kwh) = ev_total;
|
||||
|
||||
if (ev_total < id(g_ev_day_start_kwh)) id(g_ev_day_start_kwh) = ev_total;
|
||||
if (ev_total < id(g_ev_month_start_kwh)) id(g_ev_month_start_kwh) = ev_total;
|
||||
if (ev_total < id(g_ev_year_start_kwh)) id(g_ev_year_start_kwh) = ev_total;
|
||||
}
|
||||
|
||||
// YEAR rollover
|
||||
if (y != id(g_last_year)) {
|
||||
if (!isnan(ev_total) && id(g_ev_year_start_kwh) >= 0.0f) {
|
||||
float ykwh = ev_total - id(g_ev_year_start_kwh);
|
||||
if (ykwh < 0.0f) ykwh = 0.0f;
|
||||
id(g_ev_last_year_kwh) = ykwh;
|
||||
id(g_ev_year_start_kwh) = ev_total;
|
||||
}
|
||||
id(g_last_year) = y;
|
||||
}
|
||||
|
||||
// MONTH rollover
|
||||
if (m != id(g_last_month)) {
|
||||
if (!isnan(ev_total) && id(g_ev_month_start_kwh) >= 0.0f) {
|
||||
float mkwh = ev_total - id(g_ev_month_start_kwh);
|
||||
if (mkwh < 0.0f) mkwh = 0.0f;
|
||||
id(g_ev_last_month_kwh) = mkwh;
|
||||
id(g_ev_month_start_kwh) = ev_total;
|
||||
}
|
||||
id(g_last_month) = m;
|
||||
}
|
||||
|
||||
// DAY rollover
|
||||
if (d != id(g_last_day)) {
|
||||
if (!isnan(ev_total) && id(g_ev_day_start_kwh) >= 0.0f) {
|
||||
float dkwh = ev_total - id(g_ev_day_start_kwh);
|
||||
if (dkwh < 0.0f) dkwh = 0.0f;
|
||||
id(g_ev_yesterday_kwh) = dkwh;
|
||||
id(g_ev_day_start_kwh) = ev_total;
|
||||
}
|
||||
id(g_last_day) = d;
|
||||
}
|
||||
|
||||
#:########################################################################################:#
|
||||
# ROLLBACK COPY (v1.1) - PREVIOUS EV SECTION (pulse_meter + daily snapshot + mqtt window)
|
||||
# NOTE: Commented out. Kept for quick revert if needed.
|
||||
#:########################################################################################:#
|
||||
#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"
|
||||
#
|
||||
# - id: ev_last_snapshot_doy
|
||||
# type: int
|
||||
# restore_value: no
|
||||
# initial_value: "-1"
|
||||
#
|
||||
#sensor:
|
||||
# - platform: pulse_meter
|
||||
# id: ev_pulse_pm
|
||||
# pin:
|
||||
# number: GPIO2
|
||||
# mode: INPUT_PULLUP
|
||||
# inverted: false
|
||||
# name: "${switch_1_name} Power Now"
|
||||
# unit_of_measurement: "W"
|
||||
# accuracy_decimals: 0
|
||||
# internal_filter: ${ev_pulse_filter}
|
||||
# timeout: ${ev_pulse_timeout}
|
||||
# filters:
|
||||
# - multiply: 120.0
|
||||
# total:
|
||||
# id: ev_energy_total_kwh
|
||||
# name: "${switch_1_name} Energy Cumulative Total"
|
||||
# unit_of_measurement: "kWh"
|
||||
# accuracy_decimals: 1
|
||||
# filters:
|
||||
# - multiply: 0.002
|
||||
#
|
||||
#interval:
|
||||
# - interval: 60s
|
||||
# then:
|
||||
# - mqtt.publish:
|
||||
# topic: "${ev_tasmota_tele_topic}"
|
||||
# qos: 0
|
||||
# retain: false
|
||||
# payload: !lambda |-
|
||||
# float current = id(ev_energy_total_kwh).state;
|
||||
# if (!isfinite(current)) current = id(ev_last_total_kwh);
|
||||
# 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;
|
||||
# id(ev_last_total_kwh) = current;
|
||||
# char buf[64];
|
||||
# snprintf(buf, sizeof(buf), "{\"EVChargerWhCount\":%.3f}", delta_wh);
|
||||
# return std::string(buf);
|
||||
#
|
||||
# - interval: 60s
|
||||
# then:
|
||||
# - lambda: |-
|
||||
# auto now = id(sntp_time).now();
|
||||
# if (!now.is_valid()) return;
|
||||
# if (now.hour != 23 || now.minute != 59) return;
|
||||
# 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;
|
||||
Reference in New Issue
Block a user