Sequencing Issues

I am using Lunar to control and synchronize the brightness of my monitors (green app by the way despite the slightly dysfunctional UI) and am running into a few problems sequencing problems.

As a start all questions correspond to the following macros:

  1. The below macros are tools that I have built to confirm, log and understand Lunar's behaviour (remember that slight dysfunctional UI). They are very useful particularly the brightness monitoring tool.

__Display Macros_Lunar Monitoring.kmmacros (190.6 KB)

  1. The below macros are macros that I built to reset the Lunar Global variable that I built in 1 and 2 above, more on that below.

__Display Macros_Reset Variables.kmmacros (7.4 KB)

As to the issues:

Issue 1.
Lunar is used to make sure that the dual monitors have teh same brightness throughout the day / night and adjust in sync. At home I use the light sensor in my Studio Display and Lunar in Sync mode, while at the office I use Lunar in Location mode (as I don't have a light sensor).

The macro depends on detecting the Studio Display's USB connection and the office's docking station's USB connection at work.

The macro work in terms of the connection but not the detachment noting detachment general occur on computer shutdown. While not an expert, I assume that the Keyboard Maestro Engine is shut down before the USB connection detach which is why it fails.

The workarounds that I have developed are: a) the second set of macros which reset the Lunar related global variables and then restart / shut down the computer as appropriate or b) creating macros that on long and/or engine startup reset the Lunar related global variables (which means the normal restart and shit down can be used).

Question 1: is there a preferred method between the two possible methods that I have thought of and, of yes, which one and why?

Question 2: Is there a better way of doing this that I have not thought of (i.e., getting the USB detach condition to trigger, structuring the macros, etc.) so that on restart or shutdown the global variables are reset?

Issue 2
The odd time I get a message from Lunar that second instance has been triggered noting a) Lunar is loaded / started and login and ii) Lunar is only used in the macros through its binary which presumably speak to the running instance (i.e., does not start a new instance).

Question 3: What should I test to run the it to ground?

Thanks.

I as thinking about this earlier this morning and believe the following may be easier explanations of the two issues:

Issue 1

  1. Is my understanding correct that the USB Device attach / detach trigger conditions detects changes (as opposed what is attached / detached)?

  2. Is my understanding correct that USB devices attach at boot up which i) is before the Keyboard Maestro Engine launches and ii) before login?

  3. Is my understanding correct that USB devices detach on restart / shutdown after the Keyboard Maestro engine has shutdown?

Assuming all of the above are correct does this explain:

  1. If I cold boot my laptop → connect it to my monitor → login then KM does not message the USB monitor connection as the monitor was connected before KME launch / login (i.e., no change detected)?

  2. If I restart my laptop with the monitor already connected → login then KM does not message the USB monitor connection as the monitor was connected before KME launch / login (i.e., no change detected)?

  3. If I shut down or restart my computer then KM does not message the USB monitor detachment because the KME shuts down before the USB device is detached?

I want the global variable for the monitor's connection status to be 0 / False at startup so that monitor the Lunar monitoring macro which starts / triggers on KME start or login sends the correct messaging and sets the correct Lunar mode.

II can think of the below two ways of doing so (as I do not think the USB Device detach trigger will work for the reasons noted above).

  1. Build a macro that sets them to zero and then restarts or shuts down the computer; or

  2. Build a macro that sets them to zero and KME startup or login?

The questions:

  1. Is there a better approach / method of doing so that I have not thought of?

  2. Which approach / method is best and why?

I am leaning towards building a macro that sets the global variables to zero and then restarts or shuts down the computer because it avoids the possible race condition with the Lunar monitoring macro two whose triggers are KME startup and login. Though admittedly, I could Option 2 with a Semaphore Lock.

Thanks.

Issue 2
The odd time I get a message from Lunar that second instance has been triggered noting a) Lunar is loaded / started and login and ii) Lunar is only used in the macros through its binary which presumably speak to the running instance (i.e., does not start a new instance).

How / what should I test to run the it to ground as I cannot seem to determine the conditions that cause this error?

From the Wiki page:

The USB Device trigger executes a macro when a USB Device is attached or detached.
...
The %TriggerValue% token will contain the name of the device when your macro is triggered.

Your other points sound right.

Then set it to 0/False at startup! I can't see what your problem with doing that is, especially since:

As you're running this at login, why not just start the macro with "if the trigger was login then set variable to 0"?

@Nige_S

As always, greatly appreciated.

I have been working at this all day and think I have found an elegant solutions as demonstrated in the macros:

__Display Macros.kmmacros (180.9 KB)

The keys to the solution are:

  1. I created an initialization macro that i) initialize the variables and ii) calls the monitoring macros. This macro is triggered on Keyboard Maestro Engine startup.

  2. I the deleted the Keyboard Maestro Engine startup and Login triggers from teh monitoring macros.

This is simple and works i) without a Semaphore Lock ii) without a special restart / shutdown macro and iii) seems very reliable.

Any thoughts on Issue 2.

Thank you.

Honestly, I've lost track! But if what you're doing works, it works.

One thing I did spot:

SCREEN(Second, Left) + (SCREEN(Second, Width) - 555) / 2

Isn't that the same as (more self-explanatory)

SCREEN(Second, MidX) - (555 / 2)

...? And similarly with MidY for setting the Y position.

And rather than work them out twice, one for the "Set Next Engine Window" and again immediately after to set the variable, I'd set the variable then use that in the "Engine Window" Action.

Excellent advice, both suggestions incorporated!

1 Like

It detects specific devices being attached or detached.

Likely, yes. Guaranteed, who knows. The system does not specify when devices will be attached on boot.

Likely, but Unknown. The system does not specify when (or if) devices will be detached on restart/shutdown.

The order of things happening on boot, login, logout, shutdown or restart is not well defined (or if it is, I certainly would not want to depend on it).

Thank you, I greatly appreciate the clarification.

It reinforces the approach I have taken!