Detection and lighting logic

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
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

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

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

deleted this post

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

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

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

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.

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

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

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.

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