Typically, the way to save state (e.g., which door opened when a person left) is via a text helper. You can use a service (um, I guess I should say “action” now) to change its value. And then you can read its state later.
Yes, an automation is the way to, well, automate things. In your use case, it will probably take at least two automations. One to record which door a person used when they left, and then another one to open that door when they arrive back home.
However, it’s a bit more complex than it might sound on the surface, since the automation really needs to use two “events” that happen at different times - i.e., a door opening, and a person leaving. If you try to trigger the automation when the door opens, the person hasn’t left yet. If you wait until the person leaves (i.e., their person or device_tracker entity changes to away, aka not_home), the door may have already closed. Or consider if two people leave at about the same time, where multiple doors are opening/closing and multiple person/device_tracker entities are changing state.
Let’s assume only one person leaves at a time, and the door they leave through is closed by the time another person leaves. The way I would approach it is to trigger the first automation when the person/device_tracker changes to away/not_home. (Actually, I would trigger on the state changing from home to something else. We’ll also ignore the possible unknown or unavailable states, although a robust automation would take those states into account as well.) The action would be to save the door that changed state last to a text helper for that person. The other automation would trigger when the person/device_tracker changes to home, and the action would be to open the door as indicated by that person’s text helper.
That’s the basic idea. Again, getting an automation to do what you want under perfect conditions (e.g., only one person leaves at a time, none of the entities have unknown or unavailable states, HA doesn’t restart while an automatin is running, etc., etc.) is often fairly straightforward. Making them robust can sometimes be the difficult part. But, then again, that’s the way it always is in software development.
If you’d like a concrete example of how to build such an automation, just let me know. Also, it would be helpful to know some of the details, like exactly which entities represent the doors and people.