I had a couple of fails using the obvious strategy -- turn on charging at 50, turn off at 85 -- associated with a sensor becoming unavailable. So I've turned to a "belt and suspenders" approach which (so far) has been fully robust, and not run the tablet down. Here is the code:
Automation to cycle charging:
alias: Tablet Office
description: >-
Controls the charging switch for the tablet based on battery level and system
restarts.
triggers:
- entity_id: sensor.office_tablet_battery_level
below: 50
id: turn_on_low_battery_office
trigger: numeric_state
- event: start
id: turn_on_restart_office
trigger: homeassistant
- entity_id: sensor.office_tablet_battery_level
to:
- unavailable
- unknown
for:
minutes: 5
id: turn_on_error_office
trigger: state
- entity_id: sensor.office_tablet_battery_level
above: 85
id: turn_off_high_battery_office
trigger: numeric_state
- entity_id: sensor.office_tablet_battery_state
to: full
id: turn_off_full_office
trigger: state
actions:
- choose:
- conditions:
- condition: state
entity_id: switch.office_tablet
state: "off"
- condition: or
conditions:
- condition: trigger
id: turn_on_low_battery_office
- condition: trigger
id: turn_on_error_office
- condition: and
conditions:
- condition: trigger
id: turn_on_restart_office
- condition: numeric_state
entity_id: sensor.office_tablet_battery_level
below: 85
sequence:
- action: switch.turn_on
target:
entity_id: switch.office_tablet
- conditions:
- condition: state
entity_id: switch.office_tablet
state: "on"
- condition: or
conditions:
- condition: trigger
id: turn_off_high_battery_office
- condition: trigger
id: turn_off_full_office
sequence:
- action: switch.turn_off
target:
entity_id: switch.office_tablet
mode: single
Obviously your switch and tablet names will be different. Any tablet that has Home Companion on it (e.g. used as an interface to Home Assistant -- for android tablets, you can replace the launcher with home assistant, which cuts out the middleman) will report its battery state to Home Assistant.
As a failsafe, if that becomes unavailable or unknown, turn on charging, or if the battery percentage falls below 50. (You can set this wherever you like -- some commentators recommend cycling between 20 and 80, but we sometimes unplug the tablet to use it elsewhere, so having more than 20 is helpful.) Turning on charging after an HA restart protects against missing the transition, as does turning off when full.
Many IoT devices, especially $5 smart plugs, have wonky wifi. Since a run-down battery means the tablet won't recover without manual intervention, while too much charging has a downside three or four years in the future (when many of these tablets won't be getting security updates anyway), protecting against the battery running out is useful. Moreover, older tablets often have unreliable state of charge stats -- I see a lot of implausible patterns in the history.
Indeed, I would just keep charging the tablet and wreck its battery, except that doing so creates not just a risk of a tablet that can't run on battery, but also of a fire. Cycling the battery also reduces the risk of a swelling battery, which in turn reduces the risk of a dangerous lithium fire.