diff --git a/automation/Battery_control/smart_battery_charging.yaml b/automation/Battery_control/smart_battery_charging.yaml index 787b1b4..4b9ba2a 100644 --- a/automation/Battery_control/smart_battery_charging.yaml +++ b/automation/Battery_control/smart_battery_charging.yaml @@ -1,81 +1,49 @@ blueprint: name: Smart Battery Charging - Reusable Controller - description: > - Reusable battery charging controller for Android tablets, - phones and other battery-powered devices. - - Select a battery percentage sensor and a power switch. - Charging starts at the lower threshold and stops at - 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. - + 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 - description: > - Sensor reporting battery level between 0 and 100. selector: entity: domain: sensor - power_switch: name: Charging power switch - description: > - Switch controlling power to the charger. - Supports Zigbee2MQTT, ZHA and other switch entities. selector: entity: domain: switch - charge_start: - name: Start charging at (%) - description: > - Turn charging ON when battery is at or below - this percentage. + name: Start charging at or below (%) default: 40 selector: number: min: 10 max: 90 - step: 5 + step: 1 unit_of_measurement: "%" mode: slider - charge_stop: - name: Stop charging at (%) - description: > - Turn charging OFF when battery reaches or exceeds - this percentage. + name: Stop charging at or above (%) default: 80 selector: number: min: 20 max: 100 - step: 5 + step: 1 unit_of_measurement: "%" mode: slider - minimum_switch_interval: - name: Minimum switch-state duration (minutes) - description: > - Minimum time the switch must remain in its - observed state before another command is allowed. + 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: @@ -84,162 +52,114 @@ blueprint: step: 1 unit_of_measurement: min mode: box - max_sensor_age: - name: Maximum sensor age (minutes) - 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. + name: Maximum sensor update age (minutes, 0 disables) default: 180 selector: number: min: 0 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 unit_of_measurement: min mode: box - enable_switching: name: Enable real switching - description: > - OFF = Monitoring-only mode. - ON = Automation may control the power switch. - - Keep OFF during initial testing. + 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: restart +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 - interval_minutes: !input evaluation_interval switching_enabled: !input enable_switching + logging_enabled: !input enable_logging triggers: - trigger: state entity_id: !input battery_sensor - id: battery_changed - - trigger: state entity_id: !input power_switch - id: switch_changed - - trigger: time_pattern minutes: "/1" - id: periodic_check - - trigger: homeassistant event: start - id: ha_started -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 %} +conditions: [] actions: - variables: - battery: "{{ states(battery_entity) | float(0) }}" - current_switch_state: "{{ states(switch_entity) }}" - - elapsed_switch_seconds: > - {% set sw = states.get(switch_entity) %} - {% if sw is not none %} - {{ as_timestamp(now()) - - as_timestamp(sw.last_changed, 0) }} + 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 %} - 0 + 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: > - {{ elapsed_switch_seconds | float(0) - >= min_interval | float(0) * 60 }} - + value_template: "{{ switching_enabled | bool(false) }}" + - condition: template + value_template: "{{ switch_stable | bool(false) }}" - choose: - - alias: "Battery low - enable charging" - conditions: + - conditions: - condition: template - value_template: > - {{ battery | float(0) - <= start_threshold | float(0) }} - - - condition: template - value_template: > - {{ current_switch_state == 'off' }} - + value_template: "{{ desired_action | trim == 'on' }}" sequence: - action: switch.turn_on target: entity_id: !input power_switch - - - alias: "Battery high - disable charging" - conditions: + - conditions: - condition: template - value_template: > - {{ battery | float(0) - >= stop_threshold | float(0) }} - - - condition: template - value_template: > - {{ current_switch_state == 'on' }} - + value_template: "{{ desired_action | trim == 'off' }}" sequence: - action: switch.turn_off target: