Outside - Light pir and switch mechanic

Hello,

i am new on Node-red. I managed to make a simple switch on, light on.
Also i managed to trigger pir to switch on the light.

mein main problem is now:
The light needs only be triggerd on time after it is dark (sunset or before sunrise). AND it should not turn off when the switch is ON.

so the switch sould have "automatic" or "ON"

need some Help with the mechanic.

here is the flow:

[{"id":"ad478da0.376af","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"b468130f.791ea","type":"debug","z":"ad478da0.376af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":130,"y":60,"wires":[]},{"id":"706880d3.1dbd7","type":"mqtt in","z":"ad478da0.376af","name":"PIR Sensor Online?","topic":"tele/tasmota_7C7AA1/LWT","qos":"2","datatype":"auto","broker":"ceba0b4b.cd64b8","nl":false,"rap":true,"rh":0,"x":130,"y":140,"wires":[["b468130f.791ea","5943044c.1f6b7c"]]},{"id":"c326c00e.893cd","type":"mqtt in","z":"ad478da0.376af","name":"Drausen Licht Schalter Status","topic":"stat/tasmota_107217/SWITCH1","qos":"2","datatype":"auto","broker":"ceba0b4b.cd64b8","nl":false,"rap":true,"rh":0,"x":160,"y":380,"wires":[["ed4fb0bc.16043"]]},{"id":"ed4fb0bc.16043","type":"mqtt out","z":"ad478da0.376af","name":"DrauĂźen Licht Power","topic":"cmnd/tasmota_107217/Power1","qos":"","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ceba0b4b.cd64b8","x":440,"y":380,"wires":[]},{"id":"52da26fa.13a858","type":"mqtt in","z":"ad478da0.376af","name":"","topic":"tele/tasmota_7C7AA1/SENSOR","qos":"2","datatype":"auto","broker":"ceba0b4b.cd64b8","nl":false,"rap":true,"rh":0,"x":170,"y":200,"wires":[["228c033b.11093c","d9270bbc.559d38"]]},{"id":"228c033b.11093c","type":"debug","z":"ad478da0.376af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":280,"wires":[]},{"id":"5e736e5c.ec74","type":"mqtt in","z":"ad478da0.376af","name":"","topic":"tele/tasmota_7C7AA1/STATE","qos":"2","datatype":"auto","broker":"ceba0b4b.cd64b8","nl":false,"rap":true,"rh":0,"x":160,"y":260,"wires":[["228c033b.11093c"]]},{"id":"5943044c.1f6b7c","type":"switch","z":"ad478da0.376af","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Online","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":390,"y":80,"wires":[["d9270bbc.559d38"]]},{"id":"d9270bbc.559d38","type":"trigger","z":"ad478da0.376af","name":"","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"13","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":570,"y":140,"wires":[["1516a72b.d0bb09"]]},{"id":"1516a72b.d0bb09","type":"mqtt out","z":"ad478da0.376af","name":"","topic":"cmnd/tasmota_663943/POWER","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ceba0b4b.cd64b8","x":690,"y":220,"wires":[]},{"id":"ceba0b4b.cd64b8","type":"mqtt-broker","name":"local broker","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Hi, I've moved this to "General" as it isn't about creating nodes.

There are a whole bunch of ways to do this that depend on your needs and what information you are collecting.

Personally, I've started keeping flag variables that mark daylight and sleep times for example and I use those to filter the on/off switches if they are at a fixed time.

For other uses though, you may want to make use of one of the scheduler nodes that has sun and other astronomical events as outputs so that you can choose a variable time directly.

In your case, I think you want the former. Use something like the sun-position node to track your daylight/night cutoffs. Then monitor the PIR trigger and filter the output depending on whether your day/night variable is indicating night or not.

image

Here is a really simplistic example of only turning a light on if it isn't "sleep" time and if a light sensor is showing less than 15 lux:

const homeInfo = global.get('homeInfo', 'file')

if ( homeInfo.sleep === "Off" ) {
    if ( msg.payload <= 15 ) {
        return { 
            'topic': 'zigbee2mqtt/Ikea_Bulb_01/set',
            'payload': { "state": "on" },
        }
    }
}

Not quite what you want but gives you an idea of how to mix a variable with a flow.

I also have separate flows that adjust the brightness level of some lights depending on the time of day.

Here using the CRON+ node:

1 Like

cron node. i take a look,

i have something near sample here:

[{"id":"8ee2f4f0.f10ad8","type":"trigger","z":"55b1b128.f579e","name":"Timeout","op1":"1","op2":"off","op1type":"nul","op2type":"val","duration":"3","extend":"true","units":"s","outputs":1,"x":340,"y":180,"wires":[["15a2f333.a8bfad"]]},{"id":"8955340e.790328","type":"inject","z":"55b1b128.f579e","name":"ON","repeat":"","crontab":"","once":false,"topic":"switch","payload":"on","payloadType":"string","x":142,"y":132,"wires":[["8ee2f4f0.f10ad8","18b4b781.276e38"]]},{"id":"51c1fead.9ab95","type":"inject","z":"55b1b128.f579e","name":"","repeat":"","crontab":"","once":false,"topic":"PIR","payload":"","payloadType":"date","x":146,"y":176,"wires":[["8ee2f4f0.f10ad8"]]},{"id":"18b4b781.276e38","type":"debug","z":"55b1b128.f579e","name":"Light","active":true,"console":"false","complete":"payload","x":606,"y":110,"wires":[]},{"id":"944933ba.e4732","type":"inject","z":"55b1b128.f579e","name":"OFF","repeat":"","crontab":"","once":false,"topic":"switch","payload":"off","payloadType":"string","x":141,"y":88,"wires":[["18b4b781.276e38"]]},{"id":"15a2f333.a8bfad","type":"function","z":"55b1b128.f579e","name":"Enabled","func":"\nif (msg.topic == \"state\") {\n\tcontext.state = msg.payload;\n\treturn null;\n}\nif (context.state == 1)\n  return msg;\nelse\n  return null;\n ","outputs":1,"x":475,"y":211,"wires":[["18b4b781.276e38"]]},{"id":"3fa1ef7d.553c1","type":"inject","z":"55b1b128.f579e","name":"Disable Timer","repeat":"","crontab":"","once":false,"topic":"state","payload":"0","payloadType":"string","x":174,"y":218,"wires":[["15a2f333.a8bfad"]]},{"id":"5ee23aea.638514","type":"inject","z":"55b1b128.f579e","name":"Enable Timer","repeat":"","crontab":"","once":false,"topic":"state","payload":"1","payloadType":"string","x":172,"y":258,"wires":[["15a2f333.a8bfad"]]}]

but there is the daytime missing, and i do not how to change the "Switch ON input" to the function node.

what is the node? i would like to have a if that than do this and if that than do nothing....

i know a littl ebash scripting. but this looks differant to me.

what kind of language is that? may i can read me in and do my own function node.

i found a Nighttimer, that set an global var. taht is ok.

now my last Problem is only that i need to get a var out of my local mqtt broker inside of the FUnction node. How can i do that?

i tryed: " var switch = stat/tasmota_107217/SWITCH1" but this will have nothing inside.

i do not know how to tell the function node the variable of the switch position...

someone have a hint?

Ok,

now!

i used a change node to put the status of the swotch in a global var, that can be used by the function node, too.

so now i have If it is dark (depends on my position) and if switch is "off" it will let the PIR trigger... if on of this is not set. It will do nothing.

great job :slight_smile:

thanks for your input und to the other thretads that help me out:

https://nodered.org/docs/user-guide/writing-functions

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