Have the presence of mind to (do something)

To have the calmness and ability to act sensibly in a difficult situation.

My aunt had the presence of mind to write a will before she passed away.