Feature request: Power triggers

Just what it appears from the title, I would love to be able to have actions trigger based on power state/battery level. As I use my macbook without it being plugged in frequently, I don’t want to have non-essential programs running in the background once unplugged. Granted: they’re useful when the computer is plugged in, but not worth the battery drain. This would probably need to have some time element too, just to prevent running if the cord is bumped and plugged back in within 30 seconds.

1 Like

Hey Jesse,

It’s hard to say if Peter will want to monkey with a power management trigger, but I imagine he’ll weigh in on that.

In any case this is dead simple to do yourself.


pmset -g batt

Plugged-In Result:

Now drawing from 'AC Power'
 -InternalBattery-0	98%; charged; 0:00 remaining


Now drawing from 'Battery Power'
 -InternalBattery-0	98%; discharging; (no estimate)

It’s easy to parse that text for AC or Battery Power and then the percentile.

Best Regards,

1 Like

There is actually a build in calculation in KM. Just use if calculation “BATTERY” is true.
I use it to warn me if my Mac is on battery and using the discrete GPU.

Thank you! This answered my question. Using a login trigger and pause until based on the script you provided I was able to get things working pretty well. I've attached a screenshot of the macro as it is now, using BetterTouchTool as an example application. It's hardly an elegant solution, especially using a high number of repeats as a loop substitute, but it's doing the trick.

EDIT: Alas! Using this solution, KM Engine sucks more power than the applications I would be quitting out to conserve it. Back to the drawing board it would appear.

I may be making an obvious error, but this calculation seems to return true whether the mac is plugged in or not, making it difficult to use as would be needed. Did I just make a mistake somewhere, or is this how it should be?

Hi Jesse

I would use a While logged in trigger.
Then you can decide how often it should run.
And I used jpgrosens suggestion with using the BATTERY() function.

In my example I use BBEdit to quit and start when on battery or not.
It check every 30 seconds. This could be every 5 min. to not make KM use to much power.

Here is the macro: http://cl.ly/233U3D451W0U

Here is an image of the macro:

1 Like

This was the knockout punch. Thanks a million!

@JimmyHartington is definitely right, you almost never want to have a permanently running macro.

You also generally do not want to use a Pause Until action that will stay running for long periods of time.

Keyboard Maestro is careful not to use much CPU or battery power when it is not executing macros (or displaying live conditions in the editor) (typically <1%). But as soon as you’ve got macros running, the CPU and battery usage will increase substantially.

Also, if you have long running macros, you have the two opposing problematic results of the macro being canceled for some reason (and thus not running), or the macro being triggered multiple times (and thus multiple instances of it running), neither of which are likely to be good.

Much better if you really need long term checking of something is to use a periodic macro as @JimmyHartington suggested. It also allows you to decide how frequently checking the condition is necessary, and add appropriate hysteresis into it if desired (by tracking the value and requiring multiple sequential periods with the same value to cause a change).

Power triggers are on the todo list, though probably not for 7.0.

Not a Keyboard Maestro solution, but SlimBatteryMonitor
a: does not take up much room on menu bar
b:you can change the icon depending on whether it is on battery or fully charged