Files
haos-blueprints/Common/Track Abnormal Plug Activity.yaml

222 lines
8.4 KiB
YAML

# =============================================================================
# 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.<name>_power
# and the corresponding switch is: switch.<name>
# Example: sensor.coffee_maker_power -> switch.coffee_maker
# =============================================================================
blueprint:
name: "Custom: Power Spike Tracker"
description: >
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.<name>_power -> switch.<name>
domain: automation
input:
# -------------------------------------------------------------------------
# Sensor Configuration
# -------------------------------------------------------------------------
power_sensors:
name: Power Sensors
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: >
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.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.
Prevents false alerts from small fluctuations at low power levels.
default: 5
selector:
number:
min: 0
max: 5000
step: 1
unit_of_measurement: W
# -------------------------------------------------------------------------
# Actions Configuration
# -------------------------------------------------------------------------
auto_turn_off:
name: Auto Turn Off Switch
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
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) }}"
# 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.<name>_power -> switch.<name>
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:
# ---------------------------------------------------------------------------
# Debug Logging (optional)
# ---------------------------------------------------------------------------
- choose:
- 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."