How to switch lights based on lux?


I have a couple of Lux sensors throughout the house and have Hue lights everyone. I’m now building a Node-Red flow to turn the lights on when it’s dark enough. But i have a problem, as soon as i turn on the lights, the LUX value in the room goes up, and the flow turns it back off on the next run.

How do you handle this in flows?

So how do you want it to work?
The lights go on and only you can turn them off? Or time based or when will they turn off again?

I typically only use my light sensors for outdoor lights. What I do is I have an event-states node that reads the light level outside that is exposed to HA:

This gets turned on in the evening and off again during the morning (“Turn on/off outdoor light level” nodes):

The trick to using light levels in your flows is a bit of trial and error to find that sweet spot where your lights don’t affect the actual lux level. So, for my lights, I don’t turn them on very bright at night plus I have my sensor placed in a slightly dark area. This way, I don’t have really wild swings in my light levels even if I turn my lights up to 100%.

For indoor uses, to be honest, because my wife and I are largely creatures of habit and I have a motion sensor in every room, I really don’t use light levels to trigger lights. They either turn on or off by motion sensor, contact sensor, or time of day. The one room where that is an exception is our bedroom because it gets the most light during the day (and stays dark during the summer to keep the house cool). So, only in that one room do I only check the light level to avoid turning on the motion based lights when the light level is high.

I have this problem as well.
Motion/light sensors in each room.
Using motion even to trigger flow.
Current state node to detect lux level in the room and if dark pass flow onto Trigger node.
Trigger node then passes payload of “on” and then “off” after X seconds to a call service node to switch lights on then off. Call service node calls lights.{{payload}}.
Trigger node is set to extend delay if new message arrives.
But new message never arrives because the current state node detects a high lux level.

Trying to store the lux level as a flow variable at beginning of flow and use an if statement and counter to only store the lux level once so that multiple motion events don’t detect the increase in lux after lights are turned on. But not sure how to do it.

Function node:

var lux             = msg.payload
var counter         = flow.get('counter') || 0

counter += 1

if(counter == 1)
  if(lux > 25)
      msg.payload = "off"
      msg.counter = counter
      return [msg]
msg.payload = "on"
msg.counter = counter
return [msg]

@rcruikshank Does your motion sensor send both on and off states? if so, you could do this, I believe it will work as you want.

[{"id":"bc2a0ed5.f96d","type":"server-state-changed","z":"937ef78.ec6b408","name":"Upstairs barthroom Motion off for X minutes?","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"2","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":210,"y":240,"wires":[["8782e443.223f18"],["d0d53d9b.15d17"]]},{"id":"8782e443.223f18","type":"api-call-service","z":"937ef78.ec6b408","name":"Light switch OFF","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":550,"y":200,"wires":[[]]},{"id":"d0d53d9b.15d17","type":"api-current-state","z":"937ef78.ec6b408","name":"Lux <= 25?","server":"9405c3fe.d0a6c","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":260,"wires":[["a47c7db8.62d72"]]},{"id":"a47c7db8.62d72","type":"api-call-service","z":"937ef78.ec6b408","name":"Light switch ON","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":700,"y":260,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

More compact

Do you want your lights to turn off when it’s clear enough too? Could you share your flow?

@obaldius thanks for your reply.
I’ll give it a go.