Quick tutorial for saving state?

I’d like to have a motion sensor turn on a light when it’s triggered - that’s easy.

But I’d like to have the light restored to the previous state when the motion sensor later on says “no more motion”.

What’s the best way to approach that? Would previousstate give me the state it was in before we changed it? Or should I save the state to a variable before turning the light “on”? What variable type is a state?

Example code would be much appreciated.

Another way to approach this would be writing a rule, which turns the light on for a predetermined time when motion is detected.
This should also include resetting the timer everytime motion is detected.

But if I switched my light on manually, then my rule fired due to motion, it would turn it off when I really wanted it to leave it in the last state before the rule fired, On.

I had a similar problem, though in my case it had to do with the lights turning on or off based on the weather.

In my rules I keep track of how the light was turned on (manually, timer, or because of the weather). It I turn on the light manually it sets a flag indicating that the light is overridden. This flag will prevent the light from being changed based on the weather.

1 Like