homeassistant-blueprints/automation/Battery_control/smart_battery_charging.yaml

167 lines
5.1 KiB
YAML
Raw Normal View History

blueprint:
name: Smart Battery Charging - Reusable Controller
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.
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:
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: >-
Prevent switching until Home Assistant has observed the current switch
state for this long. Not a persistent hardware switching timestamp.
default: 5
selector:
number:
min: 0
max: 120
step: 1
unit_of_measurement: min
mode: box
max_sensor_age:
name: Maximum 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
description: Keep disabled until the sensor and switch are verified.
default: false
selector:
boolean: {}
enable_logging:
name: Write decisions to Home Assistant logbook
default: false
selector:
boolean: {}
mode: single
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
logging_enabled: !input enable_logging
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:
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
{% else %}
hold
{% 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
value_template: "{{ switching_enabled | bool(false) }}"
- condition: template
value_template: "{{ switch_stable | bool(false) }}"
- choose:
- conditions:
- condition: template
value_template: "{{ desired_action | trim == 'on' }}"
sequence:
- action: switch.turn_on
target:
entity_id: !input power_switch
- conditions:
- condition: template
value_template: "{{ desired_action | trim == 'off' }}"
sequence:
- action: switch.turn_off
target:
entity_id: !input power_switch