Disable Switch Motion Control for 1 Hour After the light switch is manually pressed

So I almost have this completed but have two bugs I need to work through. The setup is a light which is controlled automatically with a motion sensor but if anyone uses the light switch then it ignores the motion sensor for 1 hour. This happens but has 2 issues.

  1. The logic to determine when a user presses the physical light switch takes 2 - 3 seconds to run and within this time the motion sensor is wildly switching the light fighting the user.
  2. On startup the motion sensor is disabled for the first hour as it needs some kind of logic to fix this issue.

Thoughts?

[{"id":"ac1eb4010d4cc237","type":"comment","z":"8969d5b7eebadbd4","name":"Laundry Room","info":"","x":120,"y":160,"wires":[]},{"id":"3fe68e9aaaf65181","type":"server-state-changed","z":"8969d5b7eebadbd4","name":"Laundry Room Motion Sensor","server":"ad6038b2.acf9a8","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.linknlink_e04b4100811d0000000000007bac0000_m"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":160,"y":200,"wires":[["f3ca2225c9975dee"],[]]},{"id":"8b98be0f64526a27","type":"api-call-service","z":"8969d5b7eebadbd4","name":"","server":"ad6038b2.acf9a8","version":7,"debugenabled":false,"action":"switch.turn_on","floorId":[],"areaId":[],"deviceId":[],"entityId":["switch.laundry_room_light"],"labelId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":true,"domain":"switch","service":"turn_on","x":780,"y":200,"wires":[[]]},{"id":"65c48213fccc69eb","type":"api-call-service","z":"8969d5b7eebadbd4","name":"","server":"ad6038b2.acf9a8","version":7,"debugenabled":false,"action":"switch.turn_off","floorId":[],"areaId":[],"deviceId":[],"entityId":["switch.laundry_room_light"],"labelId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":true,"domain":"switch","service":"turn_off","x":780,"y":280,"wires":[[]]},{"id":"b9542a1d.b26468","type":"comment","z":"8969d5b7eebadbd4","name":"Manually Control or Automatic Control","info":"","x":190,"y":340,"wires":[]},{"id":"27d9c640.ae21fa","type":"switch","z":"8969d5b7eebadbd4","name":"Manual press?","property":"data.new_state.context.user_id","propertyType":"msg","rules":[{"t":"null"},{"t":"nnull"}],"checkall":"true","repair":false,"outputs":2,"x":140,"y":480,"wires":[["cfeca38.8b8296"],["cf796b57.9e3938"]]},{"id":"cfeca38.8b8296","type":"change","z":"8969d5b7eebadbd4","name":"Set \"LaundryRoomManualControl\" to time","rules":[{"t":"set","p":"LaundryRoomManualControl","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":460,"wires":[["db6ea4bae1137394"]]},{"id":"cf796b57.9e3938","type":"change","z":"8969d5b7eebadbd4","name":"Set \"Automated\" to Automated","rules":[{"t":"set","p":"Automated","pt":"flow","to":"Automated","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":500,"wires":[["f62aca7d69778090"]]},{"id":"9d428e35300ce818","type":"server-state-changed","z":"8969d5b7eebadbd4","name":"Laundry Room Motion Sensor","server":"ad6038b2.acf9a8","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.linknlink_e04b4100811d0000000000007bac0000_m"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"off","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"10","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":160,"y":260,"wires":[["d88e8e0d784640a9"],[]]},{"id":"3f3da6a8e0fdc035","type":"server-state-changed","z":"8969d5b7eebadbd4","name":"Laundry Room Light turns on/off?","server":"ad6038b2.acf9a8","version":6,"outputs":1,"exposeAsEntityConfig":"","entities":{"entity":["switch.laundry_room_light"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is_not","outputOnlyOnStateChange":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":190,"y":400,"wires":[["27d9c640.ae21fa"]]},{"id":"f3ca2225c9975dee","type":"function","z":"8969d5b7eebadbd4","name":"Set \"currentTime\"","func":"var d = new Date();\n    msg.currentTime = d.getTime() - global.get(\"MotionSensorDelay\");\n    //msg.oldtime = flow.manualcontrol;\n//return msg;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":160,"wires":[["4c3f0bbd12e0f2bc"]]},{"id":"4c3f0bbd12e0f2bc","type":"switch","z":"8969d5b7eebadbd4","name":"Is Light Manually Controled?","property":"currentTime","propertyType":"msg","rules":[{"t":"lt","v":"LaundryRoomManualControl","vt":"flow"},{"t":"gte","v":"LaundryRoomManualControl","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":540,"y":200,"wires":[[],["8b98be0f64526a27"]]},{"id":"d88e8e0d784640a9","type":"function","z":"8969d5b7eebadbd4","name":"Set \"currentTime\"","func":"var d = new Date();\n    msg.currentTime = d.getTime() - global.get(\"MotionSensorDelay\");\n    //msg.oldtime = flow.manualcontrol;\n//return msg;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":240,"wires":[["d7df2ca055899242"]]},{"id":"d7df2ca055899242","type":"switch","z":"8969d5b7eebadbd4","name":"Is Light Manually Controled?","property":"currentTime","propertyType":"msg","rules":[{"t":"lt","v":"LaundryRoomManualControl","vt":"flow"},{"t":"gte","v":"LaundryRoomManualControl","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":540,"y":280,"wires":[[],["65c48213fccc69eb"]]},{"id":"db6ea4bae1137394","type":"debug","z":"8969d5b7eebadbd4","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"\"Manual Control\"","targetType":"jsonata","statusVal":"","statusType":"auto","x":700,"y":460,"wires":[]},{"id":"f62aca7d69778090","type":"debug","z":"8969d5b7eebadbd4","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"\"Automatic Control\"","targetType":"jsonata","statusVal":"","statusType":"auto","x":700,"y":500,"wires":[]},{"id":"ad6038b2.acf9a8","type":"server","name":"Home Assistant","addon":true}]```

To help someone else with a similar problem here is the solution:

Add triggers to only accept the first message from the motion sensor then reject all signals for the next 5 seconds.

Secondly, at startup the global variable which stores time when it was last press it set to 0 so automatic control works immediately.

[{"id":"ac1eb4010d4cc237","type":"comment","z":"8969d5b7eebadbd4","name":"Laundry Room","info":"","x":100,"y":160,"wires":[]},{"id":"3fe68e9aaaf65181","type":"server-state-changed","z":"8969d5b7eebadbd4","name":"Laundry Room Motion Sensor","server":"ad6038b2.acf9a8","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.linknlink_e04b4100811d0000000000007bac0000_m"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":200,"wires":[["c8a264254c55ba6b"],[]]},{"id":"8b98be0f64526a27","type":"api-call-service","z":"8969d5b7eebadbd4","name":"","server":"ad6038b2.acf9a8","version":7,"debugenabled":false,"action":"switch.turn_on","floorId":[],"areaId":[],"deviceId":[],"entityId":["switch.laundry_room_light"],"labelId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":true,"domain":"switch","service":"turn_on","x":1000,"y":200,"wires":[[]]},{"id":"65c48213fccc69eb","type":"api-call-service","z":"8969d5b7eebadbd4","name":"","server":"ad6038b2.acf9a8","version":7,"debugenabled":false,"action":"switch.turn_off","floorId":[],"areaId":[],"deviceId":[],"entityId":["switch.laundry_room_light"],"labelId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":true,"domain":"switch","service":"turn_off","x":1000,"y":280,"wires":[[]]},{"id":"b9542a1d.b26468","type":"comment","z":"8969d5b7eebadbd4","name":"Manually Control or Automatic Control","info":"","x":170,"y":340,"wires":[]},{"id":"27d9c640.ae21fa","type":"switch","z":"8969d5b7eebadbd4","name":"Manual press?","property":"data.new_state.context.user_id","propertyType":"msg","rules":[{"t":"null"},{"t":"nnull"}],"checkall":"true","repair":false,"outputs":2,"x":380,"y":380,"wires":[["cfeca38.8b8296"],["cf796b57.9e3938"]]},{"id":"cfeca38.8b8296","type":"change","z":"8969d5b7eebadbd4","name":"Set \"LaundryRoomManualControl\" to time","rules":[{"t":"set","p":"LaundryRoomManualControl","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":360,"wires":[[]]},{"id":"cf796b57.9e3938","type":"change","z":"8969d5b7eebadbd4","name":"Set \"LaundryRoomAutomaticTime\" to Automated","rules":[{"t":"set","p":"LaundryRoomAutomaticControl","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":400,"wires":[[]]},{"id":"9d428e35300ce818","type":"server-state-changed","z":"8969d5b7eebadbd4","name":"Laundry Room Motion Sensor","server":"ad6038b2.acf9a8","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.linknlink_e04b4100811d0000000000007bac0000_m"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"off","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"10","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":260,"wires":[["10fc949632114fdf"],[]]},{"id":"3f3da6a8e0fdc035","type":"server-state-changed","z":"8969d5b7eebadbd4","name":"Laundry Room Light turns on/off?","server":"ad6038b2.acf9a8","version":6,"outputs":1,"exposeAsEntityConfig":"","entities":{"entity":["switch.laundry_room_light"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is_not","outputOnlyOnStateChange":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":380,"wires":[["27d9c640.ae21fa"]]},{"id":"f3ca2225c9975dee","type":"function","z":"8969d5b7eebadbd4","name":"Set \"currentTime\"","func":"var d = new Date();\n    msg.currentTime = d.getTime() - global.get(\"MotionSensorDelay\");\n    //msg.oldtime = flow.manualcontrol;\n//return msg;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":160,"wires":[["4c3f0bbd12e0f2bc"]]},{"id":"4c3f0bbd12e0f2bc","type":"switch","z":"8969d5b7eebadbd4","name":"Is Light Manually Controled?","property":"currentTime","propertyType":"msg","rules":[{"t":"lt","v":"LaundryRoomManualControl","vt":"flow"},{"t":"gte","v":"LaundryRoomManualControl","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":760,"y":200,"wires":[[],["8b98be0f64526a27"]]},{"id":"d88e8e0d784640a9","type":"function","z":"8969d5b7eebadbd4","name":"Set \"currentTime\"","func":"var d = new Date();\n    msg.currentTime = d.getTime() - global.get(\"MotionSensorDelay\");\n    //msg.oldtime = flow.manualcontrol;\n//return msg;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":240,"wires":[["d7df2ca055899242"]]},{"id":"d7df2ca055899242","type":"switch","z":"8969d5b7eebadbd4","name":"Is Light Manually Controled?","property":"currentTime","propertyType":"msg","rules":[{"t":"lt","v":"LaundryRoomManualControl","vt":"flow"},{"t":"gte","v":"LaundryRoomManualControl","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":760,"y":280,"wires":[[],["65c48213fccc69eb"]]},{"id":"c8a264254c55ba6b","type":"trigger","z":"8969d5b7eebadbd4","name":"5 Second Cooldown","op1":"","op2":"COOLDOWN OVER","op1type":"pay","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":2,"x":420,"y":180,"wires":[["f3ca2225c9975dee"],[]]},{"id":"10fc949632114fdf","type":"trigger","z":"8969d5b7eebadbd4","name":"5 Second Cooldown","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":2,"x":420,"y":240,"wires":[["d88e8e0d784640a9"],[]]},{"id":"f6058c49494ed173","type":"comment","z":"8969d5b7eebadbd4","name":" Comment","info":"Trigger only allows 1 input every 5 seconds","x":335,"y":140,"wires":[],"l":false},{"id":"196b71a3324fb876","type":"inject","z":"8969d5b7eebadbd4","name":"Load at Startup","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":100,"y":440,"wires":[["71a33a4bf5690864"]]},{"id":"71a33a4bf5690864","type":"change","z":"8969d5b7eebadbd4","name":"Set Initial Value","rules":[{"t":"set","p":"LaundryRoomManualControl","pt":"flow","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":440,"wires":[[]]},{"id":"ad6038b2.acf9a8","type":"server","name":"Home Assistant","addon":true}]