Update automation/Battery_control/smart_battery_charging.yaml

This commit is contained in:
Dejan 2026-10-25 17:30:49 +00:00
parent 7c1ef989c5
commit 220bf7e1db

View file

@ -1,81 +1,49 @@
blueprint: blueprint:
name: Smart Battery Charging - Reusable Controller name: Smart Battery Charging - Reusable Controller
description: > description: >-
Reusable battery charging controller for Android tablets, Version 2. Reusable battery charging control with configurable hysteresis,
phones and other battery-powered devices. read-only commissioning mode, input validation and optional log messages.
A valid numeric battery percentage sensor and a switch entity are required.
Select a battery percentage sensor and a power switch. The sensor freshness guard uses last_updated (which can include attribute
Charging starts at the lower threshold and stops at updates). This does not replace the tablet's internal battery protection.
the upper threshold.
Features:
- Adjustable charging thresholds
- Hysteresis to avoid rapid switching
- Monitoring-only mode for safe commissioning
- Battery sensor validation
- Optional stale sensor protection
- Minimum switch-state duration
- Periodic evaluation and HA restart recovery
- Works with Zigbee2MQTT, ZHA and other HA switches
IMPORTANT:
Start with real switching disabled. Verify sensor values,
switch entity, wiring and power restoration before enabling.
domain: automation domain: automation
input: input:
battery_sensor: battery_sensor:
name: Battery percentage sensor name: Battery percentage sensor
description: >
Sensor reporting battery level between 0 and 100.
selector: selector:
entity: entity:
domain: sensor domain: sensor
power_switch: power_switch:
name: Charging power switch name: Charging power switch
description: >
Switch controlling power to the charger.
Supports Zigbee2MQTT, ZHA and other switch entities.
selector: selector:
entity: entity:
domain: switch domain: switch
charge_start: charge_start:
name: Start charging at (%) name: Start charging at or below (%)
description: >
Turn charging ON when battery is at or below
this percentage.
default: 40 default: 40
selector: selector:
number: number:
min: 10 min: 10
max: 90 max: 90
step: 5 step: 1
unit_of_measurement: "%" unit_of_measurement: "%"
mode: slider mode: slider
charge_stop: charge_stop:
name: Stop charging at (%) name: Stop charging at or above (%)
description: >
Turn charging OFF when battery reaches or exceeds
this percentage.
default: 80 default: 80
selector: selector:
number: number:
min: 20 min: 20
max: 100 max: 100
step: 5 step: 1
unit_of_measurement: "%" unit_of_measurement: "%"
mode: slider mode: slider
minimum_switch_interval: minimum_switch_interval:
name: Minimum switch-state duration (minutes) name: Minimum observed switch-state duration (minutes)
description: > description: >-
Minimum time the switch must remain in its Prevent switching until Home Assistant has observed the current switch
observed state before another command is allowed. state for this long. Not a persistent hardware switching timestamp.
default: 5 default: 5
selector: selector:
number: number:
@ -84,162 +52,114 @@ blueprint:
step: 1 step: 1
unit_of_measurement: min unit_of_measurement: min
mode: box mode: box
max_sensor_age: max_sensor_age:
name: Maximum sensor age (minutes) name: Maximum sensor update age (minutes, 0 disables)
description: >
Maximum time since the battery sensor last
reported a state update. Set to 0 to disable
the age check.
Note: last_updated may advance on attribute
updates, so this is not proof of a fresh
battery measurement.
default: 180 default: 180
selector: selector:
number: number:
min: 0 min: 0
max: 1440 max: 1440
step: 10
unit_of_measurement: min
mode: box
evaluation_interval:
name: Evaluation interval (minutes)
description: >
How often to evaluate charging conditions.
Battery changes are also evaluated immediately.
default: 5
selector:
number:
min: 1
max: 60
step: 1 step: 1
unit_of_measurement: min unit_of_measurement: min
mode: box mode: box
enable_switching: enable_switching:
name: Enable real switching name: Enable real switching
description: > description: Keep disabled until the sensor and switch are verified.
OFF = Monitoring-only mode. default: false
ON = Automation may control the power switch. selector:
boolean: {}
Keep OFF during initial testing. enable_logging:
name: Write decisions to Home Assistant logbook
default: false default: false
selector: selector:
boolean: {} boolean: {}
mode: restart mode: single
max_exceeded: silent 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
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
interval_minutes: !input evaluation_interval
switching_enabled: !input enable_switching switching_enabled: !input enable_switching
logging_enabled: !input enable_logging
triggers: triggers:
- trigger: state - trigger: state
entity_id: !input battery_sensor entity_id: !input battery_sensor
id: battery_changed
- trigger: state - trigger: state
entity_id: !input power_switch entity_id: !input power_switch
id: switch_changed
- trigger: time_pattern - trigger: time_pattern
minutes: "/1" minutes: "/1"
id: periodic_check
- trigger: homeassistant - trigger: homeassistant
event: start event: start
id: ha_started
conditions: conditions: []
- condition: template
value_template: >
{{ switching_enabled | bool(false) }}
- condition: template
value_template: >
{{ start_threshold | float(0)
< stop_threshold | float(0) }}
- condition: template
value_template: >
{% set value = states(battery_entity) %}
{{ is_number(value)
and 0 <= value | float(0) <= 100 }}
- condition: template
value_template: >
{{ states(switch_entity) in ['on', 'off'] }}
- condition: template
value_template: >
{% if max_age | float(0) <= 0 %}
true
{% else %}
{% set sensor = states.get(battery_entity) %}
{{ sensor is not none
and (as_timestamp(now()) -
as_timestamp(sensor.last_updated, 0))
<= max_age | float(0) * 60 }}
{% endif %}
actions: actions:
- variables: - variables:
battery: "{{ states(battery_entity) | float(0) }}" raw_battery: "{{ states(battery_entity) }}"
current_switch_state: "{{ states(switch_entity) }}" switch_state: "{{ states(switch_entity) }}"
battery_valid: >-
elapsed_switch_seconds: > {{ is_number(states(battery_entity)) and
{% set sw = states.get(switch_entity) %} 0 <= states(battery_entity) | float(-1) <= 100 }}
{% if sw is not none %} thresholds_valid: >-
{{ as_timestamp(now()) - {{ start_threshold | float(0) < stop_threshold | float(0) }}
as_timestamp(sw.last_changed, 0) }} sensor_fresh: >-
{% set sensor = states[battery_entity] %}
{{ max_age | float(0) <= 0 or
(sensor is not none and
(as_timestamp(now()) - as_timestamp(sensor.last_updated, 0))
<= max_age | float(0) * 60) }}
switch_stable: >-
{% set sw = states[switch_entity] %}
{{ sw is not none and
(as_timestamp(now()) - as_timestamp(sw.last_changed, 0))
>= min_interval | float(0) * 60 }}
- variables:
desired_action: >-
{% if not battery_valid | bool(false) or
not thresholds_valid | bool(false) or
not sensor_fresh | bool(false) or
switch_state not in ['on', 'off'] %}
blocked
{% elif raw_battery | float(0) <= start_threshold | float(0) and switch_state == 'off' %}
on
{% elif raw_battery | float(0) >= stop_threshold | float(0) and switch_state == 'on' %}
off
{% else %} {% else %}
0 hold
{% endif %} {% endif %}
- if:
- condition: template
value_template: "{{ logging_enabled | bool(false) }}"
then:
- action: logbook.log
data:
name: Smart Battery Charging
entity_id: "{{ battery_entity }}"
message: >-
Battery={{ raw_battery }}%, switch={{ switch_state }},
decision={{ desired_action | trim }}, enabled={{ switching_enabled }},
stable={{ switch_stable }}, fresh={{ sensor_fresh }}.
- condition: template - condition: template
value_template: > value_template: "{{ switching_enabled | bool(false) }}"
{{ elapsed_switch_seconds | float(0) - condition: template
>= min_interval | float(0) * 60 }} value_template: "{{ switch_stable | bool(false) }}"
- choose: - choose:
- alias: "Battery low - enable charging" - conditions:
conditions:
- condition: template - condition: template
value_template: > value_template: "{{ desired_action | trim == 'on' }}"
{{ battery | float(0)
<= start_threshold | float(0) }}
- condition: template
value_template: >
{{ current_switch_state == 'off' }}
sequence: sequence:
- action: switch.turn_on - action: switch.turn_on
target: target:
entity_id: !input power_switch entity_id: !input power_switch
- conditions:
- alias: "Battery high - disable charging"
conditions:
- condition: template - condition: template
value_template: > value_template: "{{ desired_action | trim == 'off' }}"
{{ battery | float(0)
>= stop_threshold | float(0) }}
- condition: template
value_template: >
{{ current_switch_state == 'on' }}
sequence: sequence:
- action: switch.turn_off - action: switch.turn_off
target: target: