From 04c2bc1332f894fff48e4d84be400885e7539594 Mon Sep 17 00:00:00 2001 From: "alexei.dolgolyov" Date: Thu, 22 Jan 2026 02:44:11 +0300 Subject: [PATCH] [Claude] - Analyze `Track Abnormal Plug Activity.yaml` file designed to work as automation blueprint for Home Assistant OS. Refactor it improving overall code quality, fix obvious or critical bugs/mistakes, fix spelling if required and add comments that will make the code more easy to read and understand. Let's also change notification message, so it will include friendly name of an entity instead of entity id. --- Common/Track Abnormal Plug Activity.yaml | 182 ++++++++++++++++++++--- 1 file changed, 162 insertions(+), 20 deletions(-) diff --git a/Common/Track Abnormal Plug Activity.yaml b/Common/Track Abnormal Plug Activity.yaml index 1ed7724..34cccb9 100644 --- a/Common/Track Abnormal Plug Activity.yaml +++ b/Common/Track Abnormal Plug Activity.yaml @@ -1,79 +1,221 @@ +# ============================================================================= +# Power Spike Tracker Blueprint +# ============================================================================= +# This blueprint monitors power sensors for abnormal consumption spikes that +# might indicate device malfunction or safety concerns. +# +# Features: +# - Monitors multiple power sensors simultaneously +# - Configurable spike detection ratio (e.g., 2x = power doubled) +# - Minimum threshold to ignore normal low-power fluctuations +# - Optional automatic switch turn-off for safety +# - Derives switch entity from sensor naming convention +# +# Naming Convention: +# The blueprint assumes sensors follow the pattern: sensor._power +# and the corresponding switch is: switch. +# Example: sensor.coffee_maker_power -> switch.coffee_maker +# ============================================================================= + blueprint: name: "Custom: Power Spike Tracker" description: > - Monitors a list of power sensors and triggers when consumption spikes - beyond a defined ratio compared to the previous reading. Optionally - turns off the associated switch. + Monitors power sensors and triggers when consumption spikes beyond a defined + ratio compared to the previous reading. Optionally turns off the associated + switch for safety. Assumes naming convention: sensor._power -> switch. domain: automation + input: + # ------------------------------------------------------------------------- + # Sensor Configuration + # ------------------------------------------------------------------------- power_sensors: name: Power Sensors - description: 'List of power sensors to monitor. Switch entity ids will be calculated by replacing `sensor.` and `_power` substrings. I.e. for `sensor.my_switch_power` switch entity must be `switch.my_switch`.' + description: > + Power sensors to monitor. Switch entities are derived by replacing + "sensor." with "switch." and removing "_power" suffix. + Example: sensor.my_plug_power -> switch.my_plug selector: entity: domain: sensor + device_class: power multiple: true + # ------------------------------------------------------------------------- + # Detection Thresholds + # ------------------------------------------------------------------------- spike_ratio: name: Power Spike Ratio - description: Ratio threshold (e.g. 2.0 means power doubled) + description: > + Minimum ratio of current/previous power to trigger alert. + Example: 2.0 means power must at least double to trigger. default: 2.0 selector: number: - min: 1.0 + min: 1.1 max: 10.0 step: 0.1 - + mode: slider + threshold_value: name: Minimum Power Threshold - description: Ignore spikes if current power is below this value + description: > + Ignore spikes if current power is below this value. + Prevents false alerts from small fluctuations at low power levels. default: 5 selector: number: min: 0 max: 5000 step: 1 - unit_of_measurement: "W" - + unit_of_measurement: W + + # ------------------------------------------------------------------------- + # Actions Configuration + # ------------------------------------------------------------------------- auto_turn_off: name: Auto Turn Off Switch - description: 'If true, turn off the switch linked to the sensor that spiked' + description: > + Automatically turn off the associated switch when a spike is detected. + Useful for safety when abnormal power draw might indicate a problem. default: false selector: boolean: + # ------------------------------------------------------------------------- + # Notification Configuration + # ------------------------------------------------------------------------- + notify_target: + name: Notification Target (optional) + description: > + Notify entity to send alerts (e.g., notify.mobile_app_phone). + Leave empty to use the default notify.notify service. + default: + selector: + entity: + domain: notify + +# Restart mode ensures rapid state changes are handled with latest values mode: restart +# ============================================================================= +# Trigger +# ============================================================================= trigger: + # Monitor all configured power sensors for state changes - platform: state entity_id: !input power_sensors + id: "power_change" +# ============================================================================= +# Variables +# ============================================================================= variables: + # Input references spike_ratio: !input spike_ratio auto_turn_off: !input auto_turn_off - threshold_value: !input threshold_value + threshold_value: !input threshold_value + notify_target: !input notify_target + + # Trigger context entity: "{{ trigger.entity_id }}" + entity_name: "{{ state_attr(trigger.entity_id, 'friendly_name') | default(trigger.entity_id) }}" prev: "{{ trigger.from_state.state | float(0) }}" curr: "{{ trigger.to_state.state | float(0) }}" - # Derive switch name if convention is consistent (sensor._power → switch.) + + # Calculate the actual spike ratio for reporting + actual_ratio: "{{ (curr / prev) | round(2) if prev > 0 else 0 }}" + + # Derive switch entity from sensor naming convention + # sensor._power -> switch. switch_entity: > {{ entity | regex_replace('^sensor\\.', 'switch.') | regex_replace('_power$', '') }} + switch_name: "{{ state_attr(switch_entity, 'friendly_name') | default(switch_entity) }}" + # Debug flag - set to true to enable persistent notifications for troubleshooting + is_debug: false + +# ============================================================================= +# Condition - Only proceed if this is a genuine power spike +# ============================================================================= condition: - condition: template value_template: > {{ curr > threshold_value and prev > 0 and (curr / prev) >= spike_ratio }} +# ============================================================================= +# Actions +# ============================================================================= action: - - service: notify.notify - data: - message: > - Power spike detected on {{ entity }}: - {{ prev }}W → {{ curr }}W (ratio {{ (curr/prev) | round(2) }}) - + # --------------------------------------------------------------------------- + # Debug Logging (optional) + # --------------------------------------------------------------------------- - choose: - - conditions: "{{ auto_turn_off }}" + - conditions: + - condition: template + value_template: "{{ is_debug }}" + sequence: + - service: persistent_notification.create + data: + title: "Power Spike Debug" + message: > + Entity: {{ entity_name }} ({{ entity }}) + Previous: {{ prev }}W + Current: {{ curr }}W + Ratio: {{ actual_ratio }}x + Threshold: {{ threshold_value }}W + Required ratio: {{ spike_ratio }}x + Switch: {{ switch_name }} ({{ switch_entity }}) + Auto turn off: {{ auto_turn_off }} + + # --------------------------------------------------------------------------- + # Send Notification + # --------------------------------------------------------------------------- + - choose: + # Use configured notify target if provided + - conditions: + - condition: template + value_template: "{{ notify_target is not none }}" + sequence: + - service: notify.send_message + target: + entity_id: "{{ notify_target }}" + data: + title: "Power Spike Detected" + message: > + {{ entity_name }}: {{ prev }}W → {{ curr }}W ({{ actual_ratio }}x increase) + # Fall back to default notify service + default: + - service: notify.notify + data: + title: "Power Spike Detected" + message: > + {{ entity_name }}: {{ prev }}W → {{ curr }}W ({{ actual_ratio }}x increase) + + # --------------------------------------------------------------------------- + # Auto Turn Off Switch (if enabled) + # --------------------------------------------------------------------------- + - choose: + - conditions: + - condition: template + value_template: "{{ auto_turn_off }}" sequence: - service: switch.turn_off target: entity_id: "{{ switch_entity }}" + + # Notify that the switch was turned off + - choose: + - conditions: + - condition: template + value_template: "{{ notify_target is not none }}" + sequence: + - service: notify.send_message + target: + entity_id: "{{ notify_target }}" + data: + message: "{{ switch_name }} has been automatically turned off for safety." + default: + - service: notify.notify + data: + message: "{{ switch_name }} has been automatically turned off for safety."