Control the PIR Sensor with Switch

Hi, I wanna control the motion sensor with a switch (As shown in the below picture).error
But, what I need to in the function node any suggestion. While I found some function node with global data description.

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.

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.

1 Like

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

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

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

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

Yes, I only want to pass the payload that the motion sensor sends if a switch in the dashboard is turned on.

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'

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

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

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

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

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

3 Likes

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

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

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

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

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)

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

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

1 Like

Thanks, It is working.

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