I have a photoresistor light sensor hooked up to an Arduino Uno to check the light levels in the living room. The sensor is in the living room attached to the Raspberry Pi running OpenHAB. OpenHAB pulls in the light level and whether it is daytime according to Astro. It switches on the lights if the light level falls below a set point and Astro says it is daytime. The idea is to turn on the lights if it gets dark during a thunderstorm.
This works fine turning on the lights when it gets dark. The problem is that I don’t know how to get the lights to switch off if it is not dark out anymore. I had reconfigured the rules set up to pull in light intensity from Weather Underground instead of the light sensor. This isn’t ideal, but the weather station is 3-4 blocks away so it does work okay. Another problem is that the free API is going away soon, so I need to migrate it away from that.
What can I do to check when the lights should switch off? Is there another data source to poll? It will be a little difficult to move the sensor to another place since the Arduino doesn’t have networking.
Here are the configuration files:
lightsensor.rules
rule "Light Sensor String Conversion"
when
Item light changed
then
LRLightLevel.sendCommand(Float::parseFloat(String::format("%s",light.state).replace(' ','')))
logInfo("rule", "light State is " + light.state)
logInfo("rule", "LRLightLevel State is " + LRLightLevel.state)
end
rule "LR Lights Sensor"
when
Item LRLightLevel changed
then
if (lightsensorenable == ON) { //check if light sensor switch item is enabled
if (Daylight.state == ON && LRLightLevel.state <= 1.0) { //only run if it is daytime
logInfo("events", "Light Sensor changed to LOWER than threshold")
logInfo("events", "Turning on Livingroom Lights")
sendCommand(LRLamp, ON)
postUpdate(LRLightSensor, ON) //set a flag that the lights were turned on by sensor
}
}
if (LRLightSensor.state == ON && LRLightLevel.state > 25.0) {
if (Daylight.state == ON) { //only run if it is daytime
logInfo("events", "Light Sensor changed to HIGHER than threshold")
logInfo("events", "Turning off Livingroom Lights")
sendCommand(LRLamp, OFF)
postUpdate(LRLightSensor, OFF) //clear the flag that the lights were turned on by sensor
}
}
end
dayflags.rules
rule "Set Daylight"
when
Channel 'astro:sun:home:rise#event' triggered START
then
logInfo(events, "Day flag set")
postUpdate(Daylight, ON)
end
rule "Set night"
when
Channel 'astro:sun:home:set#start' triggered START
then
logInfo(events, "Night flag set")
postUpdate(Daylight, OFF)
end
lightsensor.items
String light "[%s]" {channel="exec:command:poll_light:output"}
Switch LRLightSensor
Number LRLightLevel "LR Light level [%.1f]" (lightsensors)
Number LRLight_Levels "Light Sensor History"
Group lightsensors "Light Level"
Switch lightsensorenable
Switch Daylight "Daylight"
Light Sensor Schematic