Restoring the state of a light but only if it's still on

I have an automation that alerts people in the house by turnings various lights blue. They stay blue for 30 minutes and after that revert to their previous state. I accomplish this using scene.create to capture the before state (before turning them blue) then at the end I use scene.turn_on to activate before scene.

However, in that 30 min timeframe some lights will get manually shut off. Thus, when the scene gets activated at the end they will be turned back on the their previous state. What I would prefer to do is only revert them to their previous state if they’re still on, but if they have been turned off in that timeframe leave them off.

The only thing I can think of so far is to create individual scenes for each entity and have a condition in the final automation to check if they’re on before applying the scene for that entity. It’s simple and will work but clunky and has a lot of overhead.

Are there other methods that could accomplish the same thing that are less clunky?

Entity Controller from HACS has a feature which blocks a light when it has been switched on/off manually, either for a set time or until it is manually switched off/on again. I don’t know how well it is being maintained, though - the developer did a really good job initially, but there haven’t been any updates lately.

Alternatively, there’s a blueprint automation with the snappy name “Turn on light, switch, scene, script or group based on motion and illuminance” which appears to have a similar feature. It’s intended for use with a motion sensor as trigger, but I imagine it would work with other binary sensors.

Thanks for the suggestions. After thinking about it and searching with some different keywords I might use an approach like this.

I can put the lights in a group (called alert_lights or similar), iterate over them and create a “before” scene for each automatically, then when the time period ends iterate over the group again and if they’re currently on restore the before scene otherwise do nothing.