How to stop lights from resetting the color/effect when turned off

I have a few rgb lights in my Home Assistant setup (via ESPHome). Everytime I turn them off and back on they reset to white. Is there a way to somehow save the state they are in (color/effect) when they are turned off and reapply that state when they are turned back on?

when you say turn off and on, do you mean removing power to the device?

I mean turn off and on with light.turn_on and light.turn_off. When I set the light to the effect “rainbow” and turn it off and back on with Home Assistant the light resets to white.

In that case, you would need either to have persistent storage on the controller which returns the last state, or you need to save that state in HA and “wrap” light control commands in a way to saves and restores that state