# How to detect a 0-1-0 and then a 1-0-1 sequence

**URL:** <https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534>\
**Category:** General\
**Created:** [9 August 2021 14:18 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534 "2021-08-09T14:18:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charry2014](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charry2014/32/46308_2.png) [@Charry2014](https://discourse.nodered.org/u/Charry2014)\
**Post date:** [9 August 2021 14:18 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534/1 "2021-08-09T14:18:11Z")

</div>

Hello everyone, I have a garage door sensor that when the door opens is emits a 1-0-1 sequence of events in about 2 seconds, then when it closes the inverse 0-1-0. A steady 0 means the door is closed, a steady 1 means the door is open. The image below shows first an opening sequence, then a couple minutes later a closing sequence.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0da11659451762589c9300d7ee438b038c5c62f6.png)

Does anyone have any suggestions how I can generate 'door opening' and a 'door closing' events from this state? Thanks in advance.

---

<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:** [9 August 2021 14:34 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534/2 "2021-08-09T14:34:26Z")

</div>

This example may help you The inject and split simulate your payloads.

```auto
[{"id":"bbdadc0c.47864","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[0,1,0]","payloadType":"json","x":110,"y":3160,"wires":[["ff364a64.b29f7"]]},{"id":"ff364a64.b29f7","type":"split","z":"c74669a0.6a34f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":250,"y":3180,"wires":[["db51b787.e5576","7bee5c17.c6ee14"]]},{"id":"7d9d5e3.5637e2","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,0,1]","payloadType":"json","x":120,"y":3220,"wires":[["ff364a64.b29f7"]]},{"id":"db51b787.e5576","type":"trigger","z":"c74669a0.6a34f8","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"150","extend":true,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":430,"y":3160,"wires":[["6ab22d61.4ff8b4"]]},{"id":"7bee5c17.c6ee14","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":400,"y":3100,"wires":[]},{"id":"6ab22d61.4ff8b4","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload = 1 ? \"open\" : \"closed\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":3140,"wires":[["ca83da9f.87c02"]]},{"id":"ca83da9f.87c02","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":3120,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 August 2021 14:39 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534/3 "2021-08-09T14:39:37Z")

</div>

I think the trigger needs to be 2.5 seconds, or thereabouts doesn't it?

---

<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:** [9 August 2021 14:41 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534/4 "2021-08-09T14:41:05Z")

</div>

yes you are correct i did not see the times on the image . maybe a second would do as the payloads seem a second apart.  
[edit] 2 seconds should be fine

```auto
[{"id":"bbdadc0c.47864","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pi/18","payload":"[0,1,0]","payloadType":"json","x":140,"y":3160,"wires":[["ff364a64.b29f7"]]},{"id":"ff364a64.b29f7","type":"split","z":"c74669a0.6a34f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":250,"y":3180,"wires":[["dace1442.41481"]]},{"id":"7d9d5e3.5637e2","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pi/18","payload":"[1,0,1]","payloadType":"json","x":150,"y":3220,"wires":[["ff364a64.b29f7"]]},{"id":"dace1442.41481","type":"delay","z":"c74669a0.6a34f8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":380,"y":3260,"wires":[["db51b787.e5576","7bee5c17.c6ee14"]]},{"id":"db51b787.e5576","type":"trigger","z":"c74669a0.6a34f8","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"2","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"topic","topic":"topic","outputs":1,"x":450,"y":3180,"wires":[["6ab22d61.4ff8b4"]]},{"id":"7bee5c17.c6ee14","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":400,"y":3100,"wires":[]},{"id":"6ab22d61.4ff8b4","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload = 1 ? \"open\" : \"closed\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":3140,"wires":[["ca83da9f.87c02"]]},{"id":"ca83da9f.87c02","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":3120,"wires":[]}]

```

[edit] added per topic in trigger

---

<div class="post-metadata">

**Author:** ![Charry2014](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charry2014/32/46308_2.png) [@Charry2014](https://discourse.nodered.org/u/Charry2014)\
**Post date:** [9 August 2021 17:12 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534/5 "2021-08-09T17:12:34Z")

</div>

That's really cool - many thanks. It works perfectly after I fiddled with the times a bit and landed at about a second in the trigger. I see you cut the logic to just looking at the last digit, rather than the whole sequence, which in this case should be safe. Good solution, many thanks 👍

---

<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:** [23 August 2021 17:13 UTC](https://discourse.nodered.org/t/how-to-detect-a-0-1-0-and-then-a-1-0-1-sequence/49534/6 "2021-08-23T17:13:03Z")

</div>

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