Motion Sensor Light ON If within Time Range/Schedule

#general

Motion Sensor Light ON If within Time Range/Schedule

Hello all,
This is my first post here. I'm pretty new to all this stuff, and looking for help. I would really appreciate any tips to solve the following.

I'm not using HomeAssistant, just node-red and MQTT.

I've got;

  • a bedside lamp hooked up to a sonoff basic switch (tasmoto firmware).
  • a PIR motion sensor hooked up to d1 mini
  • a Pi 3 successfully running Node-red and Mosquitto (MQTT broker)

How can create a flow that:

  • Uses a time/light schedule to switch on the lamp (e.g between sunset and midnight)
  • ONLY IF motion is detected. Else, don't switch on the light as per the time schedule

I've tried several approaches found on this forum, but nearly all of them have incorporated Home Assistant (nodes) which I don't (intend to) use.

I can succesfully, separately, using MQTT topics;

  • Get the light turning on and off ok with just the light-scheduler.
  • Switch the light on with just motion detection, using a trigger with whatever time delay to switch off again.

Below is my flow as it currently stands.

Again, any help much appreciated!

[
    {
        "id": "d5a55d5358bf9c58",
        "type": "tab",
        "label": "Motion Light If Time Range",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d0e63953bdd549a5",
        "type": "mqtt in",
        "z": "d5a55d5358bf9c58",
        "name": "Motion detected",
        "topic": "stat/porch/sensors/PIR1",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "b0c9ee69a4523c65",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 180,
        "y": 260,
        "wires": [
            [
                "3ce78c8000a3284e"
            ]
        ]
    },
    {
        "id": "4d406c9d48d76811",
        "type": "mqtt out",
        "z": "d5a55d5358bf9c58",
        "name": "Bedroom Lamp ON/OFF",
        "topic": "cmnd/bedroom/LampB/POWER",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "b0c9ee69a4523c65",
        "x": 910,
        "y": 260,
        "wires": []
    },
    {
        "id": "69248e2c42cb3399",
        "type": "light-scheduler",
        "z": "d5a55d5358bf9c58",
        "settings": "b9f0a12258ae5bc5",
        "events": "[{\"start\":{\"dow\":5,\"mod\":1170},\"end\":{\"dow\":6,\"mod\":0}},{\"start\":{\"dow\":6,\"mod\":1170},\"end\":{\"dow\":0,\"mod\":0}},{\"start\":{\"dow\":0,\"mod\":1170},\"end\":{\"dow\":1,\"mod\":0}},{\"start\":{\"dow\":2,\"mod\":1170},\"end\":{\"dow\":3,\"mod\":0}},{\"start\":{\"dow\":1,\"mod\":1165},\"end\":{\"dow\":2,\"mod\":0}}]",
        "topic": "",
        "name": "Bedroom Lamp timer",
        "onPayload": "ON",
        "onPayloadType": "str",
        "offPayload": "OFF",
        "offPayloadType": "str",
        "onlyWhenDark": true,
        "scheduleRndMax": 0,
        "sunElevationThreshold": 6,
        "sunShowElevationInStatus": false,
        "outputfreq": "output.statechange.startup",
        "x": 640,
        "y": 260,
        "wires": [
            [
                "4d406c9d48d76811"
            ]
        ]
    },
    {
        "id": "3ce78c8000a3284e",
        "type": "trigger",
        "z": "d5a55d5358bf9c58",
        "name": "",
        "op1": "ON",
        "op2": "OFF",
        "op1type": "str",
        "op2type": "str",
        "duration": "5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 2,
        "x": 420,
        "y": 260,
        "wires": [
            [
                "69248e2c42cb3399"
            ],
            []
        ]
    },
    {
        "id": "b0c9ee69a4523c65",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.0.106",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "b9f0a12258ae5bc5",
        "type": "light-scheduler-settings",
        "name": "my settings",
        "latitude": "40.729675",
        "longitude": " -73.996925"
    }
]

Hello @Step_By_Step, welcome to Node-red

You have two conditions: a) is it within the approved hours? and b) is motion sensed?
Somehow you need to combine these.

The way I would approach it is with a node-red-contrib-simple-gate node. This lets you block or pass messages on a timetable:

[{"id":"d0e63953bdd549a5","type":"mqtt in","z":"d5a55d5358bf9c58","name":"Motion detected","topic":"stat/porch/sensors/PIR1","qos":"2","datatype":"auto-detect","broker":"754230cc.99c44","nl":false,"rap":true,"rh":0,"inputs":0,"x":260,"y":160,"wires":[["e5435415add6710f"]]},{"id":"e5435415add6710f","type":"gate","z":"d5a55d5358bf9c58","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":490,"y":120,"wires":[["6eb0bad255012539"]]},{"id":"61566b20373b9bd5","type":"inject","z":"d5a55d5358bf9c58","name":"Open gate at 19:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 19 * * *","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":240,"y":80,"wires":[["e5435415add6710f"]]},{"id":"c1aecf4b6f13579f","type":"inject","z":"d5a55d5358bf9c58","name":"Close gate at 23:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 23 * * *","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":240,"y":120,"wires":[["e5435415add6710f"]]},{"id":"6eb0bad255012539","type":"mqtt out","z":"d5a55d5358bf9c58","name":"Control Light","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"754230cc.99c44","x":670,"y":120,"wires":[]},{"id":"754230cc.99c44","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

I have just used inject nodes to open and close the gate but you might prefer node-red-contrib-cron-plus for better timing control, even including seasonal variability like "sunset plus 45 minutes".

I never bother with turning off my Tasmota controlled devices - one lamp and my coffee machine.
Instead I use theTasmota's PulseTime1 option which automatically turns off after a set time.

eg If PulseTime1 is set to 700, the light will turn off 600 seconds after motion stops being detected.

Thanks for your response. Sorry fot the late reply. Was busy over the xmas period.

I found the gate to be very useful once i understood how to send it messages to open or close!

Also, thaks for the suggestion for node-red-contrib-cron-plus node. I really like it. Especially when using the Fan Out output option.

This then created the outputs i needed from the node in order to have the control i need.

Unfortunately the sonoff switch i was using decided to completely brick, so i am in the process of trouble shooting/replacing it with another.

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