2026-10-25 15:55:46 +00:00
|
|
|
|
|
|
|
|
blueprint:
|
|
|
|
|
name: Smart Battery Charging - Reusable Controller
|
2026-10-25 17:30:49 +00:00
|
|
|
description: >-
|
|
|
|
|
Version 2. Reusable battery charging control with configurable hysteresis,
|
|
|
|
|
read-only commissioning mode, input validation and optional log messages.
|
|
|
|
|
A valid numeric battery percentage sensor and a switch entity are required.
|
|
|
|
|
The sensor freshness guard uses last_updated (which can include attribute
|
|
|
|
|
updates). This does not replace the tablet's internal battery protection.
|
2026-10-25 15:55:46 +00:00
|
|
|
domain: automation
|
|
|
|
|
input:
|
|
|
|
|
battery_sensor:
|
|
|
|
|
name: Battery percentage sensor
|
|
|
|
|
selector:
|
|
|
|
|
entity:
|
|
|
|
|
domain: sensor
|
|
|
|
|
power_switch:
|
|
|
|
|
name: Charging power switch
|
|
|
|
|
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: >-
|
|
|
|
|
Prevent switching until Home Assistant has observed the current switch
|
|
|
|
|
state for this long. Not a persistent hardware switching timestamp.
|
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-25 17:30:49 +00:00
|
|
|
name: Maximum 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
|
|
|
description: Keep disabled until the sensor and switch are verified.
|
|
|
|
|
default: false
|
|
|
|
|
selector:
|
|
|
|
|
boolean: {}
|
|
|
|
|
enable_logging:
|
|
|
|
|
name: Write decisions to Home Assistant logbook
|
2026-10-25 15:55:46 +00:00
|
|
|
default: false
|
|
|
|
|
selector:
|
|
|
|
|
boolean: {}
|
|
|
|
|
|
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
|
|
|
|
|
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-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-25 17:30:49 +00:00
|
|
|
raw_battery: "{{ 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: >-
|
|
|
|
|
{{ start_threshold | float(0) < stop_threshold | float(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
|
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
|
|
|
|
|
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 }}.
|
2026-10-25 15:55:46 +00:00
|
|
|
- condition: template
|
2026-10-25 17:30:49 +00:00
|
|
|
value_template: "{{ switching_enabled | bool(false) }}"
|
|
|
|
|
- condition: template
|
|
|
|
|
value_template: "{{ switch_stable | 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-25 17:30:49 +00:00
|
|
|
value_template: "{{ desired_action | trim == 'on' }}"
|
2026-10-25 15:55:46 +00:00
|
|
|
sequence:
|
|
|
|
|
- action: switch.turn_on
|
|
|
|
|
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-25 17:30:49 +00:00
|
|
|
value_template: "{{ desired_action | trim == 'off' }}"
|
2026-10-25 15:55:46 +00:00
|
|
|
sequence:
|
|
|
|
|
- action: switch.turn_off
|
|
|
|
|
target:
|
|
|
|
|
entity_id: !input power_switch
|