Battery()

I have a few questions about the BATTERY() and when it fires and the belt macro:

Change Power Notification.kmmacros (10.0 KB)

Summary

As to the questions:

  1. I added the SCREENSAVER action because I got an error message that the macro failed because the Display Text window could not be written which at first confused me because the Keyboard Maestro Engien cannot start until log in (at the earliest).

The scenario was i) started the MacBook ii) logged into the MacBook (which started the Keyboard Maestro Engine) and ii) plugged in the external docking stations.

The theory is that the the Keyboard Maestro Engine fired off the Display Text on connecting the docking station (because it power the MacBook too) and that the displays had yet to sync up and become visible.

Is the above theory correct, is the SCREENSAVER the solution, is there a better way of doing this?

[NOTE: I cannot test at present as I am not in the office where I use teh docking station, else I would. I am trying to set the solution in advance.]

  1. When does the BATTERY() function fire:

a) If I start my computer → connect the external monitor or docking station → login, I do not a message that the MacBook has been plugged into a power source.

b) If I start my computer → log in → connect the external monitor or docking station, I do not get a message that the MacBook is on battery power but I do get a message that the MacBook has bene plugged in.

This leads me to believe that the power source is detected / set before login so that when I log in and the Keyboard Maestro Engine starts up, the power source is known (i.e., BATTERY() does not detect a change), and that the macro only fires when it subsequently changes (which explains the observed behaviour).

Is my understanding correct and, if not, what is the correct understanding.

It's difficult to say what the actual situation is at that time.

Possibly check the results of SCREENCOUNT function and/or SCREEN function results?

Do you mean when does the Power Status Changed trigger fire?

The BATTERY function returns its result whenever it is used in a calculation.

If you mean the trigger, that fires (assuming the macro is active!) when the system sends a power status change notification to Keyboard Maestro, and Keyboard Maestro’s determination of whether or not the system is currently plugged in or running on battery descides differently from the last time.

Since Keyboard Maestro Engine is not running until you log in, when you log in, the power status is whatever it is, and has not changed at that point and wont change until something, err, changes.

I don't understand what messages from what.

After you login, assuming Keyboard Maestro Engine has launched, and assuming the system sends a power status change notification, then Keyboard Maestro will run through the system supplied power devices and find one that claims it is battery or power source, and use that (or failing finding anything, give up and say not running on battery.

Maybe if there are two power sources when the dock is attached, they might disagree. I have no idea.

The BATTERY function will return the same value as the trigger sees, so if it changes value, then that is when the trigger will fire (presuming the macro is active and the system sends a notification).

Until the engine starts up, nothing with Keyboard Maestro happens. After it starts up, the power change trigger will definitely not fire until the result of the BATTERY function changes value.

Appreciated but I am unclear as to what you mean by testing noting I am testing the values both before and after the Pause Until. What exactly should I be looking for / testing to determine whether this is the solution (other than no more error messages and the Display Text window)?

Apologies, I should have been clearer in that I was tailing about the trigger itself.

I do undertand that:

  1. Macro fires when Keyboard Maestro detects a power change
  2. Battery function return 0 for A/C power and 1 for battery

Appreciated and this is at the heart of what I wanted to get at in that:

  1. The trigger detects status changes (i.e., as opposed to status);
  2. The power status is "set" (i.e., A/C or battery) at Keyboard Maestro Engine startup; and
  3. As such the macro will not trigger until the first power change afterKeyboard Maestro Engine startup.

Is the above correct?

Apologies again, I should ahem been clear.

Message from the attached macro. This has been addressed in the immediately proceedings section

Please, I would like to clarify What do you mean by the "The BATTERY function will return the same value as the trigger sees, so if it changes value, then that is when the trigger will fire (presuming the macro is active and the system sends a notification)."

This is very interesting. Is my understanding correct that the BATTRY() is constantly polling the system and that when BATTERY() detects a change that it "activates" the Power Change Status" trigger?

Appreaciated, very helpful. Thank you!

No. Functions get the current state/value of whatever when you invoke them. That is different to Triggers, which are fired by Events -- often, but not always, a state change.

You plug in your dock/display -- that is an Event (actually multiple events for power, USB, etc). "Am I currently plugged in to my dock/display?" is a question answered with a Function or Token (or maybe a script).

So BATTERY() is "Am I currently running on battery power? Answer 1 for 'Yes', 0 for 'No'." It doesn't constantly poll (unless you put it in a loop for just that purpose!) and it can't detect a change (unless you compare the current value returned to one previously stored in a variable).

BTW, you don't need the BATTERY() Function at all in your macro. It isn't mentioned on the Wiki page, but the %TriggerValue% of the "Power Status Changed" Trigger is 1 when you unplug and 0 when you plug in -- the same as if you evaluate BATTERY() immediately after the Event.

1 Like

Appreciated, thank you and understood!

1 Like

Correct, although I would change the wording to be “The power status is "remembered" (i.e., A/C or battery) at Keyboard Maestro Engine startup”

Keyboard Maestro remembers the state of the Power/Battery (the same determination as used by the BATTERY function), and on notification of a power status change, if that Power/Battery determination is different from the previous one, then the trigger fires.

Note that if it is possible for the system to change state and fail to send the notification, or send the notification ands then change state later, the resulting behaviour might be undesirable. But as far as I am aware that is not the case.

2 Likes

Thank you very helpful indeed!