Scene with HSM100 where motion and light level turn light on and off after time

Does anyone already have this lua code? I have used the examples here:

http://wiki.micasaverde.com/index.php/Scripts_for_scenes

and basically I am looking to combine the last two, but it is proving to be a little too hard for me. So I thought I would see if anyone as already done this…

Basically I have a scene where a light will come on if motion is detected and will turn off after a period of time (example 5) which is good except it does it when you don’t need the light (i.e during the day). So I would like to combine it with example 6 where the light will only turn on when the light level is below a certain threshold.

I would think this is pretty common so I thought I would ask the forum before I spend too much more time trying to figure this out…

Thanks Jason

I just started working on something similar.

Your link to http://wiki.micasaverde.com/index.php/Scripts_for_scenes is a great resource. I will have to roll it into my lighting logic.

I’m pretty new to Vera/LUUP scripting so I’m not qualified to directly answer your question. However to the issue of control based upon existing light levels, I thought I would share what I’ve created thus far. Like you, I want to avoid turning on lights based upon current lighting levels. In my case, I found it worked well to use the current outdoor ambient light level to decide when to permit indoor lights to activate.

Here’s how my solution looks. It contains three “modules”.

They are:

  1. “Add-In” module Variable Container provided by Geckotech’s. This provides a container to share ambient light levels to all other scense.
  2. Scene to capture and save valid ambient light level to the Variable container
  3. Scene (one per zone) to control lights. It uses the light levels saved in the Variable container to decide if lights should be permited.

Geckotech’s add-in is very handy not just for control, but trouble shooting as well. Besides light levels, I also use it to display temperatures from Google’s weather add-in.

I have a “HMS100 3-in-one sensor” mounted in the carport, protected from weather but exposed to outdoor light levels. I converted this device to run from AC power so as to make it easy to tap into it’s values at any time.

From Item #2 above, I created a scene to save valid light levels to the Variable container. It runs every 15 minutes and does NOT record value if car port light is currently on.

Here is the logic for this scene:

Step-1: Set the SCHEDULE to run every 15 minutes.
LUUP Code:

Step-1: Check if the Car Port light is currently on. If it’s on, the current light level is not correct, therefore I exit the scene.
Step-2: Cause the value of the HMS100 sensor to “refresh”. I dont want stale values.
luup.call_action(“urn:micasaverde-com:serviceId:HaDevice1”, “Poll”, {}, dev_carportsensor)

I have found it necessary to wait a bit for the values to refresh. Therefore I choose an arbitrary delay of 10 seconds before proceeding.
After the delay I call a function to grab the current light levels and store them in the Variable container, variable #1
NOTE: unrelated to this topic, you can see where I grab the Google Current and LOW temps for placement in the variable container’s slot 2 and 3.

Here is the code:

–~ This code resides in GetSunshineValue scene.
–~ ==========================================
–~ Version 1.06 1/21/2012
–~ Sequence of operation:
–~ Purpose:
–~ Record and hold value of outside light level while Garage light is OFF (to not spoil reading)
–~ If lights are on, preserve existing value.

local cur_sunshine
local dev_carport_lgt = 91
local dev_hallsconse = 92
local dev_varcontainer1 = 131
local dev_carport_lightlevel = 129
local dev_carportsensor = 127
local f_lightison
local googletemp

–~ This function refreshes Car Port Light & Motion Sensor
function getlight()
cur_sunshine =luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”,“CurrentLevel”,dev_carport_lightlevel)
cur_sunshine = tonumber(cur_sunshine)
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable1”,cur_sunshine,dev_varcontainer1)
luup.variable_set(“HallSconce”, “SunshineVal”,cur_sunshine, dev_hallsconse)
end

– Get carport light status
f_lightison = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, dev_carport_lgt)
f_lightison = tonumber(f_lightison)

– Get Google temp, put into Var3 as OSA Temp
local googletemp = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”, 63)
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable2”,googletemp,dev_varcontainer1) – Set Var 2 OSA temp
local googletemp = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”, 64)
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable3”,googletemp,dev_varcontainer1) – Set Var 2 Low Forecast

if (f_lightison == 1) then – If light currently on, do nothing as the Lgt Level value is tarnished by the Garge Lights
return false
else
– Poll light sensor, wait then update Variable1 with the new information
luup.call_action(“urn:micasaverde-com:serviceId:HaDevice1”, “Poll”, {}, dev_carportsensor)
luup.call_delay( ‘getlight’, 10)
return false
end

The above scene assures my variable container provides a current ambient light level for use in other scenes. The zone scene code looks like this.

–~ Car Port ON Scene Event Version 1.2
local cur_sunshine
local dev_varcontainer1 = 131

cur_sunshine = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”,“Variable1”,dev_varcontainer1)
cur_sunshine = tonumber (cur_sunshine)

if (cur_sunshine > 30) then
return false
else
return true
end

This is the simple version. Totally unrelated to this topic, I use the PROWL service to send messages to my iPad every time a light event happens. I stripped the SEND MESSAGE code from this scene as provided to the post to avoid polluting the post with unrelated topics. However if you have an iPad you will find sending messages to Prowl EXTREMELY handy.

My apologies for not getting the code inside boxes like I’ve seen others do. My skills using this forum are weak at best. If anyone cares to tell me how?..

Thanks for the reply, I actually got this to work by following the example here:

http://wiki.micasaverde.com/index.php/Scripts_for_scenes

You set up the scene like example 5 and in the luup code tab you insert this code:

[code]local sensorDeviceNo = 25 – Motion Sensor device number
local lightDeviceNo = 43 – Light device number
local period = 10 – Seconds
local LOW_LEVEL = 20 – the light level threshold for night
local DEVICE_NO = 26 – the light sensor device number

local LS_SID = “urn:micasaverde-com:serviceId:LightSensor1” – the LightSensor service ID

local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID

local SP_SID = “urn:upnp-org:serviceId:SwitchPower1” – Switch Power Service ID

local currentLevel = luup.variable_get (LS_SID, “CurrentLevel”, DEVICE_NO) or 0
currentLevel = tonumber(currentLevel)

if currentLevel <= LOW_LEVEL then
function checkLastTrip()
local lastTrip = luup.variable_get (SS_SID, “LastTrip”, sensorDeviceNo) or os.time()
if (os.difftime (os.time(), tonumber (lastTrip)) >= period) then
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 0}, lightDeviceNo) – Turn off the light.
else
luup.call_delay (“checkLastTrip”, period) – Check when the sensor was last tripped every seconds.
end
end

luup.call_delay (“checkLastTrip”, period)

return true

else
return false
end[/code]

What it does if the detector senses motion and the light level is below the threshold it will turn on the light. The light will stay on until a defined period of time where this no motion.

Works pretty well.

Thanks Jason