# Alarm system. Need somebody to write a rule

**URL:** <https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749>\
**Category:** General\
**Created:** [1 September 2018 17:32 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749 "2018-09-01T17:32:36Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [1 September 2018 17:32 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/1 "2018-09-01T17:32:36Z")

</div>

Hello everyone, I need to create an alarm system. I'm a total incompetent when I need to write codes and rules. I want to make the alarm listen to inputs only if it's triggered with a code. If it's not armed the input messages are ignored.  
I created from a flow found on the internet the number keypad and made a join function to set the length of the code. I need a function or a code to tell my final mqtt output to listen to two different functions, only if the code is correct and the alarm is active.  
Hope I explained myself.  
Please help me 😭

P.s.: sorry for my bad English

---

<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:** [1 September 2018 20:20 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/2 "2018-09-01T20:20:21Z")

</div>

Sorry, it is not really clear what you are trying to do. Can you put debug nodes in to show the data that you have coming in and paste that here, then explain what you want to see on the output and it may make it clearer.

---

<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:** [2 September 2018 10:09 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/3 "2018-09-02T10:09:30Z")

</div>

> [@BlackMamba](#):
>
> I need to create an alarm system. I'm a total incompetent when I need to write codes and rules.

If these 2 statements together are not ringing some extreme alarm bells (pun intended), then you might want to re-assess. Please at least let us know that you wont be relying on this alarm system to keep you or anyone else safe.

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [2 September 2018 12:28 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/4 "2018-09-02T12:28:25Z")

</div>

I drew a scheme to be more clear. My problem is indicated by the red arrow in the upper right corner.  
The meaning of the symbols is indicated by the other red arrow.  
I also need a function that can read the actual state of the alarm (armed or not armed) and allows me to change it by entering the code.  
Hope this is more clear now.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/535cc0b729a1313341ff12abaec85aa38350e383.jpeg)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [2 September 2018 15:46 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/5 "2018-09-02T15:46:52Z")

</div>

I've got a little gate function that I think you could use

It blocks messages from going through it depending on control gate messages

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a29f8c6ac7b9f5dfffc9584e7d93201a6287e157.png)

`[{"id":"5ce2b4e6.98e44c","type":"function","z":"ed0f5786.2ba6d8","name":"Gate","func":"var gateMsg;\n\n/**\n * Describe this function...\n */\nfunction setStatus() {\n if (!(context.get('gate'))) {\n node.status({fill:\"red\", shape:\"dot\", text:'closed'});\n } else {\n node.status({fill:\"green\", shape:\"dot\", text:'open'});\n }\n}\n\n\nif ((context.get('gate')) != false) {\n context.set('gate', true);\n}\nif ((msg['topic']) == 'gate') {\n gateMsg = (msg['payload']);\n if (gateMsg == true) {\n context.set('gate', true);\n } else if (gateMsg == false) {\n context.set('gate', false);\n } else if (gateMsg == 'toggle') {\n context.set('gate', (!(context.get('gate'))));\n }\n setStatus();\n} else {\n if (context.get('gate')) {\n return msg;\n } else {\n setStatus();\n }\n}\n","outputs":1,"noerr":0,"x":630,"y":560,"wires":[["ecc9c75e.785df8"]]},{"id":"84b7c0e6.c4e97","type":"inject","z":"ed0f5786.2ba6d8","name":"","topic":"gate","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":680,"wires":[["5ce2b4e6.98e44c"]]},{"id":"b9b53393.dfaab","type":"inject","z":"ed0f5786.2ba6d8","name":"","topic":"gate","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":720,"wires":[["5ce2b4e6.98e44c"]]},{"id":"565d47c1.94ddd8","type":"inject","z":"ed0f5786.2ba6d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":500,"wires":[["5ce2b4e6.98e44c"]]},{"id":"ecc9c75e.785df8","type":"debug","z":"ed0f5786.2ba6d8","name":"BLOCKLY","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":830,"y":560,"wires":[]},{"id":"79bf3dc7.8274a4","type":"inject","z":"ed0f5786.2ba6d8","name":"","topic":"gate","payload":"toggle","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":640,"wires":[["5ce2b4e6.98e44c"]]},{"id":"97dd4829.84ea78","type":"inject","z":"ed0f5786.2ba6d8","name":"","topic":"gate","payload":"init","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":400,"y":600,"wires":[["5ce2b4e6.98e44c"]]}]`

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [2 September 2018 16:05 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/6 "2018-09-02T16:05:34Z")

</div>

You need some kind of timeout or attempt limit on incorrect code entries or else really long codes to prevent brute force attacks.

Also keypads end up showing wear patterns that reduce the number of digits that need to be tried unless you change the codes frequently to avoid uneven wear patterns on the keys.

I've considered using a Pi touchscreen with a randomized (different every time) numeric button layout, but ended up with a combination of physical keys and key-fob Bluetooth dongles -- approaching the keyed entry point without the dongle triggers an alert. Meets our needs without us needing to remember codes or race timers.

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [2 September 2018 16:23 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/7 "2018-09-02T16:23:22Z")

</div>

It's a fantastic function. Now I need to modify it in order to satisfy my needs. I have a change function that set a msg.payload that sends true or false. How can I modify your gate code?

```
var gateMsg;

/**
 * Describe this function...
 */
function setStatus() {
 if (!(context.get('gate'))) {
 node.status({fill:"red", shape:"dot", text:'closed'});
 } else {
 node.status({fill:"green", shape:"dot", text:'open'});
 }
}

if ((context.get('gate')) != false) {
 context.set('gate', true);
}
if ((msg['topic']) == 'gate') {
 gateMsg = (msg['payload']);
 if (gateMsg == true) {
 context.set('gate', true);
 } else if (gateMsg == false) {
 context.set('gate', false);
 } else if (gateMsg == 'toggle') {
 context.set('gate', (!(context.get('gate'))));
 }
 setStatus();
} else {
 if (context.get('gate')) {
 return msg;
 } else {
 setStatus();
 }
}
```

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [2 September 2018 16:24 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/8 "2018-09-02T16:24:40Z")

</div>

It's more like of a fun thing for now, not a real alarm system that I need to install to protect myself

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [2 September 2018 16:24 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/9 "2018-09-02T16:24:44Z")

</div>

Maybe modify your change node so that it sends true or false using topic gate?

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [2 September 2018 16:26 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/10 "2018-09-02T16:26:21Z")

</div>

It's really interesting. I have also a module MF-RC522 that I can use. Can you help me with that?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [2 September 2018 16:26 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/11 "2018-09-02T16:26:47Z")

</div>

BTW - put ``` before and after your code paste so that the forum formats it

Or paste/select all you code and press

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b7792b70bfcebed401796b806b0b44fd3b265e4d.png)

which does the same thing

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [2 September 2018 16:28 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/12 "2018-09-02T16:28:55Z")

</div>

> Maybe modify your change node so that it sends true or false using topic gate?

Thanks. I'm using change node "set msg.payload to msg.topic", now how can I set the topic?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [2 September 2018 16:45 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/13 "2018-09-02T16:45:06Z")

</div>

No - just use set msg.topic to string gate if the payload is already true or false then your good to go 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/223116c59b2f7cd1116131b0b850a4b5deaa17c2.png)

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [2 September 2018 19:30 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/14 "2018-09-02T19:30:32Z")

</div>

I've no experience with the MF-RC522 module, but a quick bit of Google suggests it has good Arduino support

[rfid](https://playground.arduino.cc/Learning/MFRC522). RFID tags have the advantage of not needing batteries like a Bluetooth beacon does, but I think the range is pretty short which could be a bug or a feature depending on your exact needs.

Here is an "Instruct-able" on using it with Python and a Raspberry Pi:  
[Instruct-able](https://www.instructables.com/id/RFID-RC522-Raspberry-Pi/)

I wanted across the room and through a wall range so the Bluetooth beacons seemed ideal for me. Battery life is a little shorter than the manufacturer's claims (big surprise, not) but it is long enough to not be a problem and "fails safe" meaning I get a notification when I didn't really want one which then informs me the beacon needs a new battery.

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [12 September 2018 21:04 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/15 "2018-09-12T21:04:55Z")

</div>

Thanks all of you for the help, I'm almost done.  
Now I need a node or a function to trigger both activation and inactivation of the alarm with a single button.  
If I press the button the first time, it sends a code that activates a boolean "true". If I press a second time, the same button sends the same code, but this time has to activate a "false" boolean and of course block the "true" message. How can I do?

---

<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:** [13 September 2018 05:29 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/16 "2018-09-13T05:29:51Z")

</div>

You need a node that can toggle a state. Like in the simple example below. As usual, there might be thousand ways achieving this but since I like Function nodes, my example is based on that.

The "Button" node can be anything that sends something to the function node. If you want to control exactly what are valid messages, you have to add some filtering before passing messages to the input. The output is currently just a boolean false/true but as you can see in the code, it can be changed to whatever you need.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/134eb6cf173c3adfec158785a9243ecb69827a61.png)

```auto
[{"id":"dbb9832e.4b5bb","type":"function","z":"bd4da483.182838","name":"","func":"context.set('state',context.get('state')||false);\n\nfunction updateNodeStatus(color, txt) {\n node.status({\n \tfill : color,\n \tshape : \"dot\",\n \ttext : txt\n });\n}\n\nvar state = context.get('state');\nvar color = '';\n\nswitch (state) {\n case 'OFF':\n state = 'ON';\n color = 'green';\n msg.payload = true;\n break;\n case 'ON':\n state = 'OFF';\n color = 'red';\n msg.payload = false;\n break;\n default:\n state = 'OFF';\n color = 'red';\n msg.payload = false;\n break;\n}\n\ncontext.set('state',state);\nupdateNodeStatus(color,state);\nnode.send(msg);","outputs":"1","noerr":0,"x":810,"y":440,"wires":[["11730845.be8368"]]},{"id":"fb923c20.3a09f","type":"inject","z":"bd4da483.182838","name":"Button","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":810,"y":360,"wires":[["dbb9832e.4b5bb"]]},{"id":"11730845.be8368","type":"debug","z":"bd4da483.182838","name":"","active":true,"console":"false","complete":"false","x":830,"y":520,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![BlackMamba](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BlackMamba](https://discourse.nodered.org/u/BlackMamba)\
**Post date:** [13 September 2018 12:59 UTC](https://discourse.nodered.org/t/alarm-system-need-somebody-to-write-a-rule/2749/17 "2018-09-13T12:59:18Z")

</div>

> [@krambriw](#):
>
> [{"id":"dbb9832e.4b5bb","type":"function","z":"bd4da483.182838","name":"","func":"context.set('state',context.get('state')||false);\n\nfunction updateNodeStatus(color, txt) {\n node.status({\n \tfill : color,\n \tshape : "dot",\n \ttext : txt\n });\n}\n\nvar state = context.get('state');\nvar color = '';\n\nswitch (state) {\n case 'OFF':\n state = 'ON';\n color = 'green';\n msg.payload = true;\n break;\n case 'ON':\n state = 'OFF';\n color = 'red';\n msg.payload = false;\n break;\n default:\n state = 'OFF';\n color = 'red';\n msg.payload = false;\n break;\n}\n\ncontext.set('state',state);\nupdateNodeStatus(color,state);\nnode.send(msg);","outputs":"1","noerr":0,"x":810,"y":440,"wires":[["11730845.be8368"]]},{"id":"fb923c20.3a09f","type":"inject","z":"bd4da483.182838","name":"Button","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":810,"y":360,"wires":[["dbb9832e.4b5bb"]]},{"id":"11730845.be8368","type":"debug","z":"bd4da483.182838","name":"","active":true,"console":"false","complete":"false","x":830,"y":520,"wires":}]

Thank you it works perfectly for my needs
