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. 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. default: 40 selector: number: min: 10 max: 90 step: 5 unit_of_measurement: "%" mode: slider charge_stop: name: Stop charging at (%) description: > Turn charging OFF when battery reaches or exceeds this percentage. default: 80 selector: number: min: 20 max: 100 step: 5 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. default: 5 selector: number: min: 0 max: 120 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. 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. default: false selector: boolean: {} mode: restart 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 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 %} 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) }} {% else %} 0 {% endif %} - condition: template value_template: > {{ elapsed_switch_seconds | float(0) >= min_interval | float(0) * 60 }} - choose: - alias: "Battery low - enable charging" conditions: - condition: template value_template: > {{ battery | float(0) <= start_threshold | float(0) }} - condition: template value_template: > {{ current_switch_state == 'off' }} sequence: - action: switch.turn_on target: entity_id: !input power_switch - alias: "Battery high - disable charging" conditions: - condition: template value_template: > {{ battery | float(0) >= stop_threshold | float(0) }} - condition: template value_template: > {{ current_switch_state == 'on' }} sequence: - action: switch.turn_off target: entity_id: !input power_switch