Update automation/Battery_control/smart_battery_charging.yaml

This commit is contained in:
Dejan 2026-10-26 05:33:20 +00:00
parent 220bf7e1db
commit 1abb00bc35

View file

@ -1,12 +1,10 @@
blueprint: blueprint:
name: Smart Battery Charging - Reusable Controller name: Smart Battery Charging - Reusable Controller
description: >- description: >-
Version 2. Reusable battery charging control with configurable hysteresis, v3: Reusable charging controller with optional battery temperature protection,
read-only commissioning mode, input validation and optional log messages. critical-battery response, fault notifications, and diagnostics.
A valid numeric battery percentage sensor and a switch entity are required. Verify the switch and battery sensor before enabling real switching.
The sensor freshness guard uses last_updated (which can include attribute Temperature thresholds apply to battery temperature in Celsius, not room temperature.
updates). This does not replace the tablet's internal battery protection.
domain: automation domain: automation
input: input:
battery_sensor: battery_sensor:
@ -15,7 +13,7 @@ blueprint:
entity: entity:
domain: sensor domain: sensor
power_switch: power_switch:
name: Charging power switch name: Charger power switch
selector: selector:
entity: entity:
domain: switch domain: switch
@ -42,8 +40,9 @@ blueprint:
minimum_switch_interval: minimum_switch_interval:
name: Minimum observed switch-state duration (minutes) name: Minimum observed switch-state duration (minutes)
description: >- description: >-
Prevent switching until Home Assistant has observed the current switch Applies to ordinary charging changes. Critical battery and unsafe
state for this long. Not a persistent hardware switching timestamp. temperature shutoff bypass this delay. Based on HA state, not physical
relay history.
default: 5 default: 5
selector: selector:
number: number:
@ -53,7 +52,7 @@ blueprint:
unit_of_measurement: min unit_of_measurement: min
mode: box mode: box
max_sensor_age: max_sensor_age:
name: Maximum sensor update age (minutes, 0 disables) name: Maximum battery sensor update age (minutes; 0 disables)
default: 180 default: 180
selector: selector:
number: number:
@ -64,15 +63,74 @@ blueprint:
mode: box mode: box
enable_switching: enable_switching:
name: Enable real switching name: Enable real switching
description: Keep disabled until the sensor and switch are verified.
default: false default: false
selector: selector:
boolean: {} boolean: {}
enable_logging: enable_logging:
name: Write decisions to Home Assistant logbook name: Log commands and faults
default: false default: false
selector: selector:
boolean: {} 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 mode: single
max_exceeded: silent max_exceeded: silent
@ -80,12 +138,20 @@ max_exceeded: silent
variables: variables:
battery_entity: !input battery_sensor battery_entity: !input battery_sensor
switch_entity: !input power_switch switch_entity: !input power_switch
temp_entity: !input temperature_sensor
start_threshold: !input charge_start start_threshold: !input charge_start
stop_threshold: !input charge_stop stop_threshold: !input charge_stop
min_interval: !input minimum_switch_interval min_interval: !input minimum_switch_interval
max_age: !input max_sensor_age max_age: !input max_sensor_age
switching_enabled: !input enable_switching switching_enabled: !input enable_switching
logging_enabled: !input enable_logging 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: triggers:
- trigger: state - trigger: state
@ -101,66 +167,151 @@ conditions: []
actions: actions:
- variables: - variables:
raw_battery: "{{ states(battery_entity) }}" battery_raw: "{{ states(battery_entity) }}"
switch_state: "{{ states(switch_entity) }}" switch_state: "{{ states(switch_entity) }}"
battery_valid: >- battery_valid: >-
{{ is_number(states(battery_entity)) and {{ is_number(states(battery_entity)) and
0 <= states(battery_entity) | float(-1) <= 100 }} 0 <= states(battery_entity) | float(-1) <= 100 }}
thresholds_valid: >- thresholds_valid: >-
{{ start_threshold | float(0) < stop_threshold | float(0) }} {{ 0 <= start_threshold | float(-1) < stop_threshold | float(-1) <= 100
sensor_fresh: >- and critical_limit | float(-1) <= start_threshold | float(-1) }}
{% set sensor = states[battery_entity] %} battery_fresh: >-
{% set obj = states[battery_entity] %}
{{ max_age | float(0) <= 0 or {{ max_age | float(0) <= 0 or
(sensor is not none and (obj is not none and
(as_timestamp(now()) - as_timestamp(sensor.last_updated, 0)) (as_timestamp(now()) - as_timestamp(obj.last_updated, 0))
<= max_age | float(0) * 60) }} <= 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: >- switch_stable: >-
{% set sw = states[switch_entity] %} {% set obj = states[switch_entity] %}
{{ sw is not none and {{ obj is not none and
(as_timestamp(now()) - as_timestamp(sw.last_changed, 0)) (as_timestamp(now()) - as_timestamp(obj.last_changed, 0))
>= min_interval | float(0) * 60 }} >= min_interval | float(0) * 60 }}
- variables: - 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: >- desired_action: >-
{% if not battery_valid | bool(false) or {% if switch_state not in ['on', 'off'] %}
not thresholds_valid | bool(false) or hold
not sensor_fresh | bool(false) or {% elif temperature_unsafe | bool(false) and switch_state == 'on' %}
switch_state not in ['on', 'off'] %} off_safety
blocked {% elif not thresholds_valid | bool(false) or
{% elif raw_battery | float(0) <= start_threshold | float(0) and switch_state == 'off' %} not battery_valid | bool(false) or
on not battery_fresh | bool(false) %}
{% elif raw_battery | float(0) >= stop_threshold | float(0) and switch_state == 'on' %} hold
{% elif battery_raw | float(0) >= stop_threshold | float and switch_state == 'on' %}
off off
{% elif battery_raw | float(101) <= start_threshold | float and
switch_state == 'off' and temperature_can_start | bool(false) %}
on
{% else %} {% else %}
hold hold
{% endif %} {% endif %}
- if: - if:
- condition: template - condition: template
value_template: "{{ logging_enabled | bool(false) }}" 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: then:
- action: logbook.log - action: logbook.log
data: data:
name: Smart Battery Charging name: Smart Battery Charging
entity_id: "{{ battery_entity }}" entity_id: "{{ battery_entity }}"
message: >- message: >-
Battery={{ raw_battery }}%, switch={{ switch_state }}, Battery={{ battery_raw }}%, switch={{ switch_state }},
decision={{ desired_action | trim }}, enabled={{ switching_enabled }}, temperature={{ temperature_c if temperature_enabled | bool(false) else 'disabled' }},
stable={{ switch_stable }}, fresh={{ sensor_fresh }}. decision={{ desired_action | trim }}, fault={{ fault_reason | trim }},
enabled={{ switching_enabled }}, stable={{ switch_stable }}.
- condition: template - condition: template
value_template: "{{ switching_enabled | bool(false) }}" value_template: "{{ switching_enabled | bool(false) }}"
- condition: template
value_template: "{{ switch_stable | bool(false) }}"
- choose: - choose:
- conditions: - conditions:
- condition: template - condition: template
value_template: "{{ desired_action | trim == 'on' }}" value_template: "{{ desired_action | trim == 'off_safety' }}"
sequence:
- action: switch.turn_on
target:
entity_id: !input power_switch
- conditions:
- condition: template
value_template: "{{ desired_action | trim == 'off' }}"
sequence: sequence:
- action: switch.turn_off - action: switch.turn_off
target: target:
entity_id: !input power_switch 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