Skip to content

Automated Lighting Levels

Northern Man edited this page Feb 20, 2021 · 2 revisions

I created this to have the lighting in my home office adjust automatically to the outside lighting conditions that occur during the day so I don't end up sitting in the dark. To provide the lighting levels to this flow I built a lux sensor using a BH1750 Illuminance sensor connected to a nodeMCU running Tasmota. It sends level updates every few seconds to node-red.

Based on the lights in my room and the outside lighting available, I have lights dim if the sensor detects over 37 lux, and brighten the lights if the sensor detects below 32 lux. ( This is in the Bright / Dim node ). When the lights need to be adjusted, they are either brightened or dimmed by 5, and it moves the slider in steps of 1 every 1.5 seconds. This way the changes are lighting level are mostly not noticed.

Contributed Nodes Used

  • homebridge-automation
  • homekit-bridged
  • simple-gate
[{"id":"df691456.3c0e3","type":"mqtt in","z":"8b416a1.08e4a98","name":"LX Sensor","topic":"tasmota-5042/tele/SENSOR","qos":"0","datatype":"json","broker":"c393578c.2b768","x":120,"y":180,"wires":[["bebd2d27.0a3fc","c3d087e3.cee308"]]},{"id":"31c89a.24954766","type":"hb-control","z":"8b416a1.08e4a98","name":"Dining Room Chandelier","Homebridge":"Leonard-Dev","Manufacturer":"Insteon","Service":"Lightbulb","device":"Leonard-DevAC:22:3D:E3:CE:30InsteonDining Room Chandelier00000043","conf":"aa4ff25c.416df8","outputs":0,"x":550,"y":480,"wires":[]},{"id":"4561a69b.319758","type":"hb-control","z":"8b416a1.08e4a98","name":"Dining Room Pot Lights","Homebridge":"Leonard-Dev","Manufacturer":"Insteon","Service":"Lightbulb","device":"Leonard-DevAC:22:3D:E3:CE:30InsteonDining Room Pot Lights00000043","conf":"aa4ff25c.416df8","outputs":0,"x":550,"y":540,"wires":[]},{"id":"81001141.d852c8","type":"switch","z":"8b416a1.08e4a98","name":"Bright or Dim","property":"payload.BH1750.Illuminance","propertyType":"msg","rules":[{"t":"gte","v":"37","vt":"num"},{"t":"lte","v":"32","vt":"num"},{"t":"nnull"}],"checkall":"false","repair":false,"outputs":3,"x":130,"y":360,"wires":[["a662a663.b72b4"],["1bdbb33d.11ef75"],[]]},{"id":"a662a663.b72b4","type":"hb-status","z":"8b416a1.08e4a98","name":"Dining Room Pot Lights","Homebridge":"Leonard-Dev","Manufacturer":"Insteon","Service":"Lightbulb","device":"Leonard-DevAC:22:3D:E3:CE:30InsteonDining Room Pot Lights00000043","conf":"aa4ff25c.416df8","x":350,"y":320,"wires":[["7b5b2cb6.8e5b04"]]},{"id":"9d8bf413.647cb","type":"debug","z":"8b416a1.08e4a98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":510,"y":600,"wires":[]},{"id":"1bdbb33d.11ef75","type":"hb-status","z":"8b416a1.08e4a98","name":"Dining Room Pot Lights","Homebridge":"Leonard-Dev","Manufacturer":"Insteon","Service":"Lightbulb","device":"Leonard-DevAC:22:3D:E3:CE:30InsteonDining Room Pot Lights00000043","conf":"aa4ff25c.416df8","x":350,"y":380,"wires":[["312cb964.290fbe"]]},{"id":"c3d087e3.cee308","type":"debug","z":"8b416a1.08e4a98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload.BH1750.Illuminance","targetType":"msg","x":700,"y":160,"wires":[]},{"id":"9276ae6f.13b908","type":"homekit-service","z":"8b416a1.08e4a98","isParent":true,"bridge":"cc234500.cbcc3","parentService":"","name":"Dinning Room Work","serviceName":"Switch","topic":"","filter":false,"manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","characteristicProperties":"{ \"On\": true, \"Brightness\": true }","x":160,"y":120,"wires":[["d96d3104.48dfd","c2b651c2.428cf8"]]},{"id":"bebd2d27.0a3fc","type":"gate","z":"8b416a1.08e4a98","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":true,"x":150,"y":260,"wires":[["81001141.d852c8"]]},{"id":"d96d3104.48dfd","type":"function","z":"8b416a1.08e4a98","name":"Control Gate","func":"if ( msg.payload.On) {\n    msg.topic = \"control\";\n    msg.payload = \"open\";\n} else {\n    msg.topic = \"control\";\n    msg.payload = \"close\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":120,"wires":[["bebd2d27.0a3fc"]]},{"id":"c2b651c2.428cf8","type":"switch","z":"8b416a1.08e4a98","name":"Turn Off Switch","property":"payload.On","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":380,"y":260,"wires":[["4699b9d2.0899a","72590daa.45127c"]]},{"id":"4699b9d2.0899a","type":"link out","z":"8b416a1.08e4a98","name":"Lights","links":["df6db559.5f377"],"x":1035,"y":380,"wires":[]},{"id":"df6db559.5f377","type":"link in","z":"8b416a1.08e4a98","name":"Lights","links":["4699b9d2.0899a"],"x":295,"y":540,"wires":[["31c89a.24954766","4561a69b.319758","9d8bf413.647cb"]]},{"id":"312cb964.290fbe","type":"function","z":"8b416a1.08e4a98","name":"Bright by 5","func":"var start = msg.payload.Brightness;\nvar brightness = start;\nvar delta = 5;\nif ( brightness < 100 ) {\nfor (; brightness <= (start + delta); brightness++) { \n    msg.payload = {};\n    msg.payload.Brightness = brightness;\n    msg.payload.On = true;\n    node.send(msg)\n}\n}\n// return msg;","outputs":1,"noerr":0,"x":570,"y":380,"wires":[["7bc7a5fa.22e49c"]]},{"id":"7b5b2cb6.8e5b04","type":"function","z":"8b416a1.08e4a98","name":"Dim by 5","func":"var start = msg.payload.Brightness;\nvar brightness = start;\nvar delta = -5;\nif ( brightness > 0 ) {\nfor (; brightness >= (start + delta); brightness--) { \n    msg.payload = {};\n    msg.payload.Brightness = brightness;\n    msg.payload.On = true;\n    node.send(msg)\n}}\n// return msg;","outputs":1,"noerr":0,"x":560,"y":320,"wires":[["7bc7a5fa.22e49c"]]},{"id":"7bc7a5fa.22e49c","type":"delay","z":"8b416a1.08e4a98","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1.5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":760,"y":380,"wires":[["ecda151.38866e8"]]},{"id":"72590daa.45127c","type":"template","z":"8b416a1.08e4a98","name":"Reset","field":"reset","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"","output":"str","x":630,"y":260,"wires":[["7bc7a5fa.22e49c"]]},{"id":"ecda151.38866e8","type":"function","z":"8b416a1.08e4a98","name":"Max/Min","func":"if (msg.payload.Brightness > 100)\n  msg.payload.Brightness = 100;\nif (msg.payload.Brightness <= 0)\n  { msg.payload.Brightness = 0;\n    msg.payload.On = false;\n  }\nreturn msg;","outputs":1,"noerr":0,"x":920,"y":380,"wires":[["4699b9d2.0899a"]]},{"id":"c393578c.2b768","type":"mqtt-broker","z":"","name":"Mqtt","broker":"mqtt.local","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"aa4ff25c.416df8","type":"hb-conf","z":"","username":"031-45-154"},{"id":"cc234500.cbcc3","type":"homekit-bridge","z":"","bridgeName":"Sheldon-NodeRed","pinCode":"031-45-154","port":"51831","allowInsecureRequest":true,"manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","customMdnsConfig":false,"mdnsMulticast":true,"mdnsInterface":"","mdnsPort":"","mdnsIp":"","mdnsTtl":"","mdnsLoopback":true,"mdnsReuseAddr":true}]