homeassistant-blueprints/automation/Battery_control/smart_battery_charging.yaml

318 lines
11 KiB
YAML

blueprint:
name: Smart Battery Charging - Reusable Controller
description: >-
v3: Reusable charging controller with optional battery temperature protection,
critical-battery response, fault notifications, and diagnostics.
Verify the switch and battery sensor before enabling real switching.
Temperature thresholds apply to battery temperature in Celsius, not room temperature.
domain: automation
input:
battery_sensor:
name: Battery percentage sensor
selector:
entity:
domain: sensor
power_switch:
name: Charger power switch
selector:
entity:
domain: switch
charge_start:
name: Start charging at or below (%)
default: 40
selector:
number:
min: 10
max: 90
step: 1
unit_of_measurement: "%"
mode: slider
charge_stop:
name: Stop charging at or above (%)
default: 80
selector:
number:
min: 20
max: 100
step: 1
unit_of_measurement: "%"
mode: slider
minimum_switch_interval:
name: Minimum observed switch-state duration (minutes)
description: >-
Applies to ordinary charging changes. Critical battery and unsafe
temperature shutoff bypass this delay. Based on HA state, not physical
relay history.
default: 5
selector:
number:
min: 0
max: 120
step: 1
unit_of_measurement: min
mode: box
max_sensor_age:
name: Maximum battery sensor update age (minutes; 0 disables)
default: 180
selector:
number:
min: 0
max: 1440
step: 1
unit_of_measurement: min
mode: box
enable_switching:
name: Enable real switching
default: false
selector:
boolean: {}
enable_logging:
name: Log commands and faults
default: false
selector:
boolean: {}
temperature_sensor:
name: Battery temperature sensor (optional)
description: >-
Leave empty to disable all temperature checks. Must report actual
battery temperature in degrees Celsius.
default: ""
selector:
entity:
domain: sensor
cold_cutoff:
name: Cold charging cutoff (°C)
default: 5
selector:
number:
min: 0
max: 15
step: 1
unit_of_measurement: "°C"
mode: slider
hot_cutoff:
name: Hot charging cutoff (°C)
default: 40
selector:
number:
min: 30
max: 50
step: 1
unit_of_measurement: "°C"
mode: slider
temperature_hysteresis:
name: Temperature recovery margin (°C)
description: >-
Charging may restart only above cold cutoff plus this margin and
below hot cutoff minus this margin.
default: 3
selector:
number:
min: 1
max: 5
step: 1
unit_of_measurement: "°C"
mode: slider
critical_battery:
name: Critical battery level (%)
description: >-
At or below this level, charging ON bypasses the normal minimum
switch-state delay, but never bypasses temperature safety.
default: 20
selector:
number:
min: 5
max: 30
step: 1
unit_of_measurement: "%"
mode: slider
enable_fault_notifications:
name: Create Home Assistant persistent fault notifications
default: true
selector:
boolean: {}
mode: single
max_exceeded: silent
variables:
battery_entity: !input battery_sensor
switch_entity: !input power_switch
temp_entity: !input temperature_sensor
start_threshold: !input charge_start
stop_threshold: !input charge_stop
min_interval: !input minimum_switch_interval
max_age: !input max_sensor_age
switching_enabled: !input enable_switching
logging_enabled: !input enable_logging
cold_limit: !input cold_cutoff
hot_limit: !input hot_cutoff
temp_margin: !input temperature_hysteresis
critical_limit: !input critical_battery
notifications_enabled: !input enable_fault_notifications
notification_id: >-
{{ ('battery_charge_' ~ battery_entity) | replace('.', '_') | replace('-', '_') }}
triggers:
- trigger: state
entity_id: !input battery_sensor
- trigger: state
entity_id: !input power_switch
- trigger: time_pattern
minutes: "/1"
- trigger: homeassistant
event: start
conditions: []
actions:
- variables:
battery_raw: "{{ states(battery_entity) }}"
switch_state: "{{ states(switch_entity) }}"
battery_valid: >-
{{ is_number(states(battery_entity)) and
0 <= states(battery_entity) | float(-1) <= 100 }}
thresholds_valid: >-
{{ 0 <= start_threshold | float(-1) < stop_threshold | float(-1) <= 100
and critical_limit | float(-1) <= start_threshold | float(-1) }}
battery_fresh: >-
{% set obj = states[battery_entity] %}
{{ max_age | float(0) <= 0 or
(obj is not none and
(as_timestamp(now()) - as_timestamp(obj.last_updated, 0))
<= max_age | float(0) * 60) }}
temperature_enabled: "{{ temp_entity | length > 0 }}"
temperature_valid: >-
{% if temp_entity | length == 0 %}
true
{% else %}
{% set raw = states(temp_entity) %}
{% set obj = states[temp_entity] %}
{{ is_number(raw) and -20 <= raw | float(-999) <= 100
and obj is not none
and (max_age | float(0) <= 0 or
(as_timestamp(now()) - as_timestamp(obj.last_updated, 0))
<= max_age | float(0) * 60) }}
{% endif %}
temperature_c: >-
{{ states(temp_entity) | float(-999) if temp_entity | length > 0 else -999 }}
switch_stable: >-
{% set obj = states[switch_entity] %}
{{ obj is not none and
(as_timestamp(now()) - as_timestamp(obj.last_changed, 0))
>= min_interval | float(0) * 60 }}
- variables:
temperature_unsafe: >-
{{ temperature_enabled | bool(false) and
(not temperature_valid | bool(false) or
temperature_c | float(-999) <= cold_limit | float or
temperature_c | float(-999) >= hot_limit | float) }}
temperature_can_start: >-
{{ not temperature_enabled | bool(false) or
(temperature_valid | bool(false) and
temperature_c | float(-999) >= (cold_limit | float + temp_margin | float) and
temperature_c | float(-999) <= (hot_limit | float - temp_margin | float)) }}
critical_now: >-
{{ battery_valid | bool(false) and
battery_raw | float(101) <= critical_limit | float }}
fault_reason: >-
{% if not thresholds_valid | bool(false) %}
Invalid charging thresholds
{% elif not battery_valid | bool(false) %}
Battery sensor missing or invalid
{% elif not battery_fresh | bool(false) %}
Battery sensor stale
{% elif switch_state not in ['on', 'off'] %}
Power switch unavailable
{% elif temperature_enabled | bool(false) and not temperature_valid | bool(false) %}
Battery temperature sensor missing, invalid or stale
{% elif temperature_enabled | bool(false) and temperature_unsafe | bool(false) %}
Battery temperature outside permitted charging range
{% elif critical_now | bool(false) and switch_state == 'off' and
not temperature_can_start | bool(false) %}
Critical battery; temperature not yet in recovery range
{% else %}
none
{% endif %}
- variables:
fault: "{{ fault_reason | trim != 'none' }}"
desired_action: >-
{% if switch_state not in ['on', 'off'] %}
hold
{% elif temperature_unsafe | bool(false) and switch_state == 'on' %}
off_safety
{% elif not thresholds_valid | bool(false) or
not battery_valid | bool(false) or
not battery_fresh | bool(false) %}
hold
{% elif battery_raw | float(0) >= stop_threshold | float and switch_state == 'on' %}
off
{% elif battery_raw | float(101) <= start_threshold | float and
switch_state == 'off' and temperature_can_start | bool(false) %}
on
{% else %}
hold
{% endif %}
- if:
- condition: template
value_template: "{{ notifications_enabled | bool(false) and fault | bool(false) }}"
then:
- action: persistent_notification.create
data:
notification_id: "{{ notification_id | trim }}"
title: "Battery charging controller: {{ battery_entity }}"
message: >-
{{ fault_reason | trim }}. Battery={{ battery_raw }}%,
temperature={{ temperature_c if temperature_enabled | bool(false) else 'not configured' }},
switch={{ switch_state }}. Check device and Home Assistant.
- if:
- condition: template
value_template: "{{ notifications_enabled | bool(false) and not fault | bool(false) }}"
then:
- action: persistent_notification.dismiss
continue_on_error: true
data:
notification_id: "{{ notification_id | trim }}"
- if:
- condition: template
value_template: >-
{{ logging_enabled | bool(false) and
(fault | bool(false) or desired_action | trim != 'hold') }}
then:
- action: logbook.log
data:
name: Smart Battery Charging
entity_id: "{{ battery_entity }}"
message: >-
Battery={{ battery_raw }}%, switch={{ switch_state }},
temperature={{ temperature_c if temperature_enabled | bool(false) else 'disabled' }},
decision={{ desired_action | trim }}, fault={{ fault_reason | trim }},
enabled={{ switching_enabled }}, stable={{ switch_stable }}.
- condition: template
value_template: "{{ switching_enabled | bool(false) }}"
- choose:
- conditions:
- condition: template
value_template: "{{ desired_action | trim == 'off_safety' }}"
sequence:
- action: switch.turn_off
target:
entity_id: !input power_switch
- conditions:
- condition: template
value_template: >-
{{ desired_action | trim == 'off' and switch_stable | bool(false) }}
sequence:
- action: switch.turn_off
target:
entity_id: !input power_switch
- conditions:
- condition: template
value_template: >-
{{ desired_action | trim == 'on' and
(switch_stable | bool(false) or critical_now | bool(false)) }}
sequence:
- action: switch.turn_on
target:
entity_id: !input power_switch