KHarms
Oct 21, 2009, 02:44 PM
We get a lot of questions about how to handle motion sensors and there aren’t many good answers. Why? Because what you really want is a room occupancy sensor. Something that can tell if you are *in* a room and not *moving within* a room. But since we don’t have such a sensor we did put together a sample that may show a few techniques.
Attached at the bottom of this post is an HCE (HCA export) file that you can import into your own design. To import, select from the menu Home – Import Design Elements. This imports the file and puts all the objects in the impott into an “Imports” folder.
Let me try and explain a bit about how this example works.
There are five objects: The lights that we want to control called “Mirror Lights”, the motion sensor with the not very imaginative name “Motion Sensor”, and three programs.
To start with the light, we used a UPB switch but you can do the same with an Insteon switch. The key is that it transmits when locally controlled by someone tapping the switch paddle. Also it needs to be able to respond to status requests.
The motion sensor is a simple X10 motion sensor set to transmit K2. Again, you could have used a UPB or Insteon motion sensor. All it needs do is to make a transmission when motion is seen. We only handle the ON and not the OFF. You will see why in a minute.
As to the programs, let’s start by seeing what we want to accomplish.
First, we want the light to come on when someone walks into the room. That’s obvious.
Second, we want the light to go off 2 minutes later unless the motion sensor sees motion in which case we want to reset the 2 minutes timer.
Third, we don’t want to turn the light off if whoever came in the room adjusted the light level from what the program set it to. That is, you walk into the room, the light comes on, you leave, and a few minutes later the light goes off. But if you enter the room, the light comes on, and then you adjust the light level, it is now outside control of the program. You will turn it off when you want.
Fourth, If you manually turn off the light on the way out of the room you don’t want that last bit of motion to turn the light back on.
The first and second requirements can easily be done all in hardware. Most motion sensors can be setup to handle this. But the third and fourth requirements need some intelligence.
The first program we will examine is the “master bath1” program.
81
'This program is triggered by the motion sensor. Let’s ignore the first test for now. The second test element tests if the light is on. The motion sensor is going to trigger each time it sees motion in the room. So this program will be triggered again and again. If the light is on then two actions happen. The first is to kill the program “master bath2” program if it is already running and the 2nd operation is to start the “master bath2” program. What’s all that about? The “Master Bath2” program is very simple.
82
All this program does is to delay for two minutes and when that delay expires it turns the light off. But before it does, it tests to see if the light is at the level that the “master bath1” program set it to. If it isn’t at that level then it does nothing. This handles the third requirement listed above – not to turn the light off if it was manually adjusted. Note that the test element will poll the device to get its exact light level as part of the test.
Why two programs? In HCA each program runs it own thread. This means that “master bath2” will be running independently of “master bath1”. So “Master bath1” can be re-triggered and not affect the countdown that “master bath 2” is doing. This splitting operations into more than one program is a good technique. There are other ways to accomplish this but this is very clean.
Let’s go back to “master bath1” program. If the light wasn’t on it turns it on and then starts the “master bath2” program to do the countdown. Note one other thing in this program. The test element that asks if the light is on or off doesn’t poll the device but rather uses the HCA state to know if the light is on or not. Why? It just isn’t necessary to poll the device since turning the light on and off is accomplished by HCA and so it knows the state of the device.
The last requirement – not to retrigger as you are leaving the room after turning off the list – happens in the third program “status bath switch” and the first test element in the “master bath1” program.
83
This program is triggered by a transmission from the switch when the paddle is tapped. UPB devices have a nice setting that says “send a status when locally controlled” but on Insteon switch can also be linked to HCA. When this program does is the one compute element which contains:
statusReceiveTime=_now()
This sets the variable “statusReceiveTime” to the current time. That’s all it does.
Back in “master bath1” the first test element we skipped before contains:
_totalSeconds(_now() - statusReceiveTime) < 2
What this does is to see if the time that the switch was last manually controlled is less than two seconds ago. If it was then the program just exits. What happens here is this: You are leaving the room and you manually turn the light off – you didn’t need to as the motion sensor programs would turn it off, or you manually changed the light level so you would have to turn it off as the motion sensor programs will not turn it off. The transmission from the switch happens and we note the time. Now the motion sensor triggers – it sees you leaving the room – but that happens just after you tapped the switch paddle to off. You don’t want the light to come on, so the program exits. This handled the fourth requirement we set out when we created these programs. There are other ways to do this but this works.
Is this a perfect solution? Not really. There isn’t a perfect solution with motion sensors but it works pretty well.
Attached at the bottom of this post is an HCE (HCA export) file that you can import into your own design. To import, select from the menu Home – Import Design Elements. This imports the file and puts all the objects in the impott into an “Imports” folder.
Let me try and explain a bit about how this example works.
There are five objects: The lights that we want to control called “Mirror Lights”, the motion sensor with the not very imaginative name “Motion Sensor”, and three programs.
To start with the light, we used a UPB switch but you can do the same with an Insteon switch. The key is that it transmits when locally controlled by someone tapping the switch paddle. Also it needs to be able to respond to status requests.
The motion sensor is a simple X10 motion sensor set to transmit K2. Again, you could have used a UPB or Insteon motion sensor. All it needs do is to make a transmission when motion is seen. We only handle the ON and not the OFF. You will see why in a minute.
As to the programs, let’s start by seeing what we want to accomplish.
First, we want the light to come on when someone walks into the room. That’s obvious.
Second, we want the light to go off 2 minutes later unless the motion sensor sees motion in which case we want to reset the 2 minutes timer.
Third, we don’t want to turn the light off if whoever came in the room adjusted the light level from what the program set it to. That is, you walk into the room, the light comes on, you leave, and a few minutes later the light goes off. But if you enter the room, the light comes on, and then you adjust the light level, it is now outside control of the program. You will turn it off when you want.
Fourth, If you manually turn off the light on the way out of the room you don’t want that last bit of motion to turn the light back on.
The first and second requirements can easily be done all in hardware. Most motion sensors can be setup to handle this. But the third and fourth requirements need some intelligence.
The first program we will examine is the “master bath1” program.
81
'This program is triggered by the motion sensor. Let’s ignore the first test for now. The second test element tests if the light is on. The motion sensor is going to trigger each time it sees motion in the room. So this program will be triggered again and again. If the light is on then two actions happen. The first is to kill the program “master bath2” program if it is already running and the 2nd operation is to start the “master bath2” program. What’s all that about? The “Master Bath2” program is very simple.
82
All this program does is to delay for two minutes and when that delay expires it turns the light off. But before it does, it tests to see if the light is at the level that the “master bath1” program set it to. If it isn’t at that level then it does nothing. This handles the third requirement listed above – not to turn the light off if it was manually adjusted. Note that the test element will poll the device to get its exact light level as part of the test.
Why two programs? In HCA each program runs it own thread. This means that “master bath2” will be running independently of “master bath1”. So “Master bath1” can be re-triggered and not affect the countdown that “master bath 2” is doing. This splitting operations into more than one program is a good technique. There are other ways to accomplish this but this is very clean.
Let’s go back to “master bath1” program. If the light wasn’t on it turns it on and then starts the “master bath2” program to do the countdown. Note one other thing in this program. The test element that asks if the light is on or off doesn’t poll the device but rather uses the HCA state to know if the light is on or not. Why? It just isn’t necessary to poll the device since turning the light on and off is accomplished by HCA and so it knows the state of the device.
The last requirement – not to retrigger as you are leaving the room after turning off the list – happens in the third program “status bath switch” and the first test element in the “master bath1” program.
83
This program is triggered by a transmission from the switch when the paddle is tapped. UPB devices have a nice setting that says “send a status when locally controlled” but on Insteon switch can also be linked to HCA. When this program does is the one compute element which contains:
statusReceiveTime=_now()
This sets the variable “statusReceiveTime” to the current time. That’s all it does.
Back in “master bath1” the first test element we skipped before contains:
_totalSeconds(_now() - statusReceiveTime) < 2
What this does is to see if the time that the switch was last manually controlled is less than two seconds ago. If it was then the program just exits. What happens here is this: You are leaving the room and you manually turn the light off – you didn’t need to as the motion sensor programs would turn it off, or you manually changed the light level so you would have to turn it off as the motion sensor programs will not turn it off. The transmission from the switch happens and we note the time. Now the motion sensor triggers – it sees you leaving the room – but that happens just after you tapped the switch paddle to off. You don’t want the light to come on, so the program exits. This handled the fourth requirement we set out when we created these programs. There are other ways to do this but this works.
Is this a perfect solution? Not really. There isn’t a perfect solution with motion sensors but it works pretty well.