[
{
"id": "f3ea71643a175185",
"type": "tab",
"label": "Motion and Timer",
"disabled": false,
"info": ""
},
{
"id": "06d052e7017b5cd4",
"type": "inject",
"z": "f3ea71643a175185",
"name": "Motion Sensor",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "on",
"payloadType": "str",
"x": 170,
"y": 140,
"wires": [
[
"0a89e34502e06b4f"
]
]
},
{
"id": "0a89e34502e06b4f",
"type": "change",
"z": "f3ea71643a175185",
"name": "Set Motion Payload",
"rules": [
{
"t": "change",
"p": "payload",
"pt": "msg",
"from": "on",
"fromt": "str",
"to": "true",
"tot": "bool"
},
{
"t": "set",
"p": "motionState",
"pt": "flow",
"to": "payload",
"tot": "msg"
},
{
"t": "set",
"p": "motionTimestamp",
"pt": "flow",
"to": "timestamp",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 400,
"y": 140,
"wires": [
[
"8c76c5178b8421aa"
]
]
},
{
"id": "9a4568a7c9b951f1",
"type": "change",
"z": "f3ea71643a175185",
"name": "Set Timer Payload",
"rules": [
{
"t": "change",
"p": "payload",
"pt": "msg",
"from": "on",
"fromt": "str",
"to": "true",
"tot": "bool"
},
{
"t": "set",
"p": "timerState",
"pt": "flow",
"to": "payload",
"tot": "msg"
},
{
"t": "set",
"p": "timerTimestamp",
"pt": "flow",
"to": "timestamp",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 400,
"y": 240,
"wires": [
[
"8c76c5178b8421aa"
]
]
},
{
"id": "8c76c5178b8421aa",
"type": "function",
"z": "f3ea71643a175185",
"name": "Check Motion and Timer",
"func": "var motionState = flow.get('motionState');\nvar timerState = flow.get('timerState');\n\nif (motionState === true && timerState === true) {\n // Both motion and timer are active\n var motionTimestamp = flow.get('motionTimestamp');\n var timerTimestamp = flow.get('timerTimestamp');\n var currentTime = Date.now();\n var elapsedTime = currentTime - Math.max(motionTimestamp, timerTimestamp);\n\n if (elapsedTime >= 5*60*1000) {\n // Five minutes have passed\n return { payload: 'off' };\n }\n\n return { payload: 'on' };\n}\n\nreturn { payload: 'off' };",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 600,
"y": 190,
"wires": [
[
"58258ec9a5141836"
]
]
},
{
"id": "58258ec9a5141836",
"type": "debug",
"z": "f3ea71643a175185",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 810,
"y": 190,
"wires": []
},
{
"id": "2482288f913fcc97",
"type": "timerswitch",
"z": "f3ea71643a175185",
"name": "",
"ontopic": "timer_gang_natt",
"offtopic": "",
"onpayload": "",
"offpayload": "",
"disabled": false,
"schedules": [
{
"on_h": "10",
"on_m": "00",
"on_s": "01",
"off_h": "06",
"off_m": "44",
"off_s": "59",
"valid": true
}
],
"x": 150,
"y": 240,
"wires": [
[
"9a4568a7c9b951f1"
]
]
}
]
My goal is to set a scene of light when motion is detected and if within the correct time. As I struggled a bit of how to combine them and also taking in consideration that I need the On to stay on for 5 min, before it sends OFF. I decided to do what some of my students do, ask chatgpt. This is the flow chatgpt suggested. But I am unsure if it really would work.