🔋 Stop Killing Your Laptop Battery: Automate Charging with Home Assistant

⚠️ The Hidden Reason Your Laptop Battery Dies Early

Most people leave their laptop plugged in 24/7.

That’s the fastest way to destroy a lithium-ion battery.

Keeping your battery at 100% constantly:

  • Accelerates chemical wear
  • Reduces total charge cycles
  • Shortens lifespan by years

👉 The ideal range? 20% – 80%

The problem: laptops don’t manage this well.

The solution: automate it yourself.


💡 What This Setup Does (And Why It Works)

This system automatically:

  • Cuts power at 80%
  • Restores power at 30%
  • Requires zero manual input

Result:

  • Less battery stress
  • Longer lifespan
  • Fewer replacements (save £££)

🧰 What You Need (Simple Setup)

1. Smart Plug (Critical)

This is what actually controls charging.

Look for:

  • Fast response time
  • Local control (no cloud lag)
  • Home Assistant compatibility

💡 Popular choices:

  • TP-Link Tapo (great value, reliable)
  • TP-Link Kasa (widely supported)
  • Zigbee plugs (best for local control setups)

👉 Tip: Energy monitoring plugs give you extra insight into charging patterns.


2. Home Assistant

If you’re already running Home Assistant, you’re set.

If not, this is one of the most useful real-world automations you can build with it.


3. A Simple Script (Runs on Your Laptop)

This checks battery level and sends signals.


⚙️ Step 1: Home Assistant Automation

Add these automations:

alias: Laptop Charging Control - OFF
trigger:
  - platform: webhook
    webhook_id: laptop_charge_off
action:
  - service: switch.turn_off
    target:
      entity_id: switch.laptop_plug
mode: singlealias: Laptop Charging Control - ON
trigger:
  - platform: webhook
    webhook_id: laptop_charge_on
action:
  - service: switch.turn_on
    target:
      entity_id: switch.laptop_plug
mode: single

👉 Replace switch.laptop_plug with your actual device.


🌐 Step 2: Webhook Setup (This Is the Key Bit)

Home Assistant automatically creates endpoints:

http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_off
http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_on

Important Tips:

  • Keep these URLs private
  • Use HTTPS if accessing remotely
  • Test with:

curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_off


💻 Step 3: Battery Monitoring Scripts

🪟 Windows (AutoHotkey)

SetTimer, CheckBattery, 60000
return

CheckBattery:
battery := ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Battery")
for b in battery {
    level := b.EstimatedChargeRemaining

    if (level >= 80) {
        Run, curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_off
    }

    if (level <= 30) {
        Run, curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_on
    }
}
return

🐧 Linux (Bash)

#!/bin/bash

BATTERY_LEVEL=$(cat /sys/class/power_supply/BAT0/capacity)

if [ "$BATTERY_LEVEL" -ge 80 ]; then
  curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_off
fi

if [ "$BATTERY_LEVEL" -le 30 ]; then
  curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_on
fi

Run every minute via cron:

* * * * * /path/to/script.sh


🍎 macOS Alternative

#!/bin/bash

BATTERY_LEVEL=$(pmset -g batt | grep -Eo "\d+%" | tr -d '%')

if [ "$BATTERY_LEVEL" -ge 80 ]; then
  curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_off
fi

if [ "$BATTERY_LEVEL" -le 30 ]; then
  curl -X POST http://YOUR_HOME_ASSISTANT:8123/api/webhook/laptop_charge_on
fi

🚀 Conversion Tip: Make It “Set and Forget”

To increase reliability (and avoid annoying toggling):

  • Add a 5% buffer (e.g. 75–80% OFF, 25–30% ON)
  • Run scripts every 60–120 seconds
  • Ensure BIOS allows auto power-on after AC restore

💰 Why This Is Worth Doing

A replacement laptop battery can cost £50–£150+.

A smart plug? ~£10–£25.

👉 This setup pays for itself instantly if it extends your battery life by even a few months.


If you just want something that works:

  • TP-Link Tapo smart plug
  • Home Assistant automation (above)
  • Simple script (Windows/Linux/macOS)

👉 This combo is beginner-friendly and highly reliable.


🏁 Final Thoughts (And Why Most People Don’t Do This)

This is one of those setups that:

  • Takes 15–20 minutes
  • Saves you money long-term
  • Requires zero maintenance after setup

Most people won’t bother.

But once you set it up, your laptop battery will thank you for years.


👉 Next Step

If you’re already using Home Assistant, this is one of the highest ROI automations you can add today.

And if you’re not? This might be the one that convinces you.


About the author

Tim Wilkes is a UK-based security architect with over 15 years of experience in electronics, Linux, and Unix systems administration. Since 2021, he's been designing secure systems for a telecom company while indulging his passions for programming, automation, and 3D printing. Tim shares his projects, tinkering adventures, and tech insights here - partly as a personal log, and partly in the hopes that others will find them useful.

Want to connect or follow along?

LinkedIn: [phpsytems]
Twitter / X: [@timmehwimmy]
Mastodon: [@timmehwimmy@infosec.exchange]


If you've found a post helpful, consider supporting the blog - it's a part-time passion that your support helps keep alive.

⚠️ Disclaimer

This post may contain affiliate links. If you choose to purchase through them, I may earn a small commission at no extra cost to you. I only recommend items and services I’ve personally read or used and found valuable.

As an Amazon Associate I earn from qualifying purchases.