Remembering which lights were turned off

Hi
I have this bathroom with a set of 4 light switches and a motion detector.
What I want it to do is to remember which lights were on before it turns off so then if the motion is triggered again it turns on the same set of lights.
The thing is I only want it to remember for a short period of time.
The example scenario is basically if someone goes to the bathroom and turned on just the light of the mirror, and if for any chance stays still for too long and it triggers the motion for absense then if this person moves it turns back on the same light or lights.
How about should be the best way to set this up?

You basically you need to sort this issue:

Because you are asking for this:

This is the same problem
If you solve the first one then your problem disappears
Change your presence delay

Ok let me clarify
I want the lights to turn off if someone is not present for lets say 5 minutes in order to save power…but…I can imagine someone staying very still on the loo for 5 minutes just moving their thumbs, or in the mirror doing some very fine makeup on the eye lashes not moving much, thus triggering the absense.
If in fact someone is indeed in the bathroom, then moving will trigger the lights, but which lights should turn on??
So it should remember which were on in the first place right?
But what if someone comes back to the bathroom after an hour or so more, then there is no point of triggering the previous lights, just a default one (preferably the led one which uses less power)

How shall i set up a rule that memorizes which lights are on before it turns them off and keeps that data stored if the motion detector is triggered within, something like 30 seconds or less?

Does this makes more sense?

You could put all relevant items in in a group. When motion deactivated, iterate over the items and copy their state to the a proxy item of the same type.
When motion is detected with x time, iterate over the proxy group’s setting the items to the same state as the proxy item.

Another alternative is two iterate over the group, get the previous state and set the item to that.

Thanks, but I forgot to mention im a complete newb with scripting specifically in oh.
I understand I need to start somewhere so first let me try to see if I got it right using pseudo code

Rule to turn off lights
if motion sensor state = off

  • iterate and store the state of light 1, 2, 3 and 4
  • start a timer
  • turn off all 4 lights (I guess there is no problem in turning off a light that is already off)

Rule to turn lights on
if motion sensor state = on

  • check timer if its less than 30 seconds
  • if yes
    • then iterate over all 4 lights and restore their stored state
    • stop timer
      else
      turn on default light
      stop timer

is this the correct process?
Ive check the other threads and I havent found yet a good example similar to this (just some bits) but if someone knows a good example of code to start would be appreciated.
Cheers

Nuno

I have a snippet of code where I did something similar a few months back. I’ll dig it out tonight.

You’re on the right track and with Vincent’s hit at the design pattern you should be ok. A couple of comments though -

You don’t need a timer in your logic as you say you do. When triggering the motion, set the state of a proxy item to last-triggered-date-time. When your no-motion rule fires, compare that date time to now and if more than n seconds, return; (remember to use the semicolon)
A timer makes yours confusing as you’re trying to wrap your head around to many things.

Second - the DP says to use the expire binding to trigger the no-motion rule. I would say it depends on what you are using for motion.
I have a number of Xioami motion sensors. The way they work is close a switch when motion is detected and open it n seconds after motion is last detected. i.e. they handle the timeout for you.
Some, I imagine, just flick back and forth as motion is detected. If this is yours then the use of the expire binding is good. If not, it adds complexity.

So, going on the Xiaomi option -

Some very very rough code (I am on a train).

//proxy items must be named the same as the real one but with an _proxy at the end.


rule when item changes to CLOSED //motion detected
then
 foreach item in gMyProxyItems  //loop over all items in the group
	val myItem = myproxyitem.name.replace("_proxy", "") //grab the proxy name and remove the _proxy from the name. Now you have the real name.
	myItem.sendCommand(myproxyitem.state) //send a command to the real item with the state of the proxy. This might work.
	//sendCommand(myItem, myproxyitem.state) //if the above does not work, this should.
 }

 end 
	
rule when item changes to OPEN //motion no detected. This will push the values back into the proxy items.
then
 foreach item in gMyProxyItems  //loop over all items in the group
	val myProxyItem = myproxyitem.name + "_proxy" // build your proxy item for storing. 
	myProxyItem.sendCommand(item.state) //send a command to the real item with the state of the proxy. This might work.
	
 }

 end 

if I can get my personal laptop working tonight (magic smoke escaped) I will post up my versions of the code.

If you do have persistence setup then it’s a lot easier - myLight.previousstate is your friend. No proxy items needed.

Hi Crispin
Thank you for the input, I usually only have spare time to fiddle with OH over the weekend, so no rush, ill check this out next sat or sunday, till then if you find the time to post more of your code that would be nice, thank you.

N.