# Outside - Light pir and switch mechanic

**URL:** <https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986>\
**Category:** General\
**Created:** [20 May 2021 15:36 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986 "2021-05-20T15:36:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JobbeDeluxe](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JobbeDeluxe](https://discourse.nodered.org/u/JobbeDeluxe)\
**Post date:** [20 May 2021 15:36 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986/1 "2021-05-20T15:36:51Z")

</div>

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:

```auto
[{"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":""}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 May 2021 16:18 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986/2 "2021-05-20T16:18:17Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fcaf6a64566058dffcb984bfe90d5d814a9a0703.png)

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/38d2c23c48ef81a1c46268dd4368a341686d6f5d.png)

```auto
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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6feafbc9d667ee30650ee782c29b4878a2fb1b7a.png)

---

<div class="post-metadata">

**Author:** ![JobbeDeluxe](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JobbeDeluxe](https://discourse.nodered.org/u/JobbeDeluxe)\
**Post date:** [20 May 2021 16:37 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986/3 "2021-05-20T16:37:16Z")

</div>

cron node. i take a look,

i have something near sample here:

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

---

<div class="post-metadata">

**Author:** ![JobbeDeluxe](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JobbeDeluxe](https://discourse.nodered.org/u/JobbeDeluxe)\
**Post date:** [21 May 2021 11:30 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986/4 "2021-05-21T11:30:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JobbeDeluxe](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JobbeDeluxe](https://discourse.nodered.org/u/JobbeDeluxe)\
**Post date:** [21 May 2021 13:53 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986/5 "2021-05-21T13:53:41Z")

</div>

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 🙂

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

> [@How to set global variable parameters?](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470):
>
> Hi, I would like to store 5 temperature values in one global object. I have created the global object with several temp values: var GlobalTemp = {}; GlobalTemp.a = 10; GlobalTemp.b = 15; GlobalTemp.c = 22; global.set("Temp",GlobalTemp); and I can read the Global Var and get the values I need: var work\_GlobalTemp = global.get("Temp"); msg.payload = work\_GlobalTemp.b; return msg; but I have now Idea how to set a speciel value inside the global object with a new value. Something like global…

> [@If condition with a function](https://discourse.nodered.org/t/if-condition-with-a-function/25389/22):
>
> Perfect! I can manage do that with the switch node instead.

[https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 July 2021 13:54 UTC](https://discourse.nodered.org/t/outside-light-pir-and-switch-mechanic/45986/6 "2021-07-20T13:54:25Z")

</div>

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