From 5c5256caa92647df6868855622d5bfaf04dd5923 Mon Sep 17 00:00:00 2001 From: ESPHome Device Builder Date: Sun, 12 Jul 2026 13:05:32 +1200 Subject: [PATCH] Edit esp-3dprinterpow.yaml --- esphome/esp-3dprinterpow.yaml | 134 +++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 3 deletions(-) diff --git a/esphome/esp-3dprinterpow.yaml b/esphome/esp-3dprinterpow.yaml index 14833bb..0c09d68 100644 --- a/esphome/esp-3dprinterpow.yaml +++ b/esphome/esp-3dprinterpow.yaml @@ -6,6 +6,12 @@ # https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-3dprinterpow.yaml #:########################################################################################:# # VERSIONS: +# V1.3 2026-07-12 +# - Added cumulative Load Energy integration sensor for Home Assistant. +# - Added seven-day maximum power sensor. +# - Maximum power resets after 168 powered hours. +# - Added a single-process compile limit to reduce Builder memory use. +# # V1.2 2026-07-11 # - Removed the optimistic template relay switch. # - Exposed the actual GPIO12 relay directly to Home Assistant. @@ -25,6 +31,16 @@ #:########################################################################################:# # OPERATION NOTES: # - Inline power monitor using the HLW8012. +# - Reports current, voltage, instantaneous power, and cumulative energy. +# - Load Energy integrates measured power over time and reports kWh. +# - Load Energy resets to zero if the ESPHome device reboots. +# - Home Assistant handles the reset because the sensor uses total_increasing. +# - Load Maximum Power 7 Days records the highest measured power. +# - Maximum power resets after 168 powered hours. +# - Maximum power and the elapsed-hour counter are retained across reboots. +# - The maximum value is checked for saving every five minutes. +# - The elapsed-hour counter is checked for saving every six hours. +# - An unexpected power loss may extend the peak reset period by up to about six hours. # - GPIO12 directly controls the 3D printer power relay. # - The physical button toggles the real relay. # - Home Assistant directly controls and reports the real relay state. @@ -51,21 +67,23 @@ # a) Home Assistant OFFLINE, but Network and MQTT ONLINE # - Device remains controllable through the MQTT local command topic. # - Relay status changes continue to publish through MQTT. +# - Energy integration and maximum power monitoring continue locally. # - Home Assistant entities remain unavailable until HA returns. # # b) MQTT OFFLINE, but WiFi/Network and HA API ONLINE # - Device remains controllable from Home Assistant through the native API. # - Local MQTT commands do not work while MQTT is unavailable. +# - Energy integration and maximum power monitoring continue locally. # - MQTT status publishing resumes when MQTT reconnects. # # c) Entire WiFi/Network OFFLINE # - No Home Assistant API or MQTT control is available. # - The local physical button continues to toggle the relay. # - The status LED continues to follow the real relay state. -# - Accurate time is not needed. +# - Energy integration and maximum power monitoring continue locally. +# - Accurate time is not needed for the seven-day powered-time reset. #:########################################################################################:# - #:########################################################################################:# # SUBSTITUTIONS: Specific device variable substitutions # If not using a secrets file, replace these with the required values in quotes. @@ -79,7 +97,7 @@ substitutions: # Project Naming project_name: "Sonoff Technologies.POW R1" - project_version: "v1.2" + project_version: "v1.3" # Passwords and Secrets api_key: !secret esp-api_key @@ -151,6 +169,9 @@ esphome: comment: "${description_comment}" area: "${device_area}" + # Restrict PlatformIO to one simultaneous compiler process to reduce host RAM use. + compile_process_limit: 1 + project: name: "${project_name}" version: "${project_version}" @@ -212,6 +233,28 @@ mqtt: then: - switch.turn_off: relay +#:########################################################################################:# +# GLOBALS: +# Retained maximum power and elapsed-hour counter +# https://esphome.io/components/globals.html +#:########################################################################################:# +globals: + # Highest measured power during the current seven-day period. + # Changed values are checked for saving every five minutes. + - id: max_power_7d + type: float + restore_value: true + initial_value: '0.0' + update_interval: 5min + + # Powered hours elapsed during the current seven-day period. + # Changed values are checked for saving every six hours to reduce flash writes. + - id: max_power_hours_elapsed + type: uint16_t + restore_value: true + initial_value: '0' + update_interval: 6h + #:########################################################################################:# # BINARY SENSORS: # https://esphome.io/components/binary_sensor/ @@ -257,13 +300,98 @@ sensor: current: name: "${entity_prefix} Current" + device_class: current + state_class: measurement + unit_of_measurement: "A" + accuracy_decimals: 2 voltage: name: "${entity_prefix} Voltage" + device_class: voltage + state_class: measurement + unit_of_measurement: "V" + accuracy_decimals: 1 power: name: "${entity_prefix} Power" id: power + device_class: power + state_class: measurement + unit_of_measurement: "W" + accuracy_decimals: 1 + + # Update the seven-day maximum whenever a new peak is measured. + on_value: + then: + - lambda: |- + if (!isnan(x) && x > id(max_power_7d)) { + id(max_power_7d) = x; + id(max_power_7d_sensor).publish_state(x); + + ESP_LOGI( + "max_power", + "New seven-day maximum power: %.1f W", + x + ); + } + + # Integrates instantaneous watts over time. + # Watts integrated over hours produces Wh; the filter converts Wh to kWh. + - platform: integration + name: "${entity_prefix} Energy" + id: energy + sensor: power + time_unit: h + integration_method: trapezoid + restore: false + unit_of_measurement: "kWh" + device_class: energy + state_class: total_increasing + accuracy_decimals: 3 + filters: + - multiply: 0.001 + + # Highest power recorded during the current 168 powered-hour period. + - platform: template + name: "${entity_prefix} Maximum Power 7 Days" + id: max_power_7d_sensor + unit_of_measurement: "W" + device_class: power + state_class: measurement + accuracy_decimals: 1 + update_interval: 60s + lambda: |- + return id(max_power_7d); + +#:########################################################################################:# +# INTERVAL: +# Seven-day maximum power reset +# https://esphome.io/components/interval.html +#:########################################################################################:# +interval: + # Count one powered hour at a time. + # 168 hours equals seven days. + - interval: 1h + then: + - lambda: |- + id(max_power_hours_elapsed) += 1; + + ESP_LOGD( + "max_power", + "Seven-day maximum power period: %u of 168 powered hours elapsed", + id(max_power_hours_elapsed) + ); + + if (id(max_power_hours_elapsed) >= 168) { + id(max_power_hours_elapsed) = 0; + id(max_power_7d) = 0.0f; + id(max_power_7d_sensor).publish_state(0.0f); + + ESP_LOGI( + "max_power", + "Seven-day maximum power value has been reset" + ); + } #:########################################################################################:# # SWITCH COMPONENT: