Hi Platonicsolid,
You would need to use something global to store stuff from a lower scope.
A helper would work.
This would work:
Trigger based template sensor to store global variables.
Here’s a string you can plug into your search engine to get more examples:
site:community.home-assistant.io automation global variable