Are there any cheap light sensors out there? I would like to detect whether a light is on in a closet for example so I can manually turn it off to save energy. I use hue motion sensors to do this for automated lights but they are pretty pricey for this purpose. Ideally I’d be looking for something in the $10 range. I don’t need high precision or accuracy, just a basic ability to determine if a light is on or not based on light level.
Might it not be easier to install a smart light bulb?
Maybe a DIY-Sensor will do the job?
All you need is a Wemos D1 mini with a luminosity sensor shield or an NodeMCU with a cheap luminosity sensor like the “BH 1750”.
This will comply your $10 budget and it’s fun to make!
Thanks for the help. I’ll probably go the DIY route suggested.
A smart bulb could work in some places in my situation but for the most part these are little used areas like closets where I was hoping to avoid the expense of even smart bulbs, hence my $10 price point. Smart bulbs where I live are closer to $20 and often there are multiple in a room. My bathrooms for example have 6 or 7 bulbs. I can sense if any are on with 1 sensor but it would take $100 in smart bulbs to use that approach. In the living areas where I want to also be able to control the lighting I have gone with Leviton switches and dimmers and this is working well.
I built an inexpensive light sensor using the BH1750 variant sold as GY-302 and a Raspberry Pi Zero W from Adafruit Industries. I wrapped some web.py boilerplate code around the very easily found BH1750 Python code to make a web server accessible as a command-line sensor.
configuration.yaml
sensor:
#
# Ambient Light Sensor
#
- platform: command_line
name: Ambient Light 1
scan_interval: 10
unit_of_measurement: ‘Lux’
command: “curl http://192.168.1.nnn:8080/”
command_timeout: 5
value_template: ‘{{ value }}’
#
Python code:
#!/usr/bin/python
import smbus
import time
import web
#
# Define constants from the
# BH1750 light sensor datasheet
#
DEVICE = 0x23 # Default device I2C address
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Power on
RESET = 0x07 # Reset data register value
CONTINUOUS_HIGH_RES_MODE_1 = 0x10 # Start at 1 Lux resolution. 120 msec
CONTINUOUS_HIGH_RES_MODE_2 = 0x11 # Start at .5 Lux resolution. 120 msec
ONE_TIME_HIGH_RES_MODE_1 = 0x20 # Device powers down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21 # Device powers down after measurement.
ONE_TIME_LOW_RES_MODE = 0X23 # Device powers down after measurement.
#
# Read a 16-bit word from the I2C light sensor
# Swap the bytes to obtain a numeric value
# Return that value as an integer
#
def readLight(addr=DEVICE):
data = bus.read_i2c_block_data(addr, CONTINUOUS_HIGH_RES_MODE_1)
return int((data[1] + (256 * data[0])) / 1.2)
#
# instantiate the SMBus object for I2C reading the Light Sensor
#
bus = smbus.SMBus(1) # Rev 2 Pi and onward uses bus #1
#
# instantiate the web.py app object
#
URLS = (
'/(.*)', 'light'
)
app = web.application(URLS, globals())
class light:
def GET(self, name):
return str(readLight())
## return '{{ ' + str(readLight()) + ' }}'
if __name__ == "__main__":
app.run()
Linux daemon init script
#!/bin/sh
### BEGIN INIT INFO
# Provides: lightSensor
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Ambient Light Sensor
# Description: This daemon/service provides ambient light levels in Lux as an integer value, using an I2C bus BH1750 sensor.
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/etc/lightSensor
DAEMON=$DIR/lightSensor.py
DAEMON_NAME=lightSensor
# Add any command line options for your daemon here
DAEMON_OPTS=""
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Enjoy!!
Not sure if this is still relevant for you, but Wyze has smart bulbs for 7.99 (no bridge required), motion sensors for 5.99 (bridge required), and contact sensors 19.99 per 4 count (bridge required). They are incredibly cost effective - worth checking out in any case.