# Motion sensor, switch and more

**URL:** https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783
**Category:** General
**Tags:** function-node
**Created:** [16 January 2023 22:04 UTC](https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783 "2023-01-16T22:04:47Z")
**Posts on this page:** 5
**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: [16 January 2023 22:04 UTC](https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783/1 "2023-01-16T22:04:48Z")

</div>

I have a standard light control scheme using a switch and a motion sensor. If the light is turned on by the switch, `context.flow` is `TRUE` and the motion sensor cannot turn off the light.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b3f3506a8657c874af553a1a2c228424a2e937db.png)

But what if I turn on the light from the HA application or through the smart speaker assistant? Then no events are fired so it's impossible to set the `context.flow`  
What is usually done in such cases?

---

<div class="post-metadata">

### Author: ![juanbal](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@juanbal](https://discourse.nodered.org/u/juanbal)
#### Post date: [18 January 2023 19:26 UTC](https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783/2 "2023-01-18T19:26:25Z")

</div>

Typically MQTT is used for that.  
The light (or the device controlling it) would publish light states into the MQTT broker.  
Node-red would subscribe to those publications using the MQTT IN node and that will get your flows triggered each time there's a change with the light state.

---

<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: [18 January 2023 19:43 UTC](https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783/3 "2023-01-18T19:43:57Z")

</div>

> [@juanbal](#):
>
> Typically MQTT is used for that

Thank you. But how can you determine what triggered the turning on of the light? I need to determine that the light turned on NOT from the motion sensor.

---

<div class="post-metadata">

### Author: ![juanbal](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@juanbal](https://discourse.nodered.org/u/juanbal)
#### Post date: [18 January 2023 20:07 UTC](https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783/4 "2023-01-18T20:07:30Z")

</div>

I have some lights that during the night are automatically turned on using a motion sensor.  
Those lights are automatically turned off after 2 minutes using a trigger node.

When the motion sensor triggers on the light I set a flow context variable (activatedByMovement=true).  
Before turning off the light after the 2 minutes, I check if activatedByMovement=true. If yes, the light is turned off. If false or null do nothing.

Here's the flow

```auto
[{"id":"0751ce3d5ebcd276","type":"group","z":"31ba99d2.a9b3b6","name":"night light","style":{"label":true},"nodes":["d8c6808b67891973","c4071d639cc6dec2","8dee798a75e80ea9","67b45d6f47a78f74","9784d0b842fc0597","1afd52782fe41780","e6d6782f06956cef","0e8e8cf779ff0ed9","4c8a1b7bdf2013ba","98f5d3acfa60d37b","ad348fa14f58b0d0","0ff5619ff8f1e71c","3d4630a28dafbf69","f1b2858f22810af0","dc6b12d4842e79f4","5df1e8060c341f31","c4fd10a91ddfc539","1e555d685efd645d","98b7a25a49311b96","b96ecdd4cccd8cb1","d8ed19cc1b39a6f0"],"x":54,"y":579,"w":1232,"h":322},{"id":"d8c6808b67891973","type":"mqtt in","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"MovimentCorridor","topic":"MB3/corridor/presence","qos":"2","datatype":"json","broker":"a1214186.1acad","nl":false,"rap":false,"inputs":0,"x":170,"y":720,"wires":[["67b45d6f47a78f74","8dee798a75e80ea9"]]},{"id":"c4071d639cc6dec2","type":"switch","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"occupancy","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"null"},{"t":"false"}],"checkall":"true","repair":false,"outputs":3,"x":670,"y":680,"wires":[["9784d0b842fc0597","4c8a1b7bdf2013ba","f1b2858f22810af0"],[],[]]},{"id":"8dee798a75e80ea9","type":"switch","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"verifica actiu","property":"#:(file)::nightEspill","propertyType":"flow","rules":[{"t":"true"}],"checkall":"false","repair":false,"outputs":1,"x":370,"y":720,"wires":[["3d4630a28dafbf69"]]},{"id":"67b45d6f47a78f74","type":"debug","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"Night Light raw","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":180,"y":660,"wires":[]},{"id":"9784d0b842fc0597","type":"debug","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"ocuppancy true","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":860,"y":660,"wires":[]},{"id":"1afd52782fe41780","type":"debug","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"Fora horari","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":510,"y":780,"wires":[]},{"id":"e6d6782f06956cef","type":"comment","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"Encen la llum","info":"Si detecta moviment dins de l'horari i està activat","x":1190,"y":660,"wires":[]},{"id":"0e8e8cf779ff0ed9","type":"inject","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"battery\":100,\"illuminance\":0,\"illuminance_lux\":0,\"linkquality\":42,\"occupancy\":false,\"voltage\":3025}","payloadType":"json","x":150,"y":620,"wires":[["8dee798a75e80ea9"]]},{"id":"4c8a1b7bdf2013ba","type":"change","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"Espill ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state_right\":\"ON\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":700,"wires":[["1e555d685efd645d","98b7a25a49311b96"]]},{"id":"98f5d3acfa60d37b","type":"change","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"auto mirror","rules":[{"t":"set","p":"#:(file)::nightEspill","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":840,"wires":[[]]},{"id":"ad348fa14f58b0d0","type":"inject","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"true","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":150,"y":820,"wires":[["98f5d3acfa60d37b"]]},{"id":"0ff5619ff8f1e71c","type":"inject","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":150,"y":860,"wires":[["98f5d3acfa60d37b"]]},{"id":"3d4630a28dafbf69","type":"within-time-switch","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"Horari","nameInt":"","positionConfig":"a39cfa79.ae17c8","startTime":"23:00","startTimeType":"entered","startOffset":0,"startOffsetType":"none","startOffsetMultiplier":60000,"endTime":"civilDawn","endTimeType":"pdsTime","endOffset":0,"endOffsetType":"none","endOffsetMultiplier":60000,"timeRestrictions":"","timeRestrictionsType":"none","timeDays":"*","timeOnlyOddDays":false,"timeOnlyEvenDays":false,"timeMonths":"*","timedatestart":"","timedateend":"","propertyStart":"","propertyStartType":"none","propertyStartCompare":"true","propertyStartThreshold":"","propertyStartThresholdType":"num","startTimeAlt":"","startTimeAltType":"entered","startOffsetAlt":0,"startOffsetAltType":"none","startOffsetAltMultiplier":60000,"propertyEnd":"","propertyEndType":"none","propertyEndCompare":"true","propertyEndThreshold":"","propertyEndThresholdType":"num","endTimeAlt":"","endTimeAltType":"entered","endOffsetAlt":0,"endOffsetAltType":"none","endOffsetAltMultiplier":60000,"tsCompare":"0","x":510,"y":720,"wires":[["c4071d639cc6dec2"],["1afd52782fe41780"]]},{"id":"f1b2858f22810af0","type":"change","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"activatedByMovement -> true","rules":[{"t":"set","p":"activatedByMovement","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":620,"wires":[[]]},{"id":"dc6b12d4842e79f4","type":"change","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"activatedByMovement -> false","rules":[{"t":"set","p":"activatedByMovement","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":820,"wires":[[]]},{"id":"5df1e8060c341f31","type":"switch","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"if activatedByMovement","property":"activatedByMovement","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":770,"y":820,"wires":[["dc6b12d4842e79f4","c4fd10a91ddfc539"]]},{"id":"c4fd10a91ddfc539","type":"change","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"Espill OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state_right\":\"OFF\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":780,"wires":[["1e555d685efd645d"]]},{"id":"1e555d685efd645d","type":"mqtt out","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"XA_switchBanyMenut","topic":"zigbee2mqtt/XA_switchBanyMenut/set","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"a1214186.1acad","x":1100,"y":700,"wires":[]},{"id":"98b7a25a49311b96","type":"trigger","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"","op1":"","op2":"false","op1type":"nul","op2type":"bool","duration":"120","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":735,"y":760,"wires":[["5df1e8060c341f31"]],"l":false},{"id":"b96ecdd4cccd8cb1","type":"mqtt in","z":"31ba99d2.a9b3b6","d":true,"g":"0751ce3d5ebcd276","name":"MB3/bedroom/presence","topic":"MB3/bedroom/presence","qos":"2","datatype":"json","broker":"a1214186.1acad","nl":false,"rap":false,"inputs":0,"x":180,"y":760,"wires":[["8dee798a75e80ea9"]]},{"id":"d8ed19cc1b39a6f0","type":"link in","z":"31ba99d2.a9b3b6","g":"0751ce3d5ebcd276","name":"tradfi out apaga night lights bany menut","links":["c462d03cbd614efa"],"x":595,"y":820,"wires":[["5df1e8060c341f31"]],"info":"Si fem long press en el tradfy apaga els night light"},{"id":"a1214186.1acad","type":"mqtt-broker","name":"domoserver","broker":"192.168.2.46","port":"1883","clientid":"nodeRed","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"a39cfa79.ae17c8","type":"position-config","name":"MB3","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"}]

```

---

<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: [19 March 2023 21:13 UTC](https://discourse.nodered.org/t/motion-sensor-switch-and-more/73783/6 "2023-03-19T21:13:21Z")

</div>

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