# Two motion sensors for one lamp

**URL:** <https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980>\
**Category:** General\
**Created:** [8 April 2022 11:58 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980 "2022-04-08T11:58:33Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [8 April 2022 11:58 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/1 "2022-04-08T11:58:33Z")

</div>

There are two (or more?) motion sensors and one lamp in one room. How to make the lamp turn off only if both sensors do not detect movement? But it may be that only one sensor will see the movement. So from the second sensor the signal "no movement" will not come.

---

<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:** [8 April 2022 12:10 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/2 "2022-04-08T12:10:07Z")

</div>

There are many ways you could do this and here is one.

Save the state of each sensor and then test them all. If the sensor sees motion, it sets it's value to 1 otherwise it sets it to 0 so if you have three sensors and sensor 2 notices movement you would have:  
sensor1 = 0  
sensor2 = 1  
sensor3 = 0  
if you add the values of the three up and it is greater than 0 turn on the light, if it equals 0 shut off the light. In this case the total is 1 so the light turns on.

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [8 April 2022 12:16 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/3 "2022-04-08T12:16:32Z")

</div>

> [@zenofmud](#):
>
> Save the state of each sensor

You mean context flow?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 April 2022 12:21 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/4 "2022-04-08T12:21:27Z")

</div>

or just feed the movement detected from all into a trigger node set to whatever timeout you like - and extended by any input signal.

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [8 April 2022 13:07 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/5 "2022-04-08T13:07:17Z")

</div>

Thank you. Not sure if I understood you correctly. I don't think this will work as it should. Could you make an example?

---

<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:** [8 April 2022 16:24 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/6 "2022-04-08T16:24:52Z")

</div>

> [@Leopoll](#):
>
> I don't think this will work as it should

Why don't you try to do it and see what you end up with?

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [8 April 2022 17:56 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/7 "2022-04-08T17:56:25Z")

</div>

> [@zenofmud](#):
>
> Why don't you try to do it and see what you end up with?

If this is about your idea, context flow, I'm sure it will work. I don't understand how to implement the idea from [dceejay](https://discourse.nodered.org/u/dceejay)

---

<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:** [8 April 2022 18:49 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/8 "2022-04-08T18:49:47Z")

</div>

Again try it. Play with the trigger node and see how it works

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [8 April 2022 19:27 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/9 "2022-04-08T19:27:53Z")

</div>

So far I have been able to make such an example. It works. But I think it's too complicated.

```auto
[{"id":"2da6a6319c628b8d","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"fe3b155cbf8835cf","type":"inject","z":"2da6a6319c628b8d","name":"Sensor1 On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":100,"wires":[["aae7ec2de8a80577"]]},{"id":"78a66c2fac9e59ef","type":"switch","z":"2da6a6319c628b8d","name":"","property":"$flowContext('Sensor1')+ $flowContext('Sensor2')\t\t","propertyType":"jsonata","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":180,"wires":[["463d56234ac9cedc"],["423ddf8967082c0c"]]},{"id":"aae7ec2de8a80577","type":"change","z":"2da6a6319c628b8d","name":"","rules":[{"t":"set","p":"Sensor1","pt":"flow","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":100,"wires":[["78a66c2fac9e59ef"]]},{"id":"a4e2dc2053d33a51","type":"inject","z":"2da6a6319c628b8d","name":"Sensor1 Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":160,"wires":[["5a5cb5c05853e998"]]},{"id":"08fe5d589339de1f","type":"inject","z":"2da6a6319c628b8d","name":"Sensor2 On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":220,"wires":[["f078c65f0cd30c5c"]]},{"id":"a20b8577fc59325e","type":"inject","z":"2da6a6319c628b8d","name":"Sensor2 Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":280,"wires":[["3d223032dc5ddc44"]]},{"id":"5a5cb5c05853e998","type":"change","z":"2da6a6319c628b8d","name":"","rules":[{"t":"set","p":"Sensor1","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":160,"wires":[["78a66c2fac9e59ef"]]},{"id":"f078c65f0cd30c5c","type":"change","z":"2da6a6319c628b8d","name":"","rules":[{"t":"set","p":"Sensor2","pt":"flow","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":220,"wires":[["78a66c2fac9e59ef"]]},{"id":"3d223032dc5ddc44","type":"change","z":"2da6a6319c628b8d","name":"","rules":[{"t":"set","p":"Sensor2","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":280,"wires":[["78a66c2fac9e59ef"]]},{"id":"463d56234ac9cedc","type":"debug","z":"2da6a6319c628b8d","name":"ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":140,"wires":[]},{"id":"423ddf8967082c0c","type":"debug","z":"2da6a6319c628b8d","name":"OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":220,"wires":[]}]

```

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

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [8 April 2022 22:37 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/10 "2022-04-08T22:37:08Z")

</div>

Simplified a little

```auto
[{"id":"2da6a6319c628b8d","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"fe3b155cbf8835cf","type":"inject","z":"2da6a6319c628b8d","name":"Sensor1 On","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"filename","v":"Sensor1","vt":"flow"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":100,"wires":[["aae7ec2de8a80577"]]},{"id":"78a66c2fac9e59ef","type":"switch","z":"2da6a6319c628b8d","name":"","property":"$flowContext('Sensor1')+ $flowContext('Sensor2')\t\t","propertyType":"jsonata","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":180,"wires":[["463d56234ac9cedc"],["423ddf8967082c0c"]]},{"id":"aae7ec2de8a80577","type":"change","z":"2da6a6319c628b8d","name":"","rules":[{"t":"set","p":"Sensor1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":140,"wires":[["78a66c2fac9e59ef"]]},{"id":"a4e2dc2053d33a51","type":"inject","z":"2da6a6319c628b8d","name":"Sensor1 Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":130,"y":160,"wires":[["aae7ec2de8a80577"]]},{"id":"08fe5d589339de1f","type":"inject","z":"2da6a6319c628b8d","name":"Sensor2 On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":220,"wires":[["d4d58edff41df973"]]},{"id":"a20b8577fc59325e","type":"inject","z":"2da6a6319c628b8d","name":"Sensor2 Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":130,"y":280,"wires":[["d4d58edff41df973"]]},{"id":"463d56234ac9cedc","type":"debug","z":"2da6a6319c628b8d","name":"ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":140,"wires":[]},{"id":"423ddf8967082c0c","type":"debug","z":"2da6a6319c628b8d","name":"OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":220,"wires":[]},{"id":"d4d58edff41df973","type":"change","z":"2da6a6319c628b8d","name":"","rules":[{"t":"set","p":"Sensor2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":240,"wires":[["78a66c2fac9e59ef"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 April 2022 23:37 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/11 "2022-04-08T23:37:31Z")

</div>

Trying to undersstand your problem with @dceejay's suggetion...  
Are your motion sensors PIR modules?

Some of these have two modes: "normal" and "retriggering".

It's probably best for Node-red use to have normal mode, where continuous movement results in repeated messages.

If your sensors are like this, you can feed all the messages into a trigger node set to send ON and send OFF after say 5 minutes _unless another message arrives_.

But if they are set to retriggering, you might not get another messsage despite the movement continuing.

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [9 April 2022 17:51 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/12 "2022-04-09T17:51:12Z")

</div>

Now I got it. My sensors are battery powered so they don't have repeated messages. So the trigger method won't work.  
Is my example with context flow correct?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 April 2022 20:20 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/13 "2022-04-09T20:20:09Z")

</div>

> [@Leopoll](#):
>
> My sensors are battery powered so they don't have repeated messages.

I don't see the relevance of battery power. Can you post a link to the type of sensor?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [9 April 2022 20:23 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/14 "2022-04-09T20:23:40Z")

</div>

My I suggest the Boolean logic node (node-red-contrib-boolean-logic)?  
set it to AND with an invert after.

if not aware of the node:

It asks for the number of topics (and topic name): `Sensor`  
each sensor passes it a boolean with a topic of Sensor# (1,2,3,4....)

operating in AND mode - the node will return `true` if all payloads (for each topic) are `true`  
but then you can invert it.

the sensors inputs can arrive at different times - it doesn't need to come in as 1 big payload.

the moment a `false` is passed - it returns `false` (in AND mode)

---

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ceysa75/32/31340_2.png) [@ceysa75](https://discourse.nodered.org/u/ceysa75)\
**Post date:** [9 April 2022 22:01 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/15 "2022-04-09T22:01:17Z")

</div>

I can't see why this should not work with a trigger node. And for some reason you seem to refuse to focusing on the given recommendations here.

See how it works:

> [@Problem with current state](https://discourse.nodered.org/t/problem-with-current-state/58599/20):
>
> The trick is firstly to use a Switch node to separate motion start and stop. Pass the start message on directly to switch the light on, send the Off message to a Trigger node that waits for the time required and then switches the light off. To stop the light being turned off if the motion starts again before the trigger has timed out, also feed the start message to the trigger, but configure the 'Reset Trigger If' option in the Trigger node to reset if the payload is equal to the start motion …

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [10 April 2022 16:41 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/16 "2022-04-10T16:41:55Z")

</div>

Thanks everyone. The variant with context flow works as I need.  
The variant with trigger is not suitable, precisely because the messages from the sensors are not repeated. For example:  
1.Sensor1 ON  
2.Sensor 2 ON  
3.Sensor 2 OFF  
The lamp should not turn off.  
The variant with boolean node also works. But for it to work after rebooting for the first time, all sensors must work at least once.

```auto
[{"id":"2da6a6319c628b8d","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"fe3b155cbf8835cf","type":"inject","z":"2da6a6319c628b8d","name":"Sensor1 On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Sensor1","payload":"false","payloadType":"bool","x":130,"y":100,"wires":[["3b577c0b94b14716"]]},{"id":"a4e2dc2053d33a51","type":"inject","z":"2da6a6319c628b8d","name":"Sensor1 Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Sensor1","payload":"true","payloadType":"bool","x":130,"y":160,"wires":[["3b577c0b94b14716"]]},{"id":"08fe5d589339de1f","type":"inject","z":"2da6a6319c628b8d","name":"Sensor2 On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Sensor2","payload":"false","payloadType":"bool","x":130,"y":220,"wires":[["3b577c0b94b14716"]]},{"id":"a20b8577fc59325e","type":"inject","z":"2da6a6319c628b8d","name":"Sensor2 Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Sensor2","payload":"true","payloadType":"bool","x":130,"y":280,"wires":[["3b577c0b94b14716"]]},{"id":"463d56234ac9cedc","type":"debug","z":"2da6a6319c628b8d","name":"ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":160,"wires":[]},{"id":"423ddf8967082c0c","type":"debug","z":"2da6a6319c628b8d","name":"OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":240,"wires":[]},{"id":"3b577c0b94b14716","type":"BooleanLogic","z":"2da6a6319c628b8d","name":"","operation":"AND","inputCount":"2","topic":"result","x":340,"y":200,"wires":[["6dee421c08fe25ec"]]},{"id":"6dee421c08fe25ec","type":"Invert","z":"2da6a6319c628b8d","name":"Invert","x":490,"y":200,"wires":[["a60d9e1d0f1d2917"]]},{"id":"a60d9e1d0f1d2917","type":"switch","z":"2da6a6319c628b8d","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":200,"wires":[["463d56234ac9cedc"],["423ddf8967082c0c"]]}]

```

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

---

<div class="post-metadata">

**Author:** ![Mansur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mansur/32/47998_2.png) [@Mansur](https://discourse.nodered.org/u/Mansur)\
**Post date:** [15 April 2022 07:18 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/17 "2022-04-15T07:18:14Z")

</div>

@Leopoll

You may also use OR Gate from this Node Contrib

> **[node-red-contrib-bool-gate](https://flows.nodered.org/node/node-red-contrib-bool-gate)**
>
> Node RED node to include boolean logic gates

Give a discrete "Topic" name to each sensor.

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [21 April 2022 21:14 UTC](https://discourse.nodered.org/t/two-motion-sensors-for-one-lamp/60980/18 "2022-04-21T21:14:39Z")

</div>

Great!. This is the easiest option.
