# Detection and lighting logic

**URL:** <https://discourse.nodered.org/t/detection-and-lighting-logic/14323>\
**Category:** General\
**Created:** [14 August 2019 17:27 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323 "2019-08-14T17:27:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poodlepapa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/poodlepapa/32/8174_2.png) [@Poodlepapa](https://discourse.nodered.org/u/Poodlepapa)\
**Post date:** [14 August 2019 17:27 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/1 "2019-08-14T17:27:03Z")

</div>

I have tried many different things to get this to work. Below is a table of how I would like the logic of this flow to work. I am using MQTT to send the relevant data to a Pi. Currently the lights turn on at dusk and off at 22:00. I have a light sensor that sends out a 1 or 0 via MQTT depending on light level and turns on the lights at dusk, I also have an inject node that turns off the lights at 22:00, it also works great.  
I would like to add a feature that would turn on the lights if these conditions exist:  
A- dark out  
B- the lights are off  
C- if the lights are already on it should ignore the detection.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/09f247879973ce8e8c8c50972c8b34595248e336.png)  
Both variables are arriving via MQTT here is what I've tried, there have been many others but I did not keep them.

![11%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2230e62b99d98a0a02805c424f34b0613ceb8410.png)

```auto
[{"id":"33cb17ae.2d524","type":"function","z":"c631bfb4.5cc718","name":"Twisted Logic","func":"\nvar d2d = msg.d2d;\nvar detection = msg.dec;\n\nif (detection >= 1 && d2d <=0) {\n return {payload: 0}\n} else if (detection >= 1 && d2d <=1) {\n return {payload: 1}\n}\n","outputs":1,"noerr":0,"x":360,"y":780,"wires":[["4928a604.f19f9"]]},{"id":"4928a604.f19f9","type":"mqtt out","z":"c631bfb4.5cc718","name":"","topic":"office CP","qos":"","retain":"","broker":"cd0995e3.fc6f58","x":540,"y":780,"wires":[]},{"id":"72d968b3.abd44","type":"mqtt in","z":"c631bfb4.5cc718","name":"Dusk till Dawn","topic":"d2d","qos":"2","datatype":"auto","broker":"cd0995e3.fc6f58","x":170,"y":760,"wires":[["33cb17ae.2d524"]]},{"id":"40d01c94.befd94","type":"mqtt in","z":"c631bfb4.5cc718","name":"Detection","topic":"dec","qos":"2","datatype":"auto","broker":"cd0995e3.fc6f58","x":180,"y":800,"wires":[["33cb17ae.2d524"]]},{"id":"cd0995e3.fc6f58","type":"mqtt-broker","z":"","name":"","broker":"192.168.0.50","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 August 2019 18:13 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/2 "2019-08-14T18:13:56Z")

</div>

Remember that each msg that arrives at the function node will process thru it by itself. So if a msg comes from the d2d mqtt leg, the var d2d will have data, but the var detection will not.

If you want to use data from both mqtt nodes, you need to join them or state the data in context

---

<div class="post-metadata">

**Author:** ![Poodlepapa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/poodlepapa/32/8174_2.png) [@Poodlepapa](https://discourse.nodered.org/u/Poodlepapa)\
**Post date:** [14 August 2019 19:04 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/3 "2019-08-14T19:04:02Z")

</div>

deleted this post

---

<div class="post-metadata">

**Author:** ![Poodlepapa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/poodlepapa/32/8174_2.png) [@Poodlepapa](https://discourse.nodered.org/u/Poodlepapa)\
**Post date:** [16 August 2019 16:15 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/4 "2019-08-16T16:15:37Z")

</div>

Could you take a look at this please. I think I've got the globals working, I'm not sure why i can't get any output from the function node. Thank you

```auto
[{"id":"33cb17ae.2d524","type":"function","z":"c631bfb4.5cc718","name":"Twisted Logic","func":"//d2d =context.get(d2d1);\n//detection =context.get(dec1);\n//var d2d = msg.d2d1;\n//var detection = msg.dec1;\nvar detection=global.dec1;\nvar d2d=global.d2d1;\n\nif (detection >= 1 && d2d <=0) {\n return {payload: 0}\n} else if (detection >= 1 && d2d <=1) {\n return {payload: 1}\n}\n","outputs":1,"noerr":0,"x":480,"y":820,"wires":[["4928a604.f19f9","fef2911f.8fedc"]]},{"id":"4928a604.f19f9","type":"mqtt out","z":"c631bfb4.5cc718","name":"","topic":"office CP","qos":"","retain":"","broker":"cd0995e3.fc6f58","x":640,"y":780,"wires":[]},{"id":"72d968b3.abd44","type":"mqtt in","z":"c631bfb4.5cc718","name":"d2d1","topic":"d2d1","qos":"2","datatype":"auto","broker":"cd0995e3.fc6f58","x":290,"y":800,"wires":[["33cb17ae.2d524"]]},{"id":"40d01c94.befd94","type":"mqtt in","z":"c631bfb4.5cc718","name":"Detection","topic":"dec1","qos":"2","datatype":"auto","broker":"cd0995e3.fc6f58","x":280,"y":840,"wires":[["33cb17ae.2d524"]]},{"id":"4b72c8fd.4397c8","type":"change","z":"c631bfb4.5cc718","name":"","rules":[{"t":"set","p":"d2d1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":900,"wires":[["519e5b5b.9ace64"]]},{"id":"595bca44.bf6ae4","type":"change","z":"c631bfb4.5cc718","name":"","rules":[{"t":"set","p":"dec1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1000,"wires":[["769c47d4.8d1718"]]},{"id":"519e5b5b.9ace64","type":"mqtt out","z":"c631bfb4.5cc718","name":"","topic":"d2d1","qos":"","retain":"","broker":"cd0995e3.fc6f58","x":490,"y":900,"wires":[]},{"id":"1e34b1a7.ce618e","type":"inject","z":"c631bfb4.5cc718","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":880,"wires":[["4b72c8fd.4397c8"]]},{"id":"f778d6e5.043598","type":"inject","z":"c631bfb4.5cc718","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":980,"wires":[["595bca44.bf6ae4"]]},{"id":"33a43f33.89c88","type":"inject","z":"c631bfb4.5cc718","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":920,"wires":[["4b72c8fd.4397c8"]]},{"id":"6aa492a9.0ae094","type":"inject","z":"c631bfb4.5cc718","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1020,"wires":[["595bca44.bf6ae4"]]},{"id":"fef2911f.8fedc","type":"debug","z":"c631bfb4.5cc718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":840,"wires":[]},{"id":"769c47d4.8d1718","type":"mqtt out","z":"c631bfb4.5cc718","name":"","topic":"dec1","qos":"","retain":"","broker":"cd0995e3.fc6f58","x":490,"y":1000,"wires":[]},{"id":"cd0995e3.fc6f58","type":"mqtt-broker","z":"","name":"","broker":"192.168.0.50","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 August 2019 16:39 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/5 "2019-08-16T16:39:21Z")

</div>

I would start by installing `node-red-contrib-time-range-switch`.

---

<div class="post-metadata">

**Author:** ![Poodlepapa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/poodlepapa/32/8174_2.png) [@Poodlepapa](https://discourse.nodered.org/u/Poodlepapa)\
**Post date:** [16 August 2019 17:07 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/6 "2019-08-16T17:07:58Z")

</div>

AB-SO-LUT-LY brilliant! thank you thank you! This solves so many of the issues! Truly the one node I was looking for. I will post the flow and results tonight when I can test it!! Thanks again.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 August 2019 17:09 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/7 "2019-08-16T17:09:19Z")

</div>

You need to go read this [https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<div class="post-metadata">

**Author:** ![Poodlepapa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/poodlepapa/32/8174_2.png) [@Poodlepapa](https://discourse.nodered.org/u/Poodlepapa)\
**Post date:** [16 August 2019 17:15 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/8 "2019-08-16T17:15:02Z")

</div>

Thank you zenofmud the time range node has done the heavy lifting for me. It handles the time logic so I don't have to flop my way through the code. I have read that page many times and I am missing something fundamental, I hate to seem lazy but I'm going to use this node for now and cross the multiple compared variables bridge at a later time. Thank you again

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 August 2019 17:27 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/9 "2019-08-16T17:27:16Z")

</div>

Basically you only have to check for detection between 22:00 and sunrise.  
You could easily create 2 flows, one for the detection time range and the other for the lights on/off.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 August 2019 19:13 UTC](https://discourse.nodered.org/t/detection-and-lighting-logic/14323/10 "2019-08-16T19:13:07Z")

</div>

The main thing is that in a function node to use a global you must first `get` it i.e.  
`var detection = global.get("dec1");`  
and don't confust context with flow and/or global's they are three seperate things.  
connect = this node only  
flow = all nodes on this tab  
global = all nodes in this whole set of tabs
