# Directional door sensor

**URL:** <https://discourse.nodered.org/t/directional-door-sensor/24551>\
**Category:** General\
**Created:** [9 April 2020 21:12 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551 "2020-04-09T21:12:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [9 April 2020 21:12 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/1 "2020-04-09T21:12:32Z")

</div>

Hey guys  
I have been wanting to do this for a while, finally can now.  
I saw some posts a long time ago about making directional sensors on the main door.  
My two sensors will be a aqura zigbee door sensor on the door and an aqara zigbee motion sensor located on the outside of the door above.  
Here is generally what i was thinking.  
What i would like is when i am leaving my house to turn the light off. This would trigger when the door sensor sends an mqtt message first then as i step out and it triggers the motion sensor. both send to some sort of logic node then turn off the lights

When i am arriving at the house. the motion sensor would trigger first then the door sensor would trigger next and turn the lights on.

All of this is easy for me except the main logic determining the direction.

Does anybody have suggestions for doing this logic?

My initial thoughts were to do a buffer array of 2 objects, 1 sensor would send 1 message then the next sensor would send 2nd message. The buffer then flushes and sends it to a function node and i do logic on that array/buffer.  
Other option would be a series of open and close gates that the messages make their way through and it would end in 2 options in or out and i would turn on the lights that way  
However, both of these seem very convoluded and i may be overthinking it.

Ideas? Thx

---

<div class="post-metadata">

**Author:** ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)\
**Post date:** [9 April 2020 21:34 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/2 "2020-04-09T21:34:39Z")

</div>

...nice use-case.

My initial thought goes into the direction of using a dsm-node ([https://github.com/cflurin/node-red-contrib-dsm](https://github.com/cflurin/node-red-contrib-dsm)) with some thing like in the traffic-light example from the node's wiki.  
Like using two final states "inside" and "outside", together with two state transitions "on-my-way-out" and "on-my-way-in").

However, regardless of how to define it in node-red, I think the challenge is within the real world examples.

Especially with using the motion detector as a trigger (what about stray cats or with the mail or pizza delivery man)?

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [9 April 2020 21:46 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/3 "2020-04-09T21:46:50Z")

</div>

hmm that could potentially work. I will have to mess around with that and get back with you!

My main problem which I forgot to mention was actually waiting for the events to come in. Since they are not exactly at the same time there is a race condition/wait condition. I can't use delays as what if i am holding the door opening talking to someone. I find it hard to program it with a function node because how long do i wait for the additional variable? 3 seconds? 5 seconds? 10?  
i would like a node that has two inputs; waits for both of those to trigger then send it. then i do processing based on which of those are at the top of an array or buffer.

you are certainly right about use delivery trucks etc. I think i will have to add more logic to it. I do have a home awareness state for each person in the house. It tracks your phone and graphs over time when you're home, when you leave, when you come back. I will have to use this also.  
There are a ton of variables in there as well.  
If someone is doing a delivering and i open the door to the logic it seems that I am arriving, outside triggered then door opened. so i will have to use my home awareness of; if outside is triggered , then door open and i am home don't do anything. gah! getting more complicated 😛

edit: i am also open to other suggestions of how to detect direction of door. this is just what i have seen suggested. sadly i can't find the link i read a while ago about it  
i thought also maybe a touch sensor on the door handle, it is a brass handle and electrically conductive. i can fit a small zigbee sensor in there and sends a signal when touched or something? idk

---

<div class="post-metadata">

**Author:** ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)\
**Post date:** [10 April 2020 08:41 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/4 "2020-04-10T08:41:52Z")

</div>

Yes, a complicated scenario...  
IMHO the best thing on how to determine that you really are leaving/inside and not just answering the door or filling the trashcan outside is some "Thing" that you would only carry with you when leaving home, Like your phone/wallet/set of (car-)keys. Detection could be via WLAN presence of the phone or RFID (tag reader at the door, probably).  
As for direction of the door (assuming the door itself can only open to one physical direction, like outwards), using a ultrasonic sensor at low range, that gets triggered when the door is approached from the inside could be an option.  
When direction is from inside to outside, the sensor is triggerd first...when from outside to inside, the sensor is triggered second/after the motion outside.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 April 2020 11:14 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/5 "2020-04-10T11:14:25Z")

</div>

I agree that something like an NFC token on your key ring would be sensible. Even then, you might need 2 sensors, one in and one out.

I saw a much simpler solution a long time ago though. A key board near the door. Each peg on the board senses the weight of a set of keys hanging on it.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 April 2020 11:17 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/6 "2020-04-10T11:17:38Z")

</div>

or .. [https://www.wired.com/2007/04/usb-keyholder-m/](https://www.wired.com/2007/04/usb-keyholder-m/)🙂

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [10 April 2020 16:13 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/8 "2020-04-10T16:13:48Z")

</div>

I do have a home awareness setup, phone based.  
RFID could do as well.  
My current door sensor alerts me via push notification if the door is open for more than 5 minutes. As there was one time I though I latched the door closed went on my way and the wind blew the door open keeping it open for many hours

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [10 April 2020 16:15 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/9 "2020-04-10T16:15:39Z")

</div>

I really like this idea! honestly I may do that. will have to figure out what I need to do logic with maybe a wemos and contact sensors

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 April 2020 18:56 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/10 "2020-04-10T18:56:27Z")

</div>

Yes, I have that setup as well. Also a similar setup on the freezer door. Both use 433MHz mag switches.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 April 2020 15:15 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/11 "2020-04-12T15:15:14Z")

</div>

> [@biz](#):
>
> i am also open to other suggestions of how to detect direction of door

Have you considered adding a second motion sensor on the INSIDE of the door? I haven't worked out the full state diagram, but with four events (door:opens/closes, motion:inside/outside), you can distinguish the principal cases mentioned so far. You might also need a timeout to handle pets, delivery men who just drop off packages, etc.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 April 2020 17:20 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/12 "2020-04-12T17:20:31Z")

</div>

How about photocell beam

> **[Gate Door Opener Retro Reflective Photocell Photoelectric Sensor Beam Reflector...](https://www.ebay.co.uk/itm/Gate-Door-Opener-Retro-Reflective-Photocell-Photoelectric-Sensor-Beam-Reflector/383058384541?_trkparms=aid=555018&algo=PL.SIM&ao=1&asc=225076&meid=09970e48d92b45f2909a90d658124cc5&pid=100005&rk=1&rkt=12&mehot=pf&sd=182210118276&itm=383058384541&pmt=1&noa=0&pg=2047675&algv=SimplAMLv5PairwiseWeb&_trksid=p2047675.c100005.m1851)**
>
> Retro-reflective sensor for gate opener, boom barrier;. ​when installing it, make sure no sunshine interferece;. for the smaller reflective board, the range is about 2.5m;. -- Light resource: Infrared LED.

  
or  

> **[Infrared Sensor Sensor Beam Photocell for Sliding Gate Opener Safety IR Nice ...](https://www.ebay.co.uk/itm/Infrared-Sensor-Sensor-Beam-Photocell-for-Sliding-Gate-Opener-Safety-IR-Nice/303120915505?_trkparms=aid=555018&algo=PL.SIM&ao=1&asc=225076&meid=f1a96a6c32d740e092d9bb877c4bf7a6&pid=100005&rk=3&rkt=11&mehot=co&sd=192911084071&itm=303120915505&pmt=1&noa=0&pg=2047675&algv=SimplAMLv5PairwiseWeb&_trksid=p2047675.c100005.m1851)**
>
> High-power transmit devices, with a sensitization margin of 90%. High-power infrared transceivers, low-power consumption processing technology, lightning protection circuit design. Detector Area: Indoor 30M / 98.4 foot, outdoor 20M / 65.6 foot.

  
one inside one outside, attached to something like a shelly 1.

You could then detect direction as time difference, also has advantage of no couriers, pets, wind or insects causing false alarms

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [12 April 2020 18:12 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/13 "2020-04-12T18:12:01Z")

</div>

One easy solution would be to have two doors, an entrance door and an exit door 😀 We actually have two garden doors but only one main door. I'll have a look tomorrow how to do it with the dsm node.

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [16 April 2020 17:02 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/14 "2020-04-16T17:02:40Z")

</div>

This is a great idea. I will look into this

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [16 April 2020 17:04 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/15 "2020-04-16T17:04:02Z")

</div>

haha I like that idea but unfortunately cant have two doors. While I actually have 3 doors between me and the outside, my door, intermediary door , outside door. ( I live in an apartment). I do own the place but don't think other tenants would like me to put sensors on those doors.

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [16 April 2020 17:09 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/16 "2020-04-16T17:09:25Z")

</div>

What I was thinking actually....  
adding 2 motion sensors outside, 1 door sensor, 2 motion sensors inside.  
While this would get crazy to do flows for is there a node that does AI ?  
I could easily train that node with door opens. When I come back I mark it as I walked in, when I leave I mark it as i left.  
The 2 sensors on each side would be able to provide additional inputs to the ai node , if there is one?

then once trained enough data it would output what it thought happen based on all the sensors?  
does an AI node exist?

Other option I just thought of. I am trying to think what is different from the outside and inside. The only thing differently I do when I leave is i HAVE to turn the lock handle to open the door. When I come in I use the key which unlocks the door but does not move the handle. so I could put a sensor on that maybe. I would need a mercury switch or tilt switch of some sort

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [17 April 2020 09:32 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/17 "2020-04-17T09:32:26Z")

</div>

A first dsm-implementation with a door contact inside and a motion sensor outside.

Note: install the `dsm.node` first.

> **[cflurin/node-red-contrib-dsm](https://github.com/cflurin/node-red-contrib-dsm)**
>
> A dynamic state machine node based on the Finite state machine. - cflurin/node-red-contrib-dsm

```auto
[{"id":"6190347.b7639cc","type":"dsm","z":"ac4aa9f6.c24288","name":"in/out detection","sm_config":"{\n \"stateOutput\": \"payload\",\n \"currentState\": \"idle\",\n \"states\": {\n \"idle\": {\n \"open\": \"opened\",\n \"detect\": \"detected\"\n },\n \"opened\": {\n \"detect\": \"out\",\n \"close\": \"idle\",\n \"no-motion\": \"idle\"\n },\n \"detected\": {\n \"open\": \"in\",\n \"close\": \"idle\",\n \"no-motion\": \"idle\"\n },\n \"out\": {\n \"close\": \"idle\",\n \"no-motion\": \"idle\"\n },\n \"in\": {\n \"close\": \"idle\",\n \"no-motion\": \"idle\"\n }\n },\n \"methods\": {\n \"onBeforeTransition\": [\n \"switch (true) {\",\n \"case (msg.topic === 'contact'):\",\n \"msg.topic = msg.payload ? 'open' : 'close';\",\n \"break;\",\n \"case (msg.topic === 'motion'):\",\n \"msg.topic = msg.payload ? 'detect' : 'no-motion';\",\n \"break;\", \n \"}\"\n],\n \"onAfterTransition\": [\n \"switch(msg.payload) {\",\n \"case 'in':\",\n \"case 'out':\",\n \"output = true;\",\n \"break;\",\n \"default:\",\n \"output = false;\",\n \"}\"\n],\n \"status\": {\n \"fill\": {\n \"get\": \"sm.currentState === 'idle' ? 'grey' : sm.currentState === 'in' ? 'red' : 'green';\"\n },\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"sm.currentState;\"\n }\n }\n }\n}\n","x":430,"y":220,"wires":[["a288e2cd.f4d75"]]},{"id":"12fef344.228bfd","type":"inject","z":"ac4aa9f6.c24288","name":"","topic":"contact","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":140,"wires":[["6190347.b7639cc"]]},{"id":"d6bdc384.ddb62","type":"inject","z":"ac4aa9f6.c24288","name":"","topic":"motion","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["6190347.b7639cc"]]},{"id":"a288e2cd.f4d75","type":"debug","z":"ac4aa9f6.c24288","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":220,"wires":[]},{"id":"fa941603.5d6858","type":"inject","z":"ac4aa9f6.c24288","name":"","topic":"contact","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":180,"wires":[["6190347.b7639cc"]]},{"id":"e2c98e2c.b3939","type":"inject","z":"ac4aa9f6.c24288","name":"","topic":"motion","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":300,"wires":[["6190347.b7639cc"]]}]

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [17 April 2020 10:29 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/18 "2020-04-17T10:29:30Z")

</div>

And Paul, @zenofmud, just published a similar solution, in that case for a driveway but it could eventually be used as a base?

> [@Validating Driveway Sensors](https://discourse.nodered.org/t/validating-driveway-sensors/24989/2):
>
> Here is a basic flow you can try - each inject represents one of the two sensors. Both end up conneting to a join however if the second sensor reading doesn't occur within 5 seconds, the first is dumped. After the join you can use the topic to determine the order of the sensors and then use that for the direction the object is moving. [{"id":"dd6ff42c.2f5f88","type":"inject","z":"d18bea89.51692","name":"IR 1","topic":"","payload":"","payloadType":"date"…

---

<div class="post-metadata">

**Author:** ![sg25](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sg25](https://discourse.nodered.org/u/sg25)\
**Post date:** [17 April 2020 22:19 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/19 "2020-04-17T22:19:47Z")

</div>

Here is a real World example from my door/motion sensor out of my database (german but understandable, I think). The door sensor is mounted on the inside of the door:

**Block 1: Wife is kicking out the cat. With only this sensor I cannot detect if she is in the house. She stayed inside.**  
'66236', '2020-04-16 06:06:02', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66238', '2020-04-16 06:06:08', 'door1', 'Alarm', '1', '(door1) offen'  
'66239', '2020-04-16 06:06:22', 'door1', 'Alarm', '0', '(door1) geschlossen'  
'66242', '2020-04-16 06:06:32', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'

**Block 2: Wife is preparing the dog for walking and leaving the house.**  
'66356', '2020-04-16 06:49:01', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66358', '2020-04-16 06:49:20', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66360', '2020-04-16 06:49:27', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66362', '2020-04-16 06:49:42', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66364', '2020-04-16 06:49:54', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66365', '2020-04-16 06:50:12', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66366', '2020-04-16 06:50:41', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66367', '2020-04-16 06:51:03', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66368', '2020-04-16 06:51:07', 'door1', 'Alarm', '1', '(door1) offen'  
'66369', '2020-04-16 06:51:13', 'door1', 'Alarm', '0', '(door1) geschlossen'

**Block 3: Wife and the dog are coming home.**  
'66396', '2020-04-16 07:39:35', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66397', '2020-04-16 07:39:37', 'door1', 'Alarm', '1', '(door1) offen'  
'66398', '2020-04-16 07:39:47', 'door1', 'Alarm', '0', '(door1) geschlossen'  
'66399', '2020-04-16 07:39:57', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66400', '2020-04-16 07:40:05', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66401', '2020-04-16 07:40:21', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66402', '2020-04-16 07:40:28', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66403', '2020-04-16 07:40:43', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66404', '2020-04-16 07:40:49', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66405', '2020-04-16 07:41:05', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'  
'66406', '2020-04-16 07:41:18', 'motion\_door1', 'Alarm', '1', '(door1) Bewegung ein'  
'66408', '2020-04-16 07:41:34', 'motion\_door1', 'Alarm', '0', '(door1) Bewegung aus'

The first problem is the long time for the motion message. The time between open an close is short. If somebody moves quick it is not possible to detect inside/outside. Whats about several persons? What is when the person comes in to get something and leaves?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 June 2020 22:19 UTC](https://discourse.nodered.org/t/directional-door-sensor/24551/20 "2020-06-16T22:19:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
