Richard, I’ve been trying to figure out how to code this with no luck. But I’m confident that you’ll figure out a solution.
I have a room with a motion sensor in it. I want the AC to turn on when someone is in the room, and turn off when they are out. (that’s simple). BUT, sometimes I will just walk into this room to grab something and walk immediately out. I don’t want the AC to turn on in that case. Upon sensing the first motion in the room, I’d like to wait 60 seconds and then see if there is still motion in the room between 1-2 minutes after the initial motion. If so, turn on the AC. If not, do nothing (I’ve already left the room).
The trouble I’m having is that the motion sensor will trigger multiple times as people are walking around - so I quickly lose the timestamp of the first trigger. I’ve tried using the count syntax (@2 >1:00 <2:00) but that doesn’t work when I get multiple motion triggers during the first 60 seconds.
Any suggestions on how to accomplish this in PLEG?
Do not try to do these complex things in one statement!
You are interested in two motions, so we have a condition that detects each.
FirstMotion (TriggerMotion; Motion1 > 10:00) and ACOff
TriggerMotion FirstMotion; Motion1 > 1:00 < 5:00
ACOn ACOff and TriggerMotion
FirstMotion is only triggered if the AC is OFF, and the Motion happens at least 10 minutes after the previous Tigger.
TriggerMotion is only triggered if there is Motion in the 1 to 5 minutes after FirstMotion.
This also insures a 10 minute delay between cycling the AC, But smart thermostats have that delay built in already.
I’m so glad this tripped you up too! I was beginning to think it was just me.
FirstMotion gets reset every time Motion1 fires. Let’s say TriggerMotion is at 6:00:00.
10:00:00 Motion1 fires > 10:00 after TriggerMotion and AC is Off. FirstMotion timestamped at 10:00:00.
10:00:45 Motion1 fires again. TriggerMotion is still 6:00:00 and AC is still Off, so FirstMotion fires again and gets timestamped 10:00:45
10:01:30 Motion1 fires again. TriggerMotion is still 6:00:00 and AC is still Off, so FirstMotion fires again and gets timestamped 10:01:30
(repeats forever)
What I think we need is this:
FirstMotion (FirstMotion; Motion1 > 5:00) and ACOff
TriggerMotion FirstMotion; Motion1 > 1:00 < 5:00
This way, at 10:00:00 FirstMotion gets timestamped at 10:00:00, and at 10:00:45 it checks the timestamp and doesn’t update because it has been less than 5 minutes since the last timestamp.
BUT, PLEG doesn’t like recursion. When I tried this, after restart I would get a Running startup LUA message for the plug-in, and then after about a minute or two Vera would restart. It was locking up the PLEG so I couldn’t edit it to remove the offending code. Had to resort to a backup to clean it up.
If PLEG can’t support recursion, I’m thinking we just need to get a copy of FirstMotion:
FirstMotionCopy =FirstMotion
FirstMotion FirstMotionCopy; Motion1 > 5:00) and ACOff
But I don’t know how to copy an entire Condition in PLEG.
@ChrisAZ
I think you ran out of memory … and that is the reason for the restarts.
I do not have any recursion … I did expect to get an error once because of a forward reference. But should not be there after a subsequent restart.
It’s also possibly a problem in overall plugin design if the cause of the crash is that incoming events are happening really fast (lots of motion triggers) … maybe we are over-running the CPU and it gets behind.
So the crash problem is different from the logic problem.
I did find a bug (caused by an earlier fix) in my execution logic … that causes a condition to be re-triggered even when it was previously true. This is causing the logic using some sequence expressions to fail, where they assumed a re-trigger would not get a new timestamp. I know how to fix … but it’s not a quick fix, so I will need time to make the change and test it. I took out my previous fix and was able to get this example to work … but other expressions will have a problem!
Maybe you can try to see what is happening on your Vera … why it’s rebooting … Like I said it could be memory (too many plugins and stuff) or CPU overrun because PLEG is responding to events to closely spaced together and can’t keep up … I sure wish Vera would provided more feedback about why it is restarting.
I have tested the following with a newer version (Should be available soon) the following works:
FirstMotion (Motion1 @ 2 > 10:00) and ACOff
TriggerMotion FirstMotion; Motion1 > 1:00 < 5:00
I consolidated several PLEGs together (something I had been meaning to do “some day”) and ended up deleting three. That seems to have fixed the memory issue. I’m still confused how I run out of memory when Vera says I have 70+ MB of memory free…but that’s another issue.
Now that it isn’t restarting on me, I went back and tried
FirstMotion (FirstMotion; Motion1 > 5:00) and ACOff
TriggerMotion FirstMotion; Motion1 > 1:00 < 5:00
And it worked. I’ll test it some more this week, but it appears to be working as expected.