OK, you have it set up as an OR, not an AND.
It will fire the lights when the time is 5 hours after sundown (at exactly that time) OR when the contact sensor opens.
The “at sunset” option in a routine is used for things like turning lights on at sunset. Or turning the porch light off 5 hours after sunset. Not for setting a period of time within which other things happen.
I can’t use the mobile app now because voiceover navigation broke a couple of updates ago and they haven’t fixed it yet, but I’m sure someone else can help you.
Are you using modes? That would be the usual way of handling this. In a separate routine, change the mode to night, then only have the contact sensor trigger the lights to come on during night mode. But there are many other ways to do it as well.
For example, at our house we have a motion sensor in the bedroom. During Home mode (daytime) it is ignored. In night mode (from sunset to whenever I go to bed) it turns on the overhead ceiling light. Once I go to bed I manually change the mode to Asleep, and in asleep mode the contact sensor does not turn on the overhead light but rather turns on a soft nightlight.
You may also be able to do it with SmartLighting. Again, someone else will have to help you look at that.