Please login or register.

Login with username, password and session length

Author Topic: Motion sensor quandry  (Read 11320 times)

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Motion sensor quandry
« on: May 03, 2011, 12:46:41 PM »

I have a mon-X10 motion sensor that I've interfaced with a PowerFlash (detailed discussion on the hardware are in another thread, this is about software interfacing).  I had a MS16A set up so that when motion was detected, it would chime every 1 minute that motion continued and it would turn the lights on until motion was not detected for 16 minutes.  That was relatively easy to accomplish (once I understood the usage of Smart Macros) primarily because all X10 motion sensors send an ON command every time motion is detected and an OFF when the timer has expired.  As such, I simply used the ON commands with the initial lighting and the chime logic and turned lights off with OFF.

In this case, I have a motion sensor that can work in one of 2 modes (effectively).  1) TEST - the sensor will send an ON command through the PF and then send an OFF ~5 seconds after not seeing motion (usually pretty much 5 seconds cause it doesn't necessarily see everything someone is doing).  2) 1, 5, 20 minute delay - the sensor will send an ON command and then send an OFF command after no motion for the delay period.

SO, I was thinking that in order to use the chime, I would use the TEST mode so that I get multiple ON commands.  I would then use a delay of some kind to keep the lights on until an ON command hadn't been seen in xx minutes.  The problem is, I don't know how to get that set up.  I was thinking that I could have a macro run when motion was detected that would turn on 2 dummy modules (one for dummy chime, one for motion start lighting macro).  I have the chime part down thanks to Puck's code, but I'm not sure how to deal with the lights.

The problem is, I need to start a timer for xx minutes on ON command.  Then, if no motion is detected, turn off the lights.  BUT, if motion is detected during the delay, I need to kill the initial delay and set a new delay.  I think (based on crude testing http://forums.x10.com/index.php?topic=23351.msg133227#msg133227), that if the initial macro is running and a second instance fires, the first will continue running.  When the timer ends for the first instance, it will then turn off the lights.  The second instance will continue counting down and eventually send an OFF command to the lights (that are already off).  I found a set of macros that would sorta work, but not exactly how I want (http://forums.x10.com/index.php?topic=21892.0).  That code will:  turn the lights on for 5 minutes, check if motion was detected during the delay, if so, add another 5 minutes, recheck, if not, turn off...

I currently only see 2 options (without any help that is).

1) Set the motion sensor to a set delay.  Have AHP turn on the lights when ON is sent and off when OFF is received.  Throw out the chime code I have and simply have it chime every xx minutes that the motion sensor is on.  This will result in chimes when noone is outside or at least minutes after they were out there.

2) Set the motion sensor in TEST mode.  Use the code above written by jarleifv that uses a set 5 minute block of on time.  Use the current chime code written by Puck.  This works ok, but results in the lights being on for variable time after motion is sensed.  I guess this isn't a huge problem in the long run, it just seems weird to me (I'm a perfectionist and need things to work exactly how I think they should or I can't let it go...my wife LOVES that  rofl).

Any thoughts would be very much appreciated.

Noam

  • Community Organizer
  • Hero Member
  • ***
  • Helpful Post Rating: 51
  • Posts: 2818
Re: Motion sensor quandry
« Reply #1 on: May 03, 2011, 01:03:36 PM »

I'm not sure I understand 100%, but here's my take on it:

Set the motion sensor to "Test" mode, and you'll need a to use a flag, and few macros:

1) MACRO-1, triggered by the "on" from the PowerFlash
    Condition - if Flag (the one you chose to use) is OFF, then:
      Turn on the light
      Sets the flag (above) to "on"
      Trigger MACRO-2. (you'll need to set up a dummy Appliance Module on the HC/UC for MACRO-2)

2) MACRO-2, triggered by MACRO-1
    Condition - if Flag (above) is ON, then:
        Chime
        Wait 1 minute
        Call MACRO-2

3) MACRO-3, triggered by the "off" from the PowerFlash
    Turn OFF the flag
    Turn off the light

Unless I missed something here, then here's how it should work:

1. Motion is detected, the light comes on, the flag is set, and MACRO-2 is called.
2. The chime goes off, and will go off again every minute, as long as motion is detected.
3. Once motion stops, the sensor sends the OFF via the PowerFlash, which turns off the flag, and turns off the light.

I haven't tried this, but I imagine it should work, as long as the sensor doesn't send an "OFF" while there is still motion happening.
Multiple "ON" commands from the sensor shouldn't have any effect, as they won't do anything.
Logged

Noam

  • Community Organizer
  • Hero Member
  • ***
  • Helpful Post Rating: 51
  • Posts: 2818
Re: Motion sensor quandry
« Reply #2 on: May 03, 2011, 01:13:53 PM »

I just tested this as best I can at my office:

I used a PalmPad to stand-in for the motion sensor and PowerFlash.
I used an appliance module (on/wait 1 second/off) to stand-in for the chime.
I used a shorter delay (10 seconds) between "chimes".

The rest I did the same way I described.

It worked fine, so I don't think you'll have any trouble with this solution.
Logged

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Re: Motion sensor quandry
« Reply #3 on: May 03, 2011, 02:29:55 PM »

That would work fine if the delay between last motion and the off command were set in the motion sensor.  But, in your code, the motion sensor would send an OFF command ~5 seconds after it sent an ON and would reset everything.  I think that would result in the lights flashing and the chime going off once per minute or so (I think the chime would work fine).  The main problem I'm having is dealing with the delay.

Noam

  • Community Organizer
  • Hero Member
  • ***
  • Helpful Post Rating: 51
  • Posts: 2818
Re: Motion sensor quandry
« Reply #4 on: May 03, 2011, 02:44:53 PM »

That would work fine if the delay between last motion and the off command were set in the motion sensor.  But, in your code, the motion sensor would send an OFF command ~5 seconds after it sent an ON and would reset everything.  I think that would result in the lights flashing and the chime going off once per minute or so (I think the chime would work fine).  The main problem I'm having is dealing with the delay.
...TEST - the sensor will send an ON command through the PF and then send an OFF ~5 seconds after not seeing motion...

It sounds like the "Test" mode is the same as the "normal" mode, but with a delay of about 5 seconds, instead of the 1/5/20 minute delays.  The way I understand what you write, there won't be an "OFF" until the motion stops.
As long as there is no "OFF" command until the motion stops, the light will stay on (not flash), and the chime will keep sounding every minute. Repeated "ON" commands shouldn't affect anything, as that first macro ONLY runs if the flag is OFF.
Maybe I misunderstood what you are trying to do with this setup.
Logged

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Re: Motion sensor quandry
« Reply #5 on: May 03, 2011, 05:13:44 PM »

Ahhh, that's the problem.  What I should have done was post a standard list of received commands (a log would be better, but that's not on this computer).  Here's basically what happens:

Code: [Select]
# Time Command
1 5/3/11 14:04:58 A1 ON (Garage Front)
2 5/3/11 14:05:05 A1 OFF (Garage Front)
3 5/3/11 14:05:16 A1 ON (Garage Front)
4 5/3/11 14:05:22 A1 OFF (Garage Front)
5 5/3/11 14:05:35 A1 ON (Garage Front)
6 5/3/11 14:05:43 A1 OFF (Garage Front)
7 5/3/11 14:05:51 A1 ON (Garage Front)
8 5/3/11 14:05:59 A1 OFF (Garage Front)
9 5/3/11 14:06:09 A1 ON (Garage Front)
10 5/3/11 14:06:15 A1 OFF (Garage Front)
11 5/3/11 14:06:28 A1 ON (Garage Front)
12 5/3/11 14:06:37 A1 OFF (Garage Front)
13 5/3/11 14:06:47 A1 ON (Garage Front)
14 5/3/11 14:06:54 A1 OFF (Garage Front)
15 5/3/11 14:07:07 A1 ON (Garage Front)
16 5/3/11 14:07:12 A1 OFF (Garage Front)
17 5/3/11 14:07:22 A1 ON (Garage Front)
18 5/3/11 14:07:28 A1 OFF (Garage Front)
19 5/3/11 14:07:36 A1 ON (Garage Front)
20 5/3/11 14:07:44 A1 OFF (Garage Front)
21 5/3/11 14:07:49 A1 ON (Garage Front)
22 5/3/11 14:07:58 A1 OFF (Garage Front)
23 5/3/11 14:08:10 A1 ON (Garage Front)
24 5/3/11 14:08:19 A1 OFF (Garage Front)
25 5/3/11 14:08:30 A1 ON (Garage Front)
26 5/3/11 14:08:37 A1 OFF (Garage Front)
27 5/3/11 14:08:46 A1 ON (Garage Front)
28 5/3/11 14:08:55 A1 OFF (Garage Front)
29 5/3/11 14:09:03 A1 ON (Garage Front)
30 5/3/11 14:09:09 A1 OFF (Garage Front)

Basically, the PF sends an ON when motion is sensed.  If it continues to see motion (if you move a reasonable amount and have enough skin visible), it keeps the motion sensor active and does not send an OFF.  If you stop moving and only have a small visible amount of skin visible, it will send OFF.  TEST mode doesn't hold the sensor ON very long (it's designed to reset quickly so you can actually see where the range of motion is and aim the sensor appropriately).

Noam

  • Community Organizer
  • Hero Member
  • ***
  • Helpful Post Rating: 51
  • Posts: 2818
Re: Motion sensor quandry
« Reply #6 on: May 03, 2011, 07:58:44 PM »

So use the built-in delay in the sensor to have it shut off after 1/5/20 minutes of no activity.
Same macros as before, but the "off" takes longer to happen.
Logged

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Re: Motion sensor quandry
« Reply #7 on: May 03, 2011, 08:09:29 PM »

Yeah, that's option 1.  I was hoping there might be a way to make option 2 work.  I'll probably try using jarleifv's code first and fall back on option 1 if that doesn't seem to work well.  Thanks for the input!

Anyone else have any thoughts?  I'll keep viewing this for responses indefinitely if anyone else wants to chime in.   >!

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Re: Motion sensor quandry
« Reply #8 on: May 05, 2011, 01:02:18 AM »

You know, I was just thinking that I do have an indication of when motion is initially detected AND when motion stops.  I know that when the motion sensor sends an ON command, it's detected motion.  I know that it will stay ON until motion is not sensed.  I know that when the motion is not sensed for a period of seconds or more, it will send an OFF command.

I was thinking initially that I could use the ON command exclusively with a delay, but today I realized that if I use the OFF command, I might be able to get the operation to work the way I want.  If I set a flag when OFF is received and set a delay before turning turning off the lights, I'm wondering if there's a way to stop that delay if an ON is seen.  I could use a different method that's a bit heavy on the flags and/or dummy modules, but it could work.

Say I want a 3 minute delay.  I have a macro for each minute of the delay.  The first one will set a flag for the first delay of 1 minuted which triggers a second macro with 1 minute delay which triggers a third.  The ON macro will trigger a flag that will be checked by the second and third macros.  If motion is not sensed, the 3 delays will continue to the end and a 4th macro will then make a final check of the flag before turning off the light.  I'm sure this would work, but it kind sucks IMO on the elegance front.

Thoughts?

Noam

  • Community Organizer
  • Hero Member
  • ***
  • Helpful Post Rating: 51
  • Posts: 2818
Re: Motion sensor quandry
« Reply #9 on: May 05, 2011, 06:48:13 AM »

Change MACRO-3 to this:
3) MACRO-3, triggered by the "off" from the PowerFlash
    Turn OFF the flag
    Wait 3 minutes
    Run MACRO-4

Then, add MACRO-4:
4)  MACRO-4, triggered by MACRO-3
    Condition - if Flag (the same one for all the macros) is OFF, then:
      Turn off the light

That way, if there is motion in the three minutes (while the flag is off), MACRO-1 will trigger, and start the process over again.
The only thing is, you might get more than 1 chime per minute if this happens. If you add a second flag (turned on at the beginning of MACRO-2, and turned off at the end of it), and make MACRO-2 conditional based on that flag being off, that should prevent multiple instances of it from running at the same time.
Logged

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Re: Motion sensor quandry
« Reply #10 on: May 18, 2011, 11:13:38 AM »

In an effort to avoid leaving an open thread that others may search and find useless, I'm going to post my final solution.  Hopefully someone finds it useful.

The resolution is...ActiveHome Pro sucks!  It can't do things that seem simple in a way that makes sense.  Some tasks can be kludged around to make a usable workaround, but that doesn't always work.  In this case, the simple task is setting a timer and having the capability to cancel/update it once it's started.  I can think of several ways this could be implemented, but X10 doesn't seem to have an interest in making a fully usable program.  Well, enough dumping on X10's software "solution" and on to my current setup...

I decided that the logic should be as follows:
Code: [Select]
If Motion ON, then motion has been sensed so check to see if it's day or night.  If day, run chime stuff.  If night, run chime stuff and turn on lights.  If it's night and the lights are on, then kill lights off delay stuff.

If Motion OFF, then sensor no longer sees motion.  If it's day, do nothing.  If it's night, run chime stuff and start a delay to turn the lights off.


DummyChime taken care of within CM15A
These 2 functions must be run in different threads to avoid locking up the processor inside the delay loop so as not to execute the MotionON function

Function MotionON
If DAY Then
Turn on DummyChime
Set MotionON flag
ElseIf NIGHT Then
If lights are off, turn them on
Turn on DummyChime
Set MotionON flag
EndIf
End MotionON


Function MotionOFF
If DAY Then
If lights are on, turn them off
clear MotionON flag
ElseIf NIGHT Then
Set delay stop time
Loop
If time = delay stop time Then
Turn off lights
Exit MotionOFF function
ElseIf MotionON flag is set Then
Exit MotionOFF function
EndIf
End Loop
EndIf
End MotionOFF

Since AHP can't implement that, I turned to another option...the AHP SDK.  This allows for watching PLC and RF signals to be sent and received by the connected computer.  The language I chose, AutoHotKey, is not the fastest, but it's easy to implement and modify since it's interpreted (but can be compiled if desired).  Here's my code (currently set in debug mode so it can be run on a non-CM15A computer for testing of logic).

Code: [Select]
; CM15A RecvAction using AHK_L and X10's ActiveHome/SDK.
#Persistent
#SingleInstance force


;oX10 := ComObjCreate("X10.ActiveHome")
;ComObjConnect(oX10, "X10event_")

NIGHT := false
LightON := false
MotionON := false
MotionDelayTime := 5000 ;5*1000*60 ; 5 minutes

return







; Functions

X10event_RecvAction(Action, Parm1, Parm2, Parm3, Parm4, Parm5, Reserved)
{
global NIGHT
global LightON
global MotionON
global MotionDelayTime

    Log("Event: " . Action . ", " . Parm1 . ", " . Parm2 . ", " . Parm3 . ", " . Parm4 . ", " . Parm5)

GoSub, LogVariables

If ((Action = "RecvPlc") AND (Parm1 = "a1") AND (Parm2 = "On"))
    {
        ; Motion ON
Log("`tMotion ON received")

; Motion seen so disable delay timer if active
Log("`tClear Timer - MotionDelay")
SetTimer, MotionDelay, OFF

; Set MotionON flag
global MotionOn := true

; Turn on DummyChime
Log("`tSet DummyChime ON")
X10CM15a_Plc("C1", "On")

If NIGHT = 1
{
Log("`t`tTime is NIGHT")

; If lights are OFF, turn them ON
If NOT LightON
{
Log("`t`t`tTurn Lights ON")
X10CM15a_Plc("A6", "On")
global LightON := true
}
Else
{
Log("`t`t`tLights already ON")
}
}
Else
{
Log("`t`tTime is DAY")

; If lights are ON, turn them OFF
If LightON
{
Log("`t`t`tTurn Lights OFF")
X10CM15a_Plc("A6", "Off")
global LightON := false
}
Else
{
Log("`t`t`tLights already OFF")
}
}
    }
    Else If ((Action = "RecvPlc") AND (Parm1 = "a1") AND (Parm2 = "Off"))
    {
        ; Motion OFF
Log("`tMotion OFF Received")

; Clear MotionON
global MotionOn := false

If NIGHT = 1
{
Log("`t`tTime is NIGHT")

; Set motion timer
Log("`t`tSet Timer - MotionDelay")
SetTimer, MotionDelay, %MotionDelayTime%
}
Else
{
Log("`t`tTime is DAY")

; If lights are ON, turn them OFF
If NOT LightON
{
Log("`t`t`tTurn Lights OFF")
X10CM15a_Plc("A6", "Off")
global LightON := false
}
Else
{
Log("`t`t`tLights already OFF")
}
}
    }
    Else If ((Action = "RecvPlc") AND (Parm1 = "p1") AND (Parm2 = "On"))
    {
        ; NIGHT
Log("`tSunset")

global Night := true
    }
    Else If ((Action = "RecvPlc") AND (Parm1 = "p1") AND (Parm2 = "Off"))
    {
        ; DAY
Log("`tSunrise")

global Night := false

; If lights are ON, turn them OFF
If LightON
{
Log("`t`tTurn Lights OFF")
X10CM15a_Plc("A6", "Off")
global LightON := true
}
Else
{
Log("`t`tLights already OFF")
}
    }
}
return


X10CM15a_Plc(HUcode, State)
{
static sdkfile = "C:\Program Files\Common Files\X10\Common\ahcmd.exe"
;Run, %sdkfile% sendplc %HUcode% %State% , , hide

X10event_RecvAction("RecvPlc", HUcode, State, 0, 0, 0, 0)
}
return


Log(Text)
{
static LogFile = A_ScriptDir "\X10_PLC_Interpreter_Log.txt"

FormatTime, TimeString,, dd/MM/yyyy HH:mm:ss

FileAppend, %TimeString%`t%Text%`n, %LogFile%
}
return


MotionDelayEnded()
{
; AHK doesn't like setting globals from subroutines
global LightON := false
SetTimer, MotionDelay, OFF

Log("MotionDelay reached - Lights OFF")

X10CM15a_Plc("A6", "Off")
}
return





; Subroutines/Timers

MotionDelay:
{
MotionDelayEnded()
}
return


LogVariables:
{
Log("NIGHT=" . NIGHT . "`tLightON=" . LightON . "`tMotionON=" . MotionON)
}
return





; Hot keys

Esc::
{
Log("ExitApp`n`n")
ExitApp
}
return






; Test Hot Keys

F8:: ; MotionON
{
X10event_RecvAction("RecvPlc","a1","On",0,0,0,0)
sleep 500
}
return

F9:: ; MotionOFF
{
X10event_RecvAction("RecvPlc","a1","Off",0,0,0,0)
sleep 500
}
return

F10:: ; DAY
{
X10event_RecvAction("RecvPlc","p1","Off",0,0,0,0)
sleep 500
}
return

F11:: ; NIGHT
{
X10event_RecvAction("RecvPlc","p1","On",0,0,0,0)
sleep 500
}
return

F12:: ; Refresh Variables
{
GoSub, LogVariables
sleep 500
}
return

And a sample log file:
Code: [Select]
18/05/2011 07:52:36 NIGHT=0 LightON=0 MotionON=0
18/05/2011 07:52:42 Event: RecvPlc, a1, On, 0, 0, 0
18/05/2011 07:52:42 NIGHT=0 LightON=0 MotionON=0
18/05/2011 07:52:42 Motion ON received
18/05/2011 07:52:43 Clear Timer - MotionDelay
18/05/2011 07:52:43 Set DummyChime ON
18/05/2011 07:52:43 Event: RecvPlc, C1, On, 0, 0, 0
18/05/2011 07:52:43 NIGHT=0 LightON=0 MotionON=1
18/05/2011 07:52:43 Time is DAY
18/05/2011 07:52:43 Lights already OFF
18/05/2011 07:52:44 Event: RecvPlc, a1, On, 0, 0, 0
18/05/2011 07:52:44 NIGHT=0 LightON=0 MotionON=1
18/05/2011 07:52:44 Motion ON received
18/05/2011 07:52:44 Clear Timer - MotionDelay
18/05/2011 07:52:44 Set DummyChime ON
18/05/2011 07:52:44 Event: RecvPlc, C1, On, 0, 0, 0
18/05/2011 07:52:45 NIGHT=0 LightON=0 MotionON=1
18/05/2011 07:52:45 Time is DAY
18/05/2011 07:52:45 Lights already OFF
18/05/2011 07:52:45 Event: RecvPlc, a1, Off, 0, 0, 0
18/05/2011 07:52:45 NIGHT=0 LightON=0 MotionON=1
18/05/2011 07:52:45 Motion OFF Received
18/05/2011 07:52:45 Time is DAY
18/05/2011 07:52:46 Turn Lights OFF
18/05/2011 07:52:46 Event: RecvPlc, A6, Off, 0, 0, 0
18/05/2011 07:52:46 NIGHT=0 LightON=0 MotionON=0
18/05/2011 07:53:19 ExitApp

The only requirement for this to work is that the motion sensor has to send an 'ON' and 'OFF' command (obviously) and there must be a sunrise/sunset phantom module (P1) set on a timer to toggle in the morning and evening.  I also have a chime (B1), a dummy chime (C1), and macro logic to only trigger a chime every 5 minutes.  If motion is sensed during this delay, it does not make a noise. 

One limitation I'm aware of is that does not save the state of 'NIGHT' in a file and can't calculate it's state.  When this code is started, if it's night then the P1 module must be toggled in AHP to the 'ON' state (or trigger the F11 test hot key I suppose).  I could add sunrise/sunset calculations, but I don't think it's a big enough issue currently to justify the extra coding.

This is certainly more complex than accepting AHP limitations.  But, in it's current implementation, this code can be changed pretty easily to add more logic whereas AHP would probably require a rewrite of the entire SmartMacro logic to go forward.  I'll probably use this type of code going forward as it's far more flexible and much less frustrating.

I'm aware of HomeSeer and the like, but these programs cost $$$ which is not in the budget.  AHK is FREE and has great forum support.  The SDK makes it a very viable option.  Of course, an AHP API would be even better, but since that won't likely ever happen, this works well enough!

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2082
Re: Motion sensor quandry
« Reply #11 on: May 20, 2011, 11:59:36 AM »

I did a bit more testing with the code and have an updated version.  I cleaned a lot of things up for both readability and improved logic.  I also made all the sensor HU code setup at the top so that they can be easily changed.  Additionally, the sending of PLC is now done with the COM object rather than ahcmd.exe which should make things cleaner and better.  If anyone ends up using this, please post (just curious).

>!
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.