What mean is a device that will let me cache a value and then recall it later in an automation?

As an simple example, I have a room light on a smart switch and in that room I also have a lamp plugged in to a smart outlet. I want to be able to use the smart switch to toggle through both lighs, but only if both lights are off when i press the switch. If one lamp is on already, it should start by turning it off. If neither is on, start by turning on the room light, and on the second press turn on the lamp and on third turn off the room etc.

(I know there are multiple ways to do this, but for sake of example)

My thought is that I could check my cache state and if it is “0” AND all lamps are off then I would turn on the first one in the series and set this cache to “1” with a timer to set it back to “0” in 10 seconds. As long as the value is “1” the switch would cycle through lamp/light combos, but once that cached value is back at 0, the switch defaults back to turn lights.

I know I could probably make something work with a hidden rgb light, but I wondered if there was something more elegant