Motion Sensor Light

Hi guys. I am trying to automate my guest bathroom to when it detects movement, it turns the light on immediately. I also want it to turn the bathroom fan on when it keeps detecting movement after 3 minutes and the bathroom fan and light should turn off after it doesn’t detect any movement.

I am new to NodeRed and this is what I was able to accomplish. The light turns off automatically after a minute even if it keeps detecting movement. How can I make this work? If you have a code please share.

This can get real complicated. Howver to fix your light off problem… remove the wire from the switch on to the 1min timer…

only need to start that timer when the movement is ‘off’.

let me see what I have for you

Something like this should get you going…

[{"id":"4bac02e1.35134c","type":"tab","label":"Flow 10","disabled":false,"info":""},{"id":"abad2e2a.2cf0b","type":"inject","z":"4bac02e1.35134c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":330,"wires":[["bd4e10ba.e14aa"]]},{"id":"bd4e10ba.e14aa","type":"switch","z":"4bac02e1.35134c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":400,"wires":[["1fbdc613.9e29ea","7df1dbe.0b60f24"],["a5af65a6.257038"]]},{"id":"1fbdc613.9e29ea","type":"stoptimer","z":"4bac02e1.35134c","duration":"3","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":740,"y":280,"wires":[["2a7bb511.e5e9ca"],[]]},{"id":"2a7bb511.e5e9ca","type":"api-current-state","z":"4bac02e1.35134c","name":"Motion Still On?","server":"663af70e.88b558","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"binary_sensor.wyzesense_bathmotion","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":760,"y":350,"wires":[["5d3164d2.90e90c"],["1fbdc613.9e29ea"]]},{"id":"5d3164d2.90e90c","type":"api-call-service","z":"4bac02e1.35134c","name":"turn bath fan on","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"fan","service":"turn_on","entityId":"fan.bathroom","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1000,"y":320,"wires":[[]]},{"id":"a5af65a6.257038","type":"stoptimer","z":"4bac02e1.35134c","duration":"1","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":740,"y":470,"wires":[["cad446fa.290ae8"],[]]},{"id":"138297d7.2652e8","type":"api-call-service","z":"4bac02e1.35134c","name":"turn light off","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.bath","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1120,"y":460,"wires":[[]]},{"id":"cad446fa.290ae8","type":"api-current-state","z":"4bac02e1.35134c","name":"Motion Still On?","server":"663af70e.88b558","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"binary_sensor.wyzesense_bathmotion","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":920,"y":460,"wires":[["a5af65a6.257038"],["138297d7.2652e8","3fbcf22d.53f84e"]]},{"id":"7df1dbe.0b60f24","type":"api-current-state","z":"4bac02e1.35134c","name":"Allready on?","server":"663af70e.88b558","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bath","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":754.5078125,"y":402.5234375,"wires":[[],["2e2a21c7.140dde"]]},{"id":"2e2a21c7.140dde","type":"api-call-service","z":"4bac02e1.35134c","name":"turn bath light on","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.bath","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1010,"y":380,"wires":[[]]},{"id":"3fbcf22d.53f84e","type":"api-call-service","z":"4bac02e1.35134c","name":"turn bath fan off","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"fan","service":"turn_off","entityId":"fan.bathroom","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1150,"y":520,"wires":[[]]},{"id":"663af70e.88b558","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

It isnt perfect. the 3min timer will remain looping if it doesnt get to 3 mins… ways around it, but it should help you get going.

Personally. mine is always getting little fixes, and to be honest, its a mess but it works.

What helps mostly is having a door sensor. if you are showering and lights turn off… not fun.
or a guest is standing/sitting real still using the toliet lights turn off… no fun

Alot going on:

motion sensor. door sensor, a second door sensor on toliet, and a oil diffuser when the toliet flushes…:

I actually have a similar use case, and for that reason i created a small subflow to deal with that.

[{"id":"38ecdf53.b21a","type":"function","z":"914a9b73.c28298","name":"on/off","func":"var stop = {payload:\"STOP\"}\n\nif (msg.payload === \"on\") {\n   return [ msg, stop ];\n} else {\n   return [ null, msg ];\n}\nreturn msg\n\n","outputs":2,"noerr":0,"x":270,"y":140,"wires":[["ed97cd7e.57ca7"],["2ce8c212.97a0ce"]]},{"id":"e5afe76f.aa45a8","type":"inject","z":"914a9b73.c28298","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":80,"wires":[["38ecdf53.b21a"]]},{"id":"deefcb6a.ad8a38","type":"inject","z":"914a9b73.c28298","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":40,"wires":[["38ecdf53.b21a"]]},{"id":"2ce8c212.97a0ce","type":"stoptimer","z":"914a9b73.c28298","duration":"10","units":"Second","payloadtype":"str","payloadval":"","name":"","x":430,"y":180,"wires":[["ed97cd7e.57ca7"],[]]},{"id":"ed97cd7e.57ca7","type":"rbe","z":"914a9b73.c28298","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":590,"y":140,"wires":[[]]}]

I believe you can translate that into your usecase. If you have some issues let me know.

This worked great! Thank you, Jim! I appreciate your help and suggestion!