2026-10-25 15:55:46 +00:00
|
|
|
blueprint:
|
|
|
|
|
name: Smart Battery Charging - Reusable Controller
|
2026-10-25 17:30:49 +00:00
|
|
|
description: >-
|
2026-10-26 05:33:20 +00:00
|
|
|
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.
|
2026-10-25 15:55:46 +00:00
|
|
|
domain: automation
|
|
|
|
|
input:
|
|
|
|
|
battery_sensor:
|
|
|
|
|
name: Battery percentage sensor
|
|
|
|
|
selector:
|
|
|
|
|
entity:
|
|
|
|
|
domain: sensor
|
|
|
|
|
power_switch:
|
2026-10-26 05:33:20 +00:00
|
|
|
name: Charger power switch
|
2026-10-25 15:55:46 +00:00
|
|
|
selector:
|
|
|
|
|
entity:
|
|
|
|
|
domain: switch
|
|
|
|
|
charge_start:
|
2026-10-25 17:30:49 +00:00
|
|
|
name: Start charging at or below (%)
|
2026-10-25 15:55:46 +00:00
|
|
|
default: 40
|
|
|
|
|
selector:
|
|
|
|
|
number:
|
|
|
|
|
min: 10
|
|
|
|
|
max: 90
|
2026-10-25 17:30:49 +00:00
|
|
|
step: 1
|
2026-10-25 15:55:46 +00:00
|
|
|
unit_of_measurement: "%"
|
|
|
|
|
mode: slider
|
|
|
|
|
charge_stop:
|
2026-10-25 17:30:49 +00:00
|
|
|
name: Stop charging at or above (%)
|
2026-10-25 15:55:46 +00:00
|
|
|
default: 80
|
|
|
|
|
selector:
|
|
|
|
|
number:
|
|
|
|
|
min: 20
|
|
|
|
|
max: 100
|
2026-10-25 17:30:49 +00:00
|
|
|
step: 1
|
2026-10-25 15:55:46 +00:00
|
|
|
unit_of_measurement: "%"
|
|
|
|
|
mode: slider
|
|
|
|
|
minimum_switch_interval:
|
2026-10-25 17:30:49 +00:00
|
|
|
name: Minimum observed switch-state duration (minutes)
|
|
|
|
|
description: >-
|
2026-10-26 05:33:20 +00:00
|
|
|
Applies to ordinary charging changes. Critical battery and unsafe
|
|
|
|
|
temperature shutoff bypass this delay. Based on HA state, not physical
|
|
|
|
|
relay history.
|
2026-10-25 15:55:46 +00:00
|
|
|
default: 5
|
|
|
|
|
selector:
|
|
|
|
|
number:
|
|
|
|
|
min: 0
|
|
|
|
|
max: 120
|
|
|
|
|
step: 1
|
|
|
|
|
unit_of_measurement: min
|
|
|
|
|
mode: box
|
|
|
|
|
max_sensor_age:
|
2026-10-26 05:33:20 +00:00
|
|
|
name: Maximum battery sensor update age (minutes; 0 disables)
|
2026-10-25 15:55:46 +00:00
|
|
|
default: 180
|
|
|
|
|
selector:
|
|
|
|
|
number:
|
|
|
|
|
min: 0
|
|
|
|
|
max: 1440
|
|
|
|
|
step: 1
|
|
|
|
|
unit_of_measurement: min
|
|
|
|
|
mode: box
|
|
|
|
|
enable_switching:
|
|
|
|
|
name: Enable real switching
|
2026-10-25 17:30:49 +00:00
|
|
|
default: false
|
|
|
|
|
selector:
|
|
|
|
|
boolean: {}
|
|
|
|
|
enable_logging:
|
2026-10-26 05:33:20 +00:00
|
|
|
name: Log commands and faults
|
2026-10-25 15:55:46 +00:00
|
|
|
default: false
|
|
|
|
|
selector:
|
|
|
|
|
boolean: {}
|
2026-10-26 05:33:20 +00:00
|
|
|
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: {}
|
2026-10-25 15:55:46 +00:00
|
|
|
|
2026-10-25 17:30:49 +00:00
|
|
|
mode: single
|
2026-10-25 15:55:46 +00:00
|
|
|
max_exceeded: silent
|
|
|
|
|
|
|
|
|
|
variables:
|
|
|
|
|
battery_entity: !input battery_sensor
|
|
|
|
|
switch_entity: !input power_switch
|
2026-10-26 05:33:20 +00:00
|
|
|
temp_entity: !input temperature_sensor
|
2026-10-25 15:55:46 +00:00
|
|
|
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
|
2026-10-25 17:30:49 +00:00
|
|
|
logging_enabled: !input enable_logging
|
2026-10-26 05:33:20 +00:00
|
|
|
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('-', '_') }}
|
2026-10-25 15:55:46 +00:00
|
|
|
|
|
|
|
|
triggers:
|
|
|
|
|
- trigger: state
|
|
|
|
|
entity_id: !input battery_sensor
|
|
|
|
|
- trigger: state
|
|
|
|
|
entity_id: !input power_switch
|
|
|
|
|
- trigger: time_pattern
|
|
|
|
|
minutes: "/1"
|
|
|
|
|
- trigger: homeassistant
|
|
|
|
|
event: start
|
|
|
|
|
|
2026-10-25 17:30:49 +00:00
|
|
|
conditions: []
|
2026-10-25 15:55:46 +00:00
|
|
|
|
|
|
|
|
actions:
|
|
|
|
|
- variables:
|
2026-10-26 05:33:20 +00:00
|
|
|
battery_raw: "{{ states(battery_entity) }}"
|
2026-10-25 17:30:49 +00:00
|
|
|
switch_state: "{{ states(switch_entity) }}"
|
|
|
|
|
battery_valid: >-
|
|
|
|
|
{{ is_number(states(battery_entity)) and
|
|
|
|
|
0 <= states(battery_entity) | float(-1) <= 100 }}
|
|
|
|
|
thresholds_valid: >-
|
2026-10-26 05:33:20 +00:00
|
|
|
{{ 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] %}
|
2026-10-25 17:30:49 +00:00
|
|
|
{{ max_age | float(0) <= 0 or
|
2026-10-26 05:33:20 +00:00
|
|
|
(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 }}
|
2026-10-25 17:30:49 +00:00
|
|
|
switch_stable: >-
|
2026-10-26 05:33:20 +00:00
|
|
|
{% set obj = states[switch_entity] %}
|
|
|
|
|
{{ obj is not none and
|
|
|
|
|
(as_timestamp(now()) - as_timestamp(obj.last_changed, 0))
|
|
|
|
|
>= min_interval | float(0) * 60 }}
|
2026-10-25 17:30:49 +00:00
|
|
|
- variables:
|
2026-10-26 05:33:20 +00:00
|
|
|
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' }}"
|
2026-10-25 17:30:49 +00:00
|
|
|
desired_action: >-
|
2026-10-26 05:33:20 +00:00
|
|
|
{% 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' %}
|
2026-10-25 17:30:49 +00:00
|
|
|
off
|
2026-10-26 05:33:20 +00:00
|
|
|
{% elif battery_raw | float(101) <= start_threshold | float and
|
|
|
|
|
switch_state == 'off' and temperature_can_start | bool(false) %}
|
|
|
|
|
on
|
2026-10-25 15:55:46 +00:00
|
|
|
{% else %}
|
2026-10-25 17:30:49 +00:00
|
|
|
hold
|
2026-10-25 15:55:46 +00:00
|
|
|
{% endif %}
|
2026-10-25 17:30:49 +00:00
|
|
|
- if:
|
|
|
|
|
- condition: template
|
2026-10-26 05:33:20 +00:00
|
|
|
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') }}
|
2026-10-25 17:30:49 +00:00
|
|
|
then:
|
|
|
|
|
- action: logbook.log
|
|
|
|
|
data:
|
|
|
|
|
name: Smart Battery Charging
|
|
|
|
|
entity_id: "{{ battery_entity }}"
|
|
|
|
|
message: >-
|
2026-10-26 05:33:20 +00:00
|
|
|
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 }}.
|
2026-10-25 15:55:46 +00:00
|
|
|
- condition: template
|
2026-10-25 17:30:49 +00:00
|
|
|
value_template: "{{ switching_enabled | bool(false) }}"
|
2026-10-25 15:55:46 +00:00
|
|
|
- choose:
|
2026-10-25 17:30:49 +00:00
|
|
|
- conditions:
|
2026-10-25 15:55:46 +00:00
|
|
|
- condition: template
|
2026-10-26 05:33:20 +00:00
|
|
|
value_template: "{{ desired_action | trim == 'off_safety' }}"
|
2026-10-25 15:55:46 +00:00
|
|
|
sequence:
|
2026-10-26 05:33:20 +00:00
|
|
|
- action: switch.turn_off
|
2026-10-25 15:55:46 +00:00
|
|
|
target:
|
|
|
|
|
entity_id: !input power_switch
|
2026-10-25 17:30:49 +00:00
|
|
|
- conditions:
|
2026-10-25 15:55:46 +00:00
|
|
|
- condition: template
|
2026-10-26 05:33:20 +00:00
|
|
|
value_template: >-
|
|
|
|
|
{{ desired_action | trim == 'off' and switch_stable | bool(false) }}
|
2026-10-25 15:55:46 +00:00
|
|
|
sequence:
|
|
|
|
|
- action: switch.turn_off
|
|
|
|
|
target:
|
|
|
|
|
entity_id: !input power_switch
|
2026-10-26 05:33:20 +00:00
|
|
|
- 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
|