Flow for automatic light control using movement, luminance sensors and scheduler

3,053 views
Skip to first unread message

lingvo

unread,
Aug 17, 2016, 3:20:11 PM8/17/16
to Node-RED
Hi there,

In this thread I want to post my progress on designing some home automation rule using Node Red. Perhaps this will be interesting for someone. Do not hesitate to post suggestions, comments or questions.

Task:
I want to make automatic light on, off and dimming in my hall, based on movement detection sensor, luminance and schedule. The light should go on after movement is detected, and switch off after some delay.

Devices:
All devices are connected via MQTT protocol. Currently I have following devices:
 
- Light - LED based Light, controlled by MQTT topic. Payload: 0-100. 0 means no light, 100 means full intensity.
- Movement Sensor - generates topics with payload: ON - movement detected. After couple of seconds it sends payload OFF if there is no movement anymore.
- Light luminance sensor - generates topics with payload 0...1000. Units are Lux. 0 means darkness. 1000- is daylight. The report is sent every minute.

I have following requirements so far:
- When it is dark (luminance less than 100 Lux) the light should go on if movement is detected.
- The light should go off automatically after 20 seconds.
- If there is new movement during light is on, the 20 second timer should start over.
- If it is later than 10pm, the light should go on only with 10% intensity.

Julian Knight

unread,
Aug 18, 2016, 5:26:16 AM8/18/16
to Node-RED
Hi Lingvo,

Sounds like a fairly standard kind of requirement for security type lighting and something that a lot of people using NR would be interested in seeing how you did it.

I would encourage you to post the results to flows.nodered.org as a flow if it is not too big/complex or maybe document in a full github repo if a more complex explanation is required (it is somewhat easier to put images into a repo than the gist that a flow uses).

Also, I'm sure that there are people in the group who will chip in with ideas and help if you get stuck somewhere.

lingvo

unread,
Aug 18, 2016, 10:26:43 AM8/18/16
to Node-RED
Actually I'm almost finished - this was easier as I expected. I will check how to publish it in better way.
Meanwhile does anyone know where I could play with Node-red editor for free if I don't have access to my server? I found node-red-editor-demo.mybluemix.net but it's only useful to view the flows - for deployment you need login.

lingvo

unread,
Aug 18, 2016, 10:31:18 AM8/18/16
to Node-RED
Another thing - is it possible to watch context variables?

Julian Knight

unread,
Aug 18, 2016, 11:32:39 AM8/18/16
to Node-RED
Well the easiest way is to install it on your laptop. It is pretty easy as it only needs node.js. In fact if you don't want the faff of running node.js from a command line or setting up a service for it, you can even run it from Electron https://github.com/natcl/electron-node-red

There are a couple of web browser based versions but they don't have all the nodes available - https://iceddev.com/blog/introducing-pagenodes/ 

I think there are some others too but I'm afraid I can't put my fingers on the references.

Personally I have copies of my flows on my Windows laptop though you do get errors of course if you are expecting certain hardware to be present.

Julian Knight

unread,
Aug 18, 2016, 11:36:34 AM8/18/16
to Node-RED
Several ways, none necessarily ideal.

You could create a simple flow with an inject running every few seconds and outputing to Dashboard.
You could move all of your context updates to be message based via a subflow that also updated a Dashboard or maybe MQTT

If a variable is only written in one place, you could simply wire a debug node to the output of the node you are using to update.

lingvo

unread,
Aug 18, 2016, 2:32:39 PM8/18/16
to Node-RED
So the first version of flow looks like on the picture below:

In first control path the Hall Light Sensor Node subscribes to MQTT topic from Light sensor. Light sensor selector forwards light intensity message based on reported Light level. If it is below 50 Lux, first output is activated and Enable Light Block writes Light Brightness of 1 to flow context. Otherwise Light Brightness set to zero.
 
In second control path Hall Motion Sensor Node subscribes to MQTT topic from motion sensor. As I wrote at the beginning motion sensor sends messages with OPEN and CLOSED Payload. Movement Detected? Node forwards only OPEN Messages to it's output. Light Brightness Adjustment changes message payload to current Light Brightness from flow context. Then finally this message sent to Hall Light Dimmer node, which publishes it to MQTT. As you see light will be switched on only if the current light level is low enough.
To turn off the light after some delay Switch off Delay Trigger node is used. It is reset by OPEN Message, and starts delay after receiving any other message from motion sensor(this will be CLOSED message obviously).  After time is elapsed it sends message with Light Brightness zero to Hall Light Dimmer.node.

JSON Code is below
[{"id":"76384a52.7084c4","type":"mqtt-broker","z":"aca73478.c3fce8","broker":"localhost","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"da21213a.d1c418","type":"mqtt in","z":"aca73478.c3fce8","name":"Hall Motion Sensor","topic":"/myhome/state/Hall_motion","broker":"76384a52.7084c4","x":121.44998168945312,"y":299.73333740234375,"wires":[["c1e6aaab.f19138","9701b7d3.99416"]]},{"id":"2b0eb06f.d7e8d8","type":"mqtt out","z":"aca73478.c3fce8","name":"Hall Light Dimmer ","topic":"/myhome/command/Light_Hall/state","qos":"0","retain":"true","broker":"76384a52.7084c4","x":1031.4500732421875,"y":370.4166564941406,"wires":[]},{"id":"c1e6aaab.f19138","type":"switch","z":"aca73478.c3fce8","name":"Movement detected?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OPEN","vt":"str"}],"checkall":"false","outputs":1,"x":415.4500427246094,"y":291.9666748046875,"wires":[["dd3b2fb8.432fc8"]]},{"id":"dd3b2fb8.432fc8","type":"change","z":"aca73478.c3fce8","name":"Light Brightness Adjustment","rules":[{"t":"set","p":"payload","pt":"msg","to":"Light_brightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":714.449951171875,"y":292.1333312988281,"wires":[["2b0eb06f.d7e8d8"]]},{"id":"9701b7d3.99416","type":"trigger","z":"aca73478.c3fce8","op1":"5","op2":"0","op1type":"nul","op2type":"val","duration":"8","extend":false,"units":"s","reset":"OPEN","name":"Switch off delay","x":502.4500427246094,"y":447.9666442871094,"wires":[["2b0eb06f.d7e8d8"]]},{"id":"e7886307.9cdb98","type":"mqtt in","z":"aca73478.c3fce8","name":"Hall Light Sensor","topic":"/myhome/state/Lumin_Hall","broker":"76384a52.7084c4","x":111.11666870117188,"y":81.11660766601562,"wires":[["6c25675.eea5698"]]},{"id":"6a32fd15.9e48e4","type":"change","z":"aca73478.c3fce8","name":"Enable Light","rules":[{"t":"set","p":"Light_brightness","pt":"flow","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":790.4500732421875,"y":45.599945068359375,"wires":[[]]},{"id":"6c25675.eea5698","type":"switch","z":"aca73478.c3fce8","name":"Light Threshold Selector","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"50","vt":"num"},{"t":"else"}],"checkall":"false","outputs":2,"x":419.4499816894531,"y":81.83328247070312,"wires":[["6a32fd15.9e48e4"],["22e3eb21.26615c"]]},{"id":"22e3eb21.26615c","type":"change","z":"aca73478.c3fce8","name":"Disable Light","rules":[{"t":"set","p":"Light_brightness","pt":"flow","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":792.11669921875,"y":130.11660766601562,"wires":[[]]}]

Does it look reasonable? As the next step I would like to add light Brightness control based on current time - e.g. before 10pm it should be brighter and after just minimum brightness.
 

Auto Generated Inline Image 1

Julian Knight

unread,
Aug 19, 2016, 3:48:58 AM8/19/16
to Node-RED
Hi, that looks good. You could, of course, write new MQTT messages instead of using flow variables. Not that much difference except that it would be easier to monitor/debug and easier to use from other flows or even other instances of Node-Red or other services. But either way gets the job done.

Once you've added the last step, I think this would make an excellent example for the flows site.


On Thursday, 18 August 2016 19:32:39 UTC+1, lingvo wrote:
So the first version of flow looks like on the picture below:
...

lingvo

unread,
Aug 20, 2016, 4:14:33 AM8/20/16
to Node-RED
Hi Julian, sorry, but I found no way how to interract if you have two asynchronius messages, other than yousing flow vars. Perhaps can you give an example?

Julian Knight

unread,
Aug 20, 2016, 9:41:45 AM8/20/16
to Node-RED
Just ignore that bit, I was thinking of something else, sorry.

lingvo

unread,
Aug 20, 2016, 12:32:44 PM8/20/16
to Node-RED
Here is the final flow

I changed a logic for light enabling to make it easier to understand.
In first control path the Hall Light Sensor Node subscribes to MQTT topic from Ambient Light sensor. Light Threshold selector forwards light intensity message based on reported ambient light level. If it is below 20 Lux, first output is activated and Enable Light Block writes Yes to Light_enabled context variable. Otherwise Light_enabled variable is set to No.

In second control path Light Brightness is set. For this I have two inject nodes - one is activated at 9:00 PM and it has a payload of 1 (minimum brightness) and another one is activated at 6:00 AM and it has payload of 50 - maximum brightness. The values are written to flow variable Light_brightness by Light Brightness Adjustment node.
 
In third control path Hall Motion Sensor Node subscribes to MQTT topic from motion sensor. As I wrote in first message motion sensor sends messages with OPEN and CLOSED Payload. Movement Detected? Node forwards only OPEN Messages to it's output. Light Enabled? node checks if Light_enabled variable is set to Yes, and if it the case, forwards the message. Light Brightness Adjustment changes message payload to current Light Brightness from flow context Light_brightness variable. Then finally this message sent to Hall Light Dimmer node, which publishes it to MQTT topic. As you see light will be switched on only if the current light level is low enough.
To turn off the light after some delay Switch off Delay Trigger node is used. It is reset by OPEN Message, and starts delay after receiving any other message from motion sensor(this will be CLOSED message obviously).  After time is elapsed it sends message with zero light brightness to Hall Light Dimmer node.

JSON Code is below
[{"id":"76384a52.7084c4","type":"mqtt-broker","z":"aca73478.c3fce8","broker":"localhost","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"da21213a.d1c418","type":"mqtt in","z":"aca73478.c3fce8","name":"Hall Motion Sensor","topic":"/myhome/state/Hall_motion","broker":"76384a52.7084c4","x":121.44998168945312,"y":299.73333740234375,"wires":[["c1e6aaab.f19138","9701b7d3.99416"]]},{"id":"2b0eb06f.d7e8d8","type":"mqtt out","z":"aca73478.c3fce8","name":"Hall Light Dimmer ","topic":"/myhome/command/Light_Hall/state","qos":"0","retain":"true","broker":"76384a52.7084c4","x":1116.4500732421875,"y":350.4166564941406,"wires":[]},{"id":"c1e6aaab.f19138","type":"switch","z":"aca73478.c3fce8","name":"Movement detected?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OPEN","vt":"str"}],"checkall":"false","outputs":1,"x":380.4500427246094,"y":277.9666748046875,"wires":[["a7f3a3f2.b51f38"]]},{"id":"9701b7d3.99416","type":"trigger","z":"aca73478.c3fce8","op1":"5","op2":"0","op1type":"nul","op2type":"val","duration":"8","extend":false,"units":"s","reset":"OPEN","name":"Switch off delay","x":502.4500427246094,"y":447.9666442871094,"wires":[["2b0eb06f.d7e8d8"]]},{"id":"e7886307.9cdb98","type":"mqtt in","z":"aca73478.c3fce8","name":"Hall Light Sensor","topic":"/myhome/state/Lumin_Hall","broker":"76384a52.7084c4","x":111.11666870117188,"y":81.11660766601562,"wires":[["ba0ba53c.012d6"]]},{"id":"1e9a8082.fb1d8f","type":"inject","z":"aca73478.c3fce8","name":"Reduce Brightness at 21:00","topic":"Night Brightness","payload":"1","payloadType":"str","repeat":"","crontab":"00 21 * * *","once":false,"x":165.45004272460938,"y":203.43331909179688,"wires":[["9455415e.123a28"]]},{"id":"160d3974.89f507","type":"inject","z":"aca73478.c3fce8","name":"Normal Brightness at 6:00","topic":"Night Brightness","payload":"50","payloadType":"num","repeat":"","crontab":"00 6 * * *","once":false,"x":156.11666870117188,"y":162.11666870117188,"wires":[["9455415e.123a28"]]},{"id":"ba0ba53c.012d6","type":"switch","z":"aca73478.c3fce8","name":"Light Threshold Selector","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"20","vt":"num"},{"t":"else"}],"checkall":"false","outputs":2,"x":465.11663818359375,"y":81.11666870117188,"wires":[["6c4b46e6.b31dd8"],["6af8fd01.69823c"]]},{"id":"6c4b46e6.b31dd8","type":"change","z":"aca73478.c3fce8","name":"Enable Light","rules":[{"t":"set","p":"Light_enabled","pt":"flow","to":"Yes","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":769.7833251953125,"y":44.399993896484375,"wires":[[]]},{"id":"6af8fd01.69823c","type":"change","z":"aca73478.c3fce8","name":"Disable Light","rules":[{"t":"set","p":"Light_enabled","pt":"flow","to":"No","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":772.449951171875,"y":112.91665649414062,"wires":[[]]},{"id":"9455415e.123a28","type":"change","z":"aca73478.c3fce8","name":"Light Brightness Adjustment","rules":[{"t":"set","p":"Light_brightness","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":514.11669921875,"y":179.11666870117188,"wires":[[]]},{"id":"a7f3a3f2.b51f38","type":"switch","z":"aca73478.c3fce8","name":"Light Enabled?","property":"Light_enabled","propertyType":"flow","rules":[{"t":"eq","v":"Yes","vt":"str"}],"checkall":"true","outputs":1,"x":611.1168212890625,"y":277.1166687011719,"wires":[["c474bc9b.e9ff88"]]},{"id":"c474bc9b.e9ff88","type":"change","z":"aca73478.c3fce8","name":"Light Brightness Adjustment","rules":[{"t":"set","p":"payload","pt":"msg","to":"Light_brightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":851.61669921875,"y":276.7499694824219,"wires":[["2b0eb06f.d7e8d8"]]}]






Auto Generated Inline Image 1

lingvo

unread,
Aug 22, 2016, 7:04:53 AM8/22/16
to Node-RED
Reply all
Reply to author
Forward
0 new messages