From 1abb00bc35d344b09e2143c3443d6ed057d1cdf4 Mon Sep 17 00:00:00 2001 From: Dejan Date: Mon, 26 Oct 2026 05:33:20 +0000 Subject: [PATCH] Update automation/Battery_control/smart_battery_charging.yaml --- .../smart_battery_charging.yaml | 241 ++++++++++++++---- 1 file changed, 196 insertions(+), 45 deletions(-) diff --git a/automation/Battery_control/smart_battery_charging.yaml b/automation/Battery_control/smart_battery_charging.yaml index 4b9ba2a..5a20c17 100644 --- a/automation/Battery_control/smart_battery_charging.yaml +++ b/automation/Battery_control/smart_battery_charging.yaml @@ -1,12 +1,10 @@ - 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. + 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. domain: automation input: battery_sensor: @@ -15,7 +13,7 @@ blueprint: entity: domain: sensor power_switch: - name: Charging power switch + name: Charger power switch selector: entity: domain: switch @@ -42,8 +40,9 @@ blueprint: 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. + Applies to ordinary charging changes. Critical battery and unsafe + temperature shutoff bypass this delay. Based on HA state, not physical + relay history. default: 5 selector: number: @@ -53,7 +52,7 @@ blueprint: unit_of_measurement: min mode: box max_sensor_age: - name: Maximum sensor update age (minutes, 0 disables) + name: Maximum battery sensor update age (minutes; 0 disables) default: 180 selector: number: @@ -64,15 +63,74 @@ blueprint: 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 + name: Log commands and faults default: false selector: 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 max_exceeded: silent @@ -80,12 +138,20 @@ max_exceeded: silent variables: battery_entity: !input battery_sensor switch_entity: !input power_switch + temp_entity: !input temperature_sensor 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 + 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: - trigger: state @@ -101,66 +167,151 @@ conditions: [] actions: - variables: - raw_battery: "{{ states(battery_entity) }}" + battery_raw: "{{ 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] %} + {{ 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] %} {{ 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) }} + (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 }} 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 }} + {% set obj = states[switch_entity] %} + {{ obj is not none and + (as_timestamp(now()) - as_timestamp(obj.last_changed, 0)) + >= min_interval | float(0) * 60 }} - 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: >- - {% 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' %} + {% 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' %} off + {% elif battery_raw | float(101) <= start_threshold | float and + switch_state == 'off' and temperature_can_start | bool(false) %} + on {% else %} hold {% endif %} - if: - 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: - 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 }}. + 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 }}. - 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' }}" + value_template: "{{ desired_action | trim == 'off_safety' }}" sequence: - action: switch.turn_off target: 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