Carbon-aware laptop charging

Andy Stanford-Clark - Aug'22

My shiny new MacBookPro M1 has a stonking battery life (around 20 hours when it's not doing much), and with my "New Normal" pattern of working almost entirely at home now, I was worried that if I leave it on charge for 2 years straight, like I did my previous one, the battery capacity when I do venture out might be impaired. At the end of its life, my 6-year-old MBP lasted about one RedJet journey before it died.

With recollections of "memory" in NiCad batteries of old, I thought maybe cycling the battery up and down from full to near empty might be a good idea. And then quickly thought that automating that process would be an even better idea.

So armed with my trusty tools of Node-RED and MQTT, I created a flow which would turn the charger off when the battery was full and on when the battery was empty.
You can find out the current state of charge of the battery on a Mac with:
pmset -g batt
which tells you something like:

 -InternalBattery-0 (id=xxx)	100%; charged; 0:00 remaining present: true
from which you are but a short regex away from extracting the battery percentage and charging state (note the <tab> character before the percentage digits!)
To turn the charger on and off I used a Tasmota SmartPlug which can be controlled using MQTT (of course!).

Conveniently, I have lots of these for my Kettle Companion project, and if you want one you can buy them here: Power Metering Smart Plug.

That worked fine, but people who know more about batteries than I do started telling me that cycling the batteries is the worst thing you can possibly do to a modern LiPo-family battery, and I should stop doing that immediately.

Then a conversation with @brackendawson led me down the more interesting rabbit hole of "could I make the charging more carbon-sensitive", in the sense that when the electricity grid is on minimal renewables and maximal high-carbon gas turbines (e.g. when it's early evening so the PV is dropping off, there's a high pressure system sitting over us so the wind isn't blowing, and grid demand is at its highest as we're all cooking dinner), could I "do my bit" to help and turn off the charger?

I had previously played with the UK National Grid Green Light Signal API which tells you the carbon-intensity of electricity generation in your part of the country.

I built a Green Light Signal GlowOrb using this API which sits on my desk and shows me from shades of green via amber to red, how badly the electricity supply is doing from a carbon perspective. on Twitter.

"Where can I get a GlowOrb?", I hear you cry. paypal.me/andysc/25 will do the trick :)

As the feed for that data is on a handy MQTT topic (on my home broker), it was quite straightforward to modify my Node-RED flow so the charger turns off when the Green Light Signal says "very high", and back on when it either falls to a lower level, OR if the battery gets close to empty (I chose < 10%).

It hasn't been up and running long, so I don't know if it might need a bit of tweaking, but it seems to work OK when I feed in test data.

Note it could leave the battery in a low state of charge when you need it full to go out somewhere. A manual over-ride button (an inject node or a dashboard button) would be the obvious way to fix that — or just plug the charger directly into a power socket and don't use the SmartPlug!


Comments, advice or questions? Tweet @andysc.