Combine time and motiondetector?

[
    {
        "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.

You are using some non core node that you don't identify. It shows up as timerswitch in the flow. Can you show exactly what node that is? possibly something like contrib-timerswitch ?

This example may help

[{"id":"06d052e7017b5cd4","type":"inject","z":"f3ea71643a175185","name":"Motion Sensor on","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":140,"y":120,"wires":[["0a89e34502e06b4f"]]},{"id":"0a89e34502e06b4f","type":"change","z":"f3ea71643a175185","name":"Set Motion Payload and topic","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload = \"on\" ? true : false","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"motion","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":140,"wires":[["ee42f8c7f05fa2d3"]]},{"id":"e4d1d973f3adba7a","type":"inject","z":"f3ea71643a175185","name":"Motion Sensor off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":150,"y":180,"wires":[["0a89e34502e06b4f"]]},{"id":"ee42f8c7f05fa2d3","type":"join","z":"f3ea71643a175185","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":200,"wires":[["f4f248a6b67a3c3e"]]},{"id":"9a4568a7c9b951f1","type":"change","z":"f3ea71643a175185","name":"Set Timer Payload and topic","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload = \"on\" ? true :false","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"timer","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":280,"wires":[["ee42f8c7f05fa2d3"]]},{"id":"f4f248a6b67a3c3e","type":"change","z":"f3ea71643a175185","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$not(false in $$.payload.*)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":200,"wires":[["2e78c41513474950","72a910fc2e80e9b9"]]},{"id":"74db70f3eef39935","type":"inject","z":"f3ea71643a175185","name":"timer on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":140,"y":280,"wires":[["9a4568a7c9b951f1"]]},{"id":"e62b813549141660","type":"inject","z":"f3ea71643a175185","name":"timer off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":140,"y":320,"wires":[["9a4568a7c9b951f1"]]},{"id":"2e78c41513474950","type":"trigger","z":"f3ea71643a175185","name":"","op1":"","op2":"false","op1type":"nul","op2type":"bool","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"false","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":260,"wires":[["72a910fc2e80e9b9"]]},{"id":"72a910fc2e80e9b9","type":"rbe","z":"f3ea71643a175185","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":770,"y":200,"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":930,"y":200,"wires":[]}]
1 Like

Yes it is actually the contrib-timerswitch. I did not get this to work tough, so I am still working on a solution to combine the time and the motion sensor to trigger together. But the AI actually showed something who turned out to be a flow and there is somethin in it. I dont know if I am to be scared or not ...

Just tweaked this one a bit so it fitted my setup. Not very far from what I initial taught, but still lack some knowledge. Thank you again E1cid

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