PLEG Basics - An Introduction to the Program Logic Event Generator

A motion-triggered light should be a good first project for learning PLEG but it does seem to generate a lot of questions. Here is another example of how this may be done. The highlights are:

[ul][li]Uses a Self-ReTrigger Schedule (Timer) rather than Now[/li]
[li]Turns on light when motion detected at night[/li]
[li]Ignores motion if recently turned off (< 30 seconds)[/li]
[li]Turns light off automatically if no motion detected for five minutes[/li]
[li]May be locked on by rapid off/on of light switch[/li][/ul]

It requires a light switch with instant (or very rapid) status reporting. The motion sensor should be configured with a short on-time (less than the interval time).

Triggers
LightOn Light is turned on
Motion Motion sensor is tripped
ItsNight DayTime indicates night

Schedules
Timer On: Self-ReTrigger Off: Interval 5:00

Conditions
AutoOn !LightOn and ItsNight and Motion and (!LightOn; Motion > 30)
KeepOn (LightOn and Motion and (LightOn; Motion)) or AutoOn
AutoOff LightOn and !Timer and (LightOn; !Timer) and (!LightOn; LightOn > 10)

Actions
AutoOn Turn Light on
KeepOn PLEG StartTimer timerName=Timer
AutoOff Turn Light off

Thank you for your example!
This is exactly what I have been searching for. I tested it yesterday and it works. Just some tuning of the values to fit my needs.

Just a quick question to try understand a bit more of this logic.

Uses a Self-ReTrigger Schedule (Timer) rather than Now
Is it preferable to use this timer instead of the Now command? I have in earlier posts been advised to limit the amount of Now commands, is this the reason for using the Timer?

regards

Is it preferable to use this timer instead of the Now command?
A Condition that includes the term [b]Now[/b] will get evaluated at least once per minute (3600 times per day). A Condition using a timer will get evaluated when the timer start and stops or another term changes.

The additional processing caused by Now may not have any noticeable impact on PLEG response. It depends how many Conditions are getting evaluated each minute.

Using a timer gives more-predictable timing. Because Now is really a one-minute interval timer, a term like:

 (LightOn; Now > 5:00)

will become true between 5:01 and 6:00 after LightOn becomes true.

@RexBeckett

Can you explain a little on the last part of the AutoOff condition.

AutoOff LightOn and !Timer and (LightOn; !Timer) and (!LightOn; LightOn > 10)

Will the last part ensure that the light will not turn off unless it has been on for more than 10 seconds?

Will the last part ensure that the light will not turn off unless it has been on for more than 10 seconds?
No. It stops it being turned off automatically if it was turned on within ten seconds of being turned off. This is the term that detects the rapid off/on switching to allow the light to be [i]locked[/i] on.

Just a practical question to the light logic above.
If a person is sitting in a room, and the light turns off automatically.
Wouldn’t it be good if he could wave his hands and make the light turn on again?
As I understand it with the “(!LightOn; Motion > 30)” expression you have to wait 30 seconds before it will turn on automatically again.
I guess this isn’t as easy as it sounds :slight_smile:

regards

Well the thing about home automation is that there is never one solution that suits everybody. PLEG makes it easy to implement whatever logic you want…

If you remove the > 30 from the sequence expression, the light will come back on as soon as motion is detected. It will make it difficult to turn the light off if the motion sensor can see the light-switch, though.

Some folk get around this by sensing that the TV is on and using that as a lock-on term. The principle being that lack of motion (and brain activity) is normal while watching TV. :wink:

Of course, if you did the on/off/on sequence on the light-switch, it wouldn’t turn off however long you sat motionless.

hmm
Could you use the timer to detect manual operation?
If something happens before the timer expires cant that indicate manual operation?

If the light is turned off and on before the timer expires the logic could be set to “manual”

Thanks for using your time on this.

regards

If the light is turned off and on before the timer expires the logic could be set to "manual"
It already does this. That's what the [b](!LightOn; LightOn > 10)[/b] term is for.

I understand. I am beginning to think that this can not be done the way I was hoping for.
Here is my scenario:

I am using this in my bathroom. If the timer expires while someone is in the bathroom it is acceptable to wave their hands.
But it is not very good WAF to have to wait 30 seconds berfore one can wave to the sensor. So lets say I remove this part of the logic. Then when the person waves right after the room goes dark, the light would be locked on (if the person does not sit in the dark for 10 seconds first).

As we all know bad WAF kills all HA projects :slight_smile:
So it might be that the manual override is to hard to implement with the required WAF.
That is when I thought one could use the timer to detect that if something happens before it expires a manual operation has happened. But this might be the same as you have done.

Thanks for your effort!

regrads

You might want to incorporate more state into your logic.
In particular instead of going OFF - ON 5 minutes - OFF
Maybe you need OFF - ON - Blink - OFF
You can still extend the ON time if you have motion during the blink interval.
This way people do not get into the 30 second wait cycle unexpectedly.
Split your ON time into a 4 1/2 Min On + Blink + 30 seconds. If you have a dimmer you can momentarily dim the lights.
You might also customize the on times based on time of day … are their certain times of day/week where long showers/bathes are common ?

@haavard, I haven’t tested this but see if it does what you want:

Conditions
AutoOn !LightOn and ItsNight and Motion and (!LightOn; Motion)
KeepOn (LightOn and Motion and (LightOn; Motion)) or AutoOn
AutoOff LightOn and !Timer and (LightOn; !Timer) and ((!LightOn; LightOn > 10) or (AutoOn; LightOn < 5))

@RexBeckett
Thanks for your update!
I have implemented the logic and done some light testing (late last night) and so far I looks good.
What is the philosophy with this logic?
I know a little about logic, but I find it difficult to understand the AutoOff condition… :slight_smile:

regards

What is the philosophy with this logic? I know a little about logic, but I find it difficult to understand the AutoOff condition..
Only half of it is logic. The real power is in the sequence expressions. [imho]This is one of PLEG's greatest features.[/imho]

AutoOff LightOn and !Timer and (LightOn; !Timer) and ((!LightOn; LightOn > 10) or (AutoOn; LightOn < 5))

AutoOff will become true if:

The Light is on AND Timer has ended AND Timer ended after the Light was turned on AND EITHER the Light was last turned on more than ten seconds after it was turned off (i.e. not by rapid off/on) OR the Light was last turned on less than five seconds after AutoOn became true (i.e. it was turned on by motion).

Does that help?

@RexBeckett
Thank you for your explanation. It helps a lot.
I tested a bit more yesterday. And when the light turned off while I took a shower, I waved at the sensor and the light turned on again, only to turn off 10-20 seconds later. I dont see how this can happen? My Pleg is now setup as follows:

Triggers
Name Description
Motion Sensor Bad is tripped
LightOn Spotter Bad 3 etg is turned on

Schedules
Name Type Time Days Random On Delay Off After Type Off Time Off Days Random Off Delay
NightLevel Weekly 23:30:00 1,2,3,4,5,6,7 None Weekly 05:45:00 1,2,3,4,5,6,7 None
Timer Self ReTrigger None Interval 20:00 None

Conditions
Name Expression
AutoOn !LightOn and Motion and (!LightOn; Motion)
KeepON (LightOn and Motion and (LightOn; Motion)) or AutoOn
AutoOff LightOn and !Timer and (LightOn; !Timer) and ((!LightOn; LightOn > 10) or (AutoOn; LightOn < 5))

Actions

Actions for Condition: AutoOn
Immediate
Device Action Arguments
Spotter Bad 3 etg SetLoadLevelTarget newLoadlevelTarget=100

Actions for Condition: KeepON
Immediate
Device Action Arguments
Smartlys Bad StartTimer timerName=Timer intervalTime=

Actions for Condition: AutoOff
Immediate
Device Action Arguments
Spotter Bad 3 etg SetLoadLevelTarget newLoadlevelTarget=0

regards

Do you have a Motion on TIME of about 20 seconds ?

I think you should be starting the timer in the AutoOn Action.

Currently you require a Second motion to start the timer.

RTS is correct. My mods have messed-up the timer start logic. Just add an Action to AutoOn to start Timer. I should know better than to suggest untested changes…

I have added the action as you suggested. I will see how it works later today. I have had some issues with getting a license, but hopefully I can test it anyway.

I assume I should not remove the start Timer action from the KeepOn condition.

regards

Correct … the start timer should be in both actions.
In the keep on action … it extends the timer.

[quote=“Bulldoglowell, post:60, topic:178580”]Create a schedule for your actions, call it say “IsMorning”

Triggers:

LightOn (You need this because you don’t want to trigger an event if the light(s) are on already)
IsMorning (Your schedule that goes MTWTF and from say 05:00 to 07:30)
IsMotion (IsMotion may be one of several motion events like MotionKitchen, MotionStairs or any or all of them)[/quote]

Thanks… just getting to try this today, and still confused.

  1. when creating the schedule “IsMorning”, I do this within PLEG, correct?
  2. Then for the triggers… in PLEG, I have to select a device before I can create a trigger. So, for LightOn, what device would I select? the light I want in the scene? If it’s more than one light, do I create a trigger for each light?
  3. Similar… what device do I select when creating a trigger for IsMorning?

sorry… i’m almost there… appreciate help!