Alarm system. Need somebody to write a rule

#1

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 :sob:

P.s.: sorry for my bad English

0 Likes

#2

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.

0 Likes

#3

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.

1 Like

#4

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.

0 Likes

#5

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

[{"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"]]}]

0 Likes

#6

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.

0 Likes

#7

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();
 }
}
0 Likes

#8

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

0 Likes

#9

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

0 Likes

#10

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

0 Likes

#11

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

Or paste/select all you code and press

image

which does the same thing

0 Likes

#12

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?

0 Likes

#13

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

0 Likes

#14

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

rfid. 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

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.

0 Likes

#15

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?

0 Likes

#16

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

[{"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":[]}]
0 Likes

#17

Thank you it works perfectly for my needs

0 Likes