# Control the PIR Sensor with Switch

**URL:** <https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147>\
**Category:** General\
**Created:** [19 April 2020 18:48 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147 "2020-04-19T18:48:16Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 April 2020 18:48 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/1 "2020-04-19T18:48:16Z")

</div>

Hi, I wanna control the motion sensor with a switch (As shown in the below picture). ![error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/8359f48e0aed7be6793851e2f1a9614deb1940d1.jpeg)  
But, what I need to in the function node any suggestion. While I found some function node with global data description.

```auto
context.value = context.value || {};

switch (msg.topic) {
    case "sw1":
         msg.payload = Number(msg.payload);
        context.value.Gen01 = msg.payload;
        msg = null;
        break;
    case "sw2":
         msg.payload = Number(msg.payload);
        context.value.Gen02= msg.payload;
        msg = null;
        break;
        
    default:
        msg = null;
    	break;

}
var msg1={};
msg1.payload= 0;
if(context.value.Gen01 !== null && context.value.Gen02 !== null ) {
if(context.value.Gen01 === 1 && context.value.Gen02 === 1 ) {

msg1.topic="swOut";

msg1.payload= 1;
 
context.value=null;

} 

  //msg1.payload= 0;  
}
	return msg1;

```

How can I modify it.

---

<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:** [19 April 2020 19:16 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/2 "2020-04-19T19:16:13Z")

</div>

so if you turn the switch off you want to ignore the PIR input, correct?

If so, connect the `switch` to a 'change' node and move the on/off to a flow variable. In the PIR flow, add a `switch` node to check if the flow variable in on or off.

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 April 2020 19:24 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/3 "2020-04-19T19:24:59Z")

</div>

Could you please elaborate, how to check the flow variable condition.

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 April 2020 19:44 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/4 "2020-04-19T19:44:55Z")

</div>

Actually, The function description working for two switch (like "AND Logic")

 ![error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b428a87f78be1cd3a6b1da55d864966f3aea0102.jpeg)

```auto
[{"id":"9e0441f3.1daf4","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"2c8e10e.ce26af","type":"debug","z":"9e0441f3.1daf4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":680,"y":320,"wires":[]},{"id":"d8d6d755.5eb888","type":"ui_switch","z":"9e0441f3.1daf4","name":"","label":"switch1","tooltip":"","group":"a131b547.834c88","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"sw1","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":270,"y":240,"wires":[["cf3410ab.df29d"]]},{"id":"c0a3c0d2.2072d","type":"ui_switch","z":"9e0441f3.1daf4","name":"","label":"switch2","tooltip":"","group":"a131b547.834c88","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"sw2","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":270,"y":380,"wires":[["cf3410ab.df29d"]]},{"id":"cf3410ab.df29d","type":"function","z":"9e0441f3.1daf4","name":"","func":"context.value = context.value || {};\n\nswitch (msg.topic) {\n case \"sw1\":\n msg.payload = Number(msg.payload);\n context.value.Gen01 = msg.payload;\n msg = null;\n break;\n case \"sw2\":\n msg.payload = Number(msg.payload);\n context.value.Gen02= msg.payload;\n msg = null;\n break;\n \n default:\n msg = null;\n \tbreak;\n\n}\nvar msg1={};\nmsg1.payload= 0;\nif(context.value.Gen01 !== null && context.value.Gen02 !== null ) {\nif(context.value.Gen01 === 1 && context.value.Gen02 === 1 ) {\n\n\nmsg1.topic=\"swOut\";\n\nmsg1.payload= 1;\n \ncontext.value=null;\n\n} \n\n //msg1.payload= 0; \n}\n\treturn msg1;\n\n\n","outputs":1,"noerr":0,"x":470,"y":300,"wires":[["2c8e10e.ce26af"]]},{"id":"a131b547.834c88","type":"ui_group","z":"","name":"Status","tab":"ded835ff.3dd788","order":1,"disp":true,"width":"6","collapse":true},{"id":"ded835ff.3dd788","type":"ui_tab","z":"","name":"Lights","icon":"fa-lightbulb-o","disabled":false,"hidden":false}]

```

but with PIR sensor, payload continously show "0"

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [19 April 2020 20:03 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/5 "2020-04-19T20:03:46Z")

</div>

Do you only want to pass the payload that the motion sensor sends if a switch in the dashboard is turned on or do you want to control the same light with a dashboard switch and a motion sensor?

Johannes

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 April 2020 20:22 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/6 "2020-04-19T20:22:35Z")

</div>

Yes, I only want to pass the payload that the motion sensor sends if a switch in the dashboard is turned 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:** [19 April 2020 21:24 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/7 "2020-04-19T21:24:00Z")

</div>

> [@usmanEmder](#):
>
> Could you please elaborate, how to check the flow variable condition.

In a `switch` node specify flow and then the name of the variable you create.  
If you donn't know about contect variables, you should read the section in the docunemtation about '[using Context](https://nodered.org/docs/user-guide/context)'

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [20 April 2020 13:20 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/8 "2020-04-20T13:20:14Z")

</div>

Do I need to make add switch node. Will it not work with function node?

---

<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:** [20 April 2020 13:25 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/9 "2020-04-20T13:25:37Z")

</div>

Use two separate flows  
Flow 1 - ui-switch to change node (which stores the on/off in flow variable)  
Flow 2 - PIR to switch node (check to see if flow variable is on or off) on output continues the flow while the off goes nowhere

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [20 April 2020 14:46 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/10 "2020-04-20T14:46:10Z")

</div>

What will you suggest me what I need to write in "if statement" of both function nodes.

---

<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:** [20 April 2020 15:10 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/11 "2020-04-20T15:10:17Z")

</div>

The best way to help & to understand & learn is in steps instead of serving you with a ready made solution 😉

What @zenofmud expressed in words, I can now present as a picture. I did not bother to deploy but you should be able to continue now, building the flow in NR

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/759b7d455fb2bfad343dfb51c0603d280121bc03.png)

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 April 2020 09:51 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/12 "2020-04-21T09:51:14Z")

</div>

Thanks dear, I did this but I think something wrong in properties.

```auto
[{"id":"2e45c55a.3f5a9a","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b2e75d23.76c79","type":"switch","z":"2e45c55a.3f5a9a","name":"","property":"switch","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":690,"y":300,"wires":[["31b3a052.11085"]]},{"id":"dea77158.0c85e","type":"change","z":"2e45c55a.3f5a9a","name":"","rules":[{"t":"set","p":"switch","pt":"flow","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":200,"wires":[[]]},{"id":"31b3a052.11085","type":"debug","z":"2e45c55a.3f5a9a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":300,"wires":[]},{"id":"d7a923e9.37ae8","type":"rpi-gpio in","z":"2e45c55a.3f5a9a","name":"PIR","pin":"40","intype":"tri","debounce":"25","read":false,"x":520,"y":300,"wires":[["b2e75d23.76c79"]]},{"id":"87a4d839.1a2448","type":"inject","z":"2e45c55a.3f5a9a","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":160,"wires":[["a342e74e.6eb728"]]},{"id":"5a7cc885.900f88","type":"inject","z":"2e45c55a.3f5a9a","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":220,"wires":[["a342e74e.6eb728"]]},{"id":"a342e74e.6eb728","type":"ui_switch","z":"2e45c55a.3f5a9a","name":"","label":"switch","tooltip":"","group":"58e3d701.906d18","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"switch","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":690,"y":200,"wires":[["dea77158.0c85e"]]},{"id":"58e3d701.906d18","type":"ui_group","z":"","name":"Status","tab":"89444d46.a9eb6","order":1,"disp":true,"width":"6","collapse":true},{"id":"89444d46.a9eb6","type":"ui_tab","z":"","name":"Lights","icon":"fa-lightbulb-o","disabled":false,"hidden":false}]

```

---

<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:** [21 April 2020 10:26 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/13 "2020-04-21T10:26:31Z")

</div>

put a debug on the output of the PIR node - what do you get back and where is that data stored?  
Now fix your switch node. Do you really want to compare `flow.switch` to 'flow.1`?  
 ![Screen Shot 2020-04-21 at 6.24.27 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0261a10b32914ff7bb84a354d710bafd7de4348.png)

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 April 2020 13:21 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/14 "2020-04-21T13:21:19Z")

</div>

PIR node shows payload "1" and "0".

---

<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:** [21 April 2020 14:40 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/15 "2020-04-21T14:40:30Z")

</div>

ok so that 1 and 0 are being passed to the `switch` in msg.payload.

Knowing that, what two things do you need to compare in the `switch` node\> (make sure both variables are the same 'type' i.e. both strings or both numbers)

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 April 2020 16:15 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/16 "2020-04-21T16:15:05Z")

</div>

Thanks, But what do you suggest me, as I mentioned my current flow (above). Still don`t get the required result.

---

<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:** [21 April 2020 17:11 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/17 "2020-04-21T17:11:19Z")

</div>

In the switch node you need to test msg.payload against flow.switch

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 April 2020 17:21 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/18 "2020-04-21T17:21:30Z")

</div>

Thanks, It is working.

---

<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:** [5 May 2020 17:21 UTC](https://discourse.nodered.org/t/control-the-pir-sensor-with-switch/25147/19 "2020-05-05T17:21:35Z")

</div>

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