You could enable logging for some core stuff. E.g., in configuration.yaml you could add:
logger:
default: info
logs:
homeassistant.components.automation: debug
homeassistant.core: debug
homeassitatnt.helpers.event: debug
homeassitatnt.helpers.script: debug
This will add a bunch of DEBUG messages to the log that might help shed some light. But honestly, to really “see” what’s happening, you might need to instrument the code some more.
FWIW, here is where the time trigger is implemented:
Basically, in your case, if the processing of sensor.sun_next_midnight changing state happens before the trigger processing, the previously set trigger “arming” will be removed and a new one (for the next midnight) will take its place, causing the previous one to be “missed.”
Based on the times you posted earlier from the database, there’s only about 8 milliseconds or so between when the trigger should fire and when the state of the sensor changes. I’m not saying it’s definitely happening, but I think it’s possible, especially if you’re running HA on a less powerful system, and you have it doing a lot, that the trigger callback is being delayed by other stuff, including the processing of the sensor’s state change, which will cause the trigger callback to be removed before it gets a chance to actually run.
Again, I doubt what you’re experiencing can be classified as a bug, although, of course, it might be. I really think that depending on a time trigger to fire at the time a sensor’s value indicates, when the sensor also changes at that same time, is not a robust design.