# Verified code for on/off switch

**URL:** <https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529>\
**Category:** General\
**Created:** [28 March 2019 07:47 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529 "2019-03-28T07:47:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [28 March 2019 07:47 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/1 "2019-03-28T07:47:32Z")

</div>

Hi,  
i wish when I put the code "1122" switch is **on** if I put "1144" the switch will be in **off** state. But i didn't find the error.

 ![node1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3f762958aa3109f3089d889c47fda9fef9cf9600.png)

```auto
[{"id":"2786ed83.cb4ff2","type":"ui_button","z":"2f0151f5.52bf9e","name":"","group":"4509a4c0.a77afc","order":0,"width":0,"height":0,"label":"Unlock_door","color":"","bgcolor":"","icon":"","payload":"true","payloadType":"bool","topic":"show","x":90,"y":640,"wires":[["66941260.f7d47c"]]},{"id":"8e561e18.3eede","type":"function","z":"2f0151f5.52bf9e","name":"verify_pin","func":"var pins = [\"1122\",\"1144\"];\nvar verified = false;\n\nfor(var i=0;i<pins.length;i++){\n if(msg.passcode == pins[0]){\n verified = true;\n break;\n }\n else(msg.passcode == pins[1]){\n verified = false;\n break;\n }\n}\n\nmsg.verified = verified;\n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":640,"wires":[["82d1fa56.7fe818"]]},{"id":"82d1fa56.7fe818","type":"switch","z":"2f0151f5.52bf9e","name":"check","property":"verified","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"false","repair":false,"outputs":2,"x":470,"y":640,"wires":[["6b2be10c.16e2"],["6b2be10c.16e2"]]},{"id":"6b2be10c.16e2","type":"function","z":"2f0151f5.52bf9e","name":"pin_ok","func":"var msg2 = {};\nmsg2.topic = \"Pin successfully verified To ON!\";\nmsg2.payload = \"\";\n \nreturn [msg, msg2];","outputs":1,"noerr":0,"x":610,"y":640,"wires":[["37a9f7d8.7d2f98"]]},{"id":"66941260.f7d47c","type":"ui_template","z":"2f0151f5.52bf9e","group":"44c9e6e6.0b4128","name":"Pin_Unlock","order":0,"width":"0","height":"0","format":"<div ng-init=\"init()\" id=\"pin_insert\" class=\"dialog\">\n \n <div class=\"dialog_content\">\n \n <div class=\"dialog_header\">\n <span ng-click=\"closeDialog()\" class=\"close\">&times;</span>\n <h2>Insert PIN</h2>\n </div>\n \n <div class=\"dialog_body\">\n\n <div layout=\"row\" layout-align=\"center\">\n <div class=\"number_placeholder\">\n {{passcode.substring(0, 1)}}\n </div>\n <div class=\"number_placeholder\">\n {{passcode.substring(1, 2)}}\n </div>\n <div class=\"number_placeholder\">\n {{passcode.substring(2, 3)}}\n </div>\n <div class=\"number_placeholder\">\n {{passcode.substring(3, 4)}}\n </div>\n </div>\n \n <div layout=\"column\" layout-align=\"center\" style=\"margin-top: 50px\">\n <div layout=\"row\" layout-align=\"center\">\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(1)\">1</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(2)\">2</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(3)\">3</md-button>\n </div>\n </div>\n <div layout=\"row\" layout-align=\"center\">\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(4)\">4</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(5)\">5</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(6)\">6</md-button>\n </div>\n </div>\n <div layout=\"row\" layout-align=\"center\">\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(7)\">7</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(8)\">8</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(9)\">9</md-button>\n </div>\n </div>\n <div layout=\"row\" layout-align=\"center\">\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"confirm()\">\n <ng-md-icon icon=\"done\" style=\"color:#fff;\"></ng-md-icon>\n </md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"add(0)\">0</md-button>\n </div>\n <div class=\"number_box\">\n <md-button class=\"md-raised\" ng-click=\"delete()\">\n <ng-md-icon icon=\"arrow_back\" style=\"color:#fff;\"></ng-md-icon>\n </md-button>\n </div>\n </div>\n </div> \n \n </div> <!--dialog_body-->\n </div> <!--dialog_content-->\n</div> <!--dialog-->\n\n\n<style>\n\n/* The Dialog (background) */\n.dialog {\n display: none; /* Hidden by default */\n position: fixed; /* Stay in place */\n z-index: 9999; /* Sit on top */\n left: 0;\n top: 0;\n width: 100%; /* Full width */\n height: 100%; /* Full height */\n overflow: auto; /* Enable scroll if needed */\n background-color: rgb(0,0,0); /* Fallback color */\n background-color: rgba(0,0,0,0.4); /* Black w/ opacity */\n -webkit-transform: translateZ(0px);\n -webkit-transform: translate3d(0,0,0);\n -webkit-perspective: 1000;\n}\n\n.dialog_content {\n position: absolute;\n background-color: #fff;\n margin-top: 9%;\n margin-left: 7%;\n padding: 0;\n width: 300px;\n box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);\n -webkit-animation-name: animatetop;\n animation-name: animatetop;\n animation-duration: 0.4s;\n}\n\n/* Media query for smartphones (to Fix?) */\n@media only screen and (min-device-width : 375px) and (max-device-width : 667px) { \n .dialog_content {\n margin-top: 5%;\n margin-left: 5%;\n}\n}\n\n/* Add Animation */\n@-webkit-keyframes animatetop {\n from {top: -300px; opacity: 0} \n to {top: 0; opacity: 1}\n}\n\n@keyframes animatetop {\n from {top: -300px; opacity: 0}\n to {top: 0; opacity: 1}\n}\n\n/* Dialog Header */\n.dialog_header {\n padding: 2px 16px;\n background-color: #03A9F4;\n color: white;\n}\n\n/* Dialog Body */\n.dialog_body {padding: 16px 16px;}\n\n/* The Close Button */\n.close {\n color: #fff;\n float: right;\n font-size: 28px;\n font-weight: bold;\n}\n\n.close:hover,\n.close:focus {\n color: #1565C0;\n text-decoration: none;\n cursor: pointer;\n}\n\n/* __ */\n.number_placeholder{\n width: 50px;\n height: 34px;\n margin: 10px;\n font-size: 20pt;\n text-align: center;\n border-bottom: 1px solid black;\n}\n\n/* Number container */\n.number_box{\n margin: 10px;\n}\n\n/* Buttons style */\n.md-button.md-default-theme.md-raised, .md-button.md-raised{\n min-height: 50px;\n min-width: 50px;\n font-weight: bold;\n margin: 0px 10px 10px 0px;\n box-shadow: 4px 4px 6px 0 #dadada;\n background-color: #29B6F6;\n color: #fff;\n}\n\n.md-button.md-default-theme.md-raised:not([disabled]):hover, .md-button.md-raised:not([disabled]):hover{\n background-color: #03A9F4;\n}\n\n.btn1 {\n color : rgb(49, 46, 46);\n background-color: rgba(255, 222, 121, 0.96);\n border-radius: 10px 0 0 10px;\n font-size: 16px;\n}\n\n.btn1:not([disabled]):hover {\n background-color: rgba(107, 103, 91, 0.96);\n color: white;\n}\n\n.btn1[disabled] {\n color : rgb(187, 187, 187);\n background-color: rgba(230, 230, 229, 0.96);\n}\n\n</style>\n\n<script>\n\n/**\n * pin_dialog.js\n * Node-Red UI template for Node-Red Dashboard. \n * Custom dialog that asks for a PIN to allow actions\n * Enjoy it :). \n * -- Daniel\n *\n *\n * @license The Unlicense, http://unlicense.org/\n * @version 0.1\n * @author Daniel Lando, https://github.com/robertsLando\n * @updated 2017-03-08\n * @link ----\n *\n *\n */\n\nvar dialog;\n\n/* ==== */\n(function(scope) {\n \n scope.passcode = \"\";\n scope.payload = \"\";\n scope.inited = false;\n \n scope.init = function() {\n scope.passcode = \"\";\n //Hide the md-panel\n $('#pin_insert').parent().parent().css(\"display\", \"none\");\n //This trick make it works on smartphones too :)\n dialog = $('#pin_insert').detach();\n dialog.appendTo(document.body);\n }\n \n scope.showDialog = function() {\n dialog.css(\"display\", \"block\");\n }\n \n scope.closeDialog = function(){\n dialog.css(\"display\", \"none\");\n }\n \n scope.add = function(value) {\n if(scope.passcode.length < 4) {\n scope.passcode = scope.passcode + value;\n if(scope.passcode.length == 4) {\n console.log(\"The four digit code was entered\");\n \n }\n }\n }\n \n scope.delete = function() {\n if(scope.passcode.length > 0) {\n scope.passcode = scope.passcode.substring(0, scope.passcode.length - 1);\n }\n }\n \n scope.confirm = function() {\n if(scope.passcode.length == 4) {\n scope.send({passcode: scope.passcode, payload : scope.payload});\n scope.closeDialog();\n scope.passcode = \"\";\n scope.payload = \"\";\n }\n }\n\n scope.$watch('msg', function(data) {\n if(data && data.topic){\n switch(data.topic){\n case \"show\":\n if(scope.inited){\n scope.payload = data.payload;\n scope.showDialog();\n }\n else\n scope.inited = true;\n break;\n case \"close\": \n scope.closeDialog(); \n break;\n }\n }\n });\n})(scope);\n\n</script>\n","storeOutMessages":false,"fwdInMessages":false,"templateScope":"local","x":211,"y":580,"wires":[["8e561e18.3eede"]]},{"id":"37a9f7d8.7d2f98","type":"switch","z":"2f0151f5.52bf9e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":730,"y":640,"wires":[["396c9ceb.4354e4"],["396c9ceb.4354e4"]]},{"id":"396c9ceb.4354e4","type":"ui_switch","z":"2f0151f5.52bf9e","name":"","label":"door PIN","tooltip":"","group":"3549a6e5.8b361a","order":9,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":880,"y":640,"wires":[[]]},{"id":"4509a4c0.a77afc","type":"ui_group","z":"","name":"Secure","tab":"a2361348.4a6dc","disp":true,"width":"6"},{"id":"44c9e6e6.0b4128","type":"ui_group","z":"","name":"pin","tab":"a2361348.4a6dc","disp":false,"width":"5","collapse":false},{"id":"3549a6e5.8b361a","type":"ui_group","z":"","name":"Demo","tab":"23ec798a.3b8f36","disp":true,"width":"8","collapse":false},{"id":"a2361348.4a6dc","type":"ui_tab","z":"","name":"Home","icon":"home","order":12,"disabled":false,"hidden":false},{"id":"23ec798a.3b8f36","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":13,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [28 March 2019 08:36 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/2 "2019-03-28T08:36:50Z")

</div>

Where are all your debug nodes so you can see what actual messages you are passing? This will help you locate the error by seeing where the logic diverts from what you want

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [28 March 2019 09:08 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/3 "2019-03-28T09:08:42Z")

</div>

for two operation when i put the code i get **true** for all nodes

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 March 2019 09:16 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/4 "2019-03-28T09:16:30Z")

</div>

Hi.

I am not getting what you are wanting to do.

Looking at the picture of the flow, there is a button, then the `Pin_unlock` node, a `verify_pin`, `check` and so on.

These go (ultimately) to the `door PIN` node which is a node to show you the state.  
(Got that bit)

But where is the pin entered?

If it is that it sends `1122` or `1144` alternatively with each button press, then I am not getting why you need the pin in the scenario.

Yeah, ok, I should maybe look at the flow, but I am just passing by and read the post.  
Then I saw the flow and what you say.

As I said, I am not getting what it is you want to do because what I am seeing isn't what you are writing/asking.

I don't want to sound dismissive. I am new at this too. Though I guess I have got a bit of practise with using NR.

Hope to hear back.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [28 March 2019 09:21 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/5 "2019-03-28T09:21:32Z")

</div>

so check what messages you are sending (using a debug node) and then  
read the docs section on how to log events within a function.  
[https://nodered.org/docs/writing-functions#logging-events](https://nodered.org/docs/writing-functions#logging-events)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 March 2019 11:47 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/6 "2019-03-28T11:47:17Z")

</div>

@hebergeweb You should go read the info tab of the swtch node to understand what it does.

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [28 March 2019 13:24 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/7 "2019-03-28T13:24:58Z")

</div>

in the template(Pin\_Unlock) there is an interface of the buttons (numbers[1,2,3,4...]) where I put the code "1122" to open the door (_ui\_switch_ changed to **on** ) and the code "1144" to close the door (_ui\_switch_ changed to **off** ).  
to open it works well but to close it I have no answer the switch remains in state on.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 March 2019 15:35 UTC](https://discourse.nodered.org/t/verified-code-for-on-off-switch/9529/8 "2019-03-28T15:35:59Z")

</div>

1. you have a bug in your fundtion node
2. you need to go read up on the switch node Edit: actually strike that, instead tell us what you think the switch node does.
