Scripts for scenes
m |
m |
||
(14 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
+ | [[Category:User Instructions]] | ||
+ | [[Category:How To]] | ||
+ | |||
+ | <br> | ||
+ | |||
== Scene that runs only if a security sensor hasn't been tripped in a set period of time == | == Scene that runs only if a security sensor hasn't been tripped in a set period of time == | ||
Line 10: | Line 15: | ||
local period = 15 | local period = 15 | ||
− | local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" | + | local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID |
− | local armed = luup.variable_get(SS_SID, "Armed", deviceNo) | + | local armed = luup.variable_get (SS_SID, "Armed", deviceNo) |
− | if armed == "1" then | + | if (armed == "1") then |
− | local lastTrip = luup.variable_get(SS_SID, "LastTrip", deviceNo) or os.time() | + | local lastTrip = luup.variable_get (SS_SID, "LastTrip", deviceNo) or os.time() |
− | lastTrip = tonumber(lastTrip) | + | lastTrip = tonumber (lastTrip) |
− | if (os.difftime(os.time(), lastTrip) / 60) >= period then | + | if ((os.difftime (os.time(), lastTrip) / 60) >= period) then |
return true | return true | ||
end | end | ||
Line 27: | Line 32: | ||
'''Note:''' The sensor must be armed for this to work, but this can be easily changed.<br> | '''Note:''' The sensor must be armed for this to work, but this can be easily changed.<br> | ||
− | <br> | + | <br> |
== Scene that runs only if a security sensor has been tripped for a set period of time == | == Scene that runs only if a security sensor has been tripped for a set period of time == | ||
Line 79: | Line 84: | ||
local HIGH_LEVEL = 40 -- the light level threshold for day | local HIGH_LEVEL = 40 -- the light level threshold for day | ||
local DEVICE_NO = 13 -- the light sensor device number | local DEVICE_NO = 13 -- the light sensor device number | ||
− | local LS_SID | + | local LS_SID = "urn:micasaverde-com:serviceId:LightSensor1" -- the LightSensor service ID |
local currentLevel = luup.variable_get (LS_SID, "CurrentLevel", DEVICE_NO) or 0 | local currentLevel = luup.variable_get (LS_SID, "CurrentLevel", DEVICE_NO) or 0 | ||
Line 106: | Line 111: | ||
end</source> | end</source> | ||
− | + | ||
+ | == Scene that runs only in a user set time interval == | ||
+ | |||
+ | Add this code in the Luup Code section: | ||
+ | |||
+ | <source lang="lua">local startTime = "22:30" | ||
+ | local endTime = "05:30" | ||
+ | |||
+ | local hour = tonumber( startTime:sub( startTime:find("%d+") ) ) | ||
+ | local minute = tonumber(startTime:sub(-2)) | ||
+ | |||
+ | if hour and minute then | ||
+ | startTime = hour * 100 + minute | ||
+ | else | ||
+ | luup.log("ERROR: invalid start time") | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | hour = tonumber( endTime:sub( endTime:find("%d+") ) ) | ||
+ | minute = tonumber(endTime:sub(-2)) | ||
+ | |||
+ | if hour and minute then | ||
+ | endTime = hour * 100 + minute | ||
+ | else | ||
+ | luup.log("ERROR: invalid end time") | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | local currentTime = os.date("*t") | ||
+ | currentTime = currentTime.hour * 100 + currentTime.min | ||
+ | |||
+ | luup.log("startTime = " .. startTime .. "; currentTime = " .. currentTime .. "; endTime = " .. endTime) | ||
+ | |||
+ | if startTime <= endTime then | ||
+ | -- Both the start time and the end time are in the same day: | ||
+ | -- if the current time is in the given interval, run the scene. | ||
+ | if startTime <= currentTime and currentTime <= endTime then | ||
+ | return true | ||
+ | end | ||
+ | else | ||
+ | -- The start time is before midnight, and the end time is after midnight: | ||
+ | -- if the current time is not outside the given interval, run the scene. | ||
+ | if not (endTime < currentTime and currentTime < startTime) then | ||
+ | return true | ||
+ | end | ||
+ | end | ||
+ | |||
+ | return false</source> | ||
+ | |||
+ | This code allows the scene to run only between 22:30 and 5:30. | ||
+ | |||
+ | |||
+ | == Scene that turns on the light when it detects movement and turns off the light when it doesn't detect movement for a while == | ||
+ | |||
+ | 1. Create a scene to turn on the light when a sensor is tripped. | ||
+ | |||
+ | 2. Add this code in the ''Luup code'' section: | ||
+ | |||
+ | <source lang="lua">local sensorDeviceNo = 16 -- Motion Sensor device number | ||
+ | local lightDeviceNo = 13 -- Light device number | ||
+ | local period = 10 -- Seconds | ||
+ | |||
+ | local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID | ||
+ | local SP_SID = "urn:upnp-org:serviceId:SwitchPower1" -- Switch Power Service ID | ||
+ | |||
+ | 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 <period> seconds. | ||
+ | end | ||
+ | end | ||
+ | |||
+ | luup.call_delay ("checkLastTrip", period) | ||
+ | |||
+ | return true</source> | ||
+ | |||
+ | |||
+ | == Scene that runs only if the light level is below a user set threshold == | ||
+ | |||
+ | 1. Create a scene to turn on the light when the sensor is tripped. | ||
+ | |||
+ | 2. Add this code in the ''Luup code'' section: | ||
+ | |||
+ | <source lang="lua">local LOW_LEVEL = 20 -- the light level threshold for night | ||
+ | local DEVICE_NO = 33 -- the light sensor device number | ||
+ | local LS_SID = "urn:micasaverde-com:serviceId:LightSensor1" -- the LightSensor service ID | ||
+ | |||
+ | local currentLevel = luup.variable_get (LS_SID, "CurrentLevel", DEVICE_NO) or 0 | ||
+ | currentLevel = tonumber(currentLevel) | ||
+ | |||
+ | if currentLevel <= LOW_LEVEL then | ||
+ | return true | ||
+ | else | ||
+ | return false | ||
+ | end</source> |
Latest revision as of 11:03, 14 May 2012
[edit] Scene that runs only if a security sensor hasn't been tripped in a set period of time
1. Create a new scene. In that scene:
2. Create a timer and set it to run every minute.
3. In the Luup Code section put the following code:
local deviceNo = 22 local period = 15 local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID local armed = luup.variable_get (SS_SID, "Armed", deviceNo) if (armed == "1") then local lastTrip = luup.variable_get (SS_SID, "LastTrip", deviceNo) or os.time() lastTrip = tonumber (lastTrip) if ((os.difftime (os.time(), lastTrip) / 60) >= period) then return true end end return false
deviceNo is the sensor's device number, which you can get by going into its Toolbox, in the Advanced tab.
period is the time (in minutes) the sensor hasn't been tripped before running the scene.
Note: The sensor must be armed for this to work, but this can be easily changed.
[edit] Scene that runs only if a security sensor has been tripped for a set period of time
1. Create a new scene. In that scene:
2. Create a timer and set it to run every minute.
3. In the Luup Code section put the following code:
local deviceNo = 22 local period = 15 local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" local armed = luup.variable_get(SS_SID, "Armed", deviceNo) or "0" if armed == "1" then local tripped = luup.variable_get(SS_SID, "Tripped", deviceNo) or "0" if tripped == "0" then luup.variable_set(SS_SID, "TripPeriod", "0", deviceNo) else local tripPeriod = luup.variable_get(SS_SID, "TripPeriod", deviceNo) or 0 tripPeriod = tonumber(tripPeriod) tripPeriod = tripPeriod + 1 luup.variable_set(SS_SID, "TripPeriod", tripPeriod, deviceNo) if tripPeriod == period then return true end end end return false
deviceNo is the sensor's device number, which you can get by going into its Toolbox, in the Advanced tab.
period is the time (in minutes) the sensor has been tripped before running the scene.
Note: The sensor must be armed for this to work, but this can be easily changed
[edit] Scene that runs once at dawn and once at dusk
1. Create a new scene. In that scene:
2. Create a timer and set it to run every hour (set a different time interval depending on the desired accuracy).
3. In the Luup Code section put the following code:
local LOW_LEVEL = 20 -- the light level threshold for night local HIGH_LEVEL = 40 -- the light level threshold for day local DEVICE_NO = 13 -- the light sensor device number local LS_SID = "urn:micasaverde-com:serviceId:LightSensor1" -- the LightSensor service ID local currentLevel = luup.variable_get (LS_SID, "CurrentLevel", DEVICE_NO) or 0 currentLevel = tonumber(currentLevel) if currentLevel <= LOW_LEVEL then local timeOfDay = luup.variable_get (LS_SID, "TimeOfDay", DEVICE_NO) or "Day" if timeOfDay == "Night" then -- It's night, so the scene has run once. return false else -- It's sunset, run the scene. luup.variable_set (LS_SID, "TimeOfDay", "Night", DEVICE_NO) return true end elseif currentLevel >= HIGH_LEVEL then local timeOfDay = luup.variable_get (LS_SID, "TimeOfDay", DEVICE_NO) or "Night" if timeOfDay == "Day" then -- It's day, so the scene has run once. return false else -- It's morning, run the scene. luup.variable_set (LS_SID, "TimeOfDay", "Day", DEVICE_NO) return true end end
[edit] Scene that runs only in a user set time interval
Add this code in the Luup Code section:
local startTime = "22:30" local endTime = "05:30" local hour = tonumber( startTime:sub( startTime:find("%d+") ) ) local minute = tonumber(startTime:sub(-2)) if hour and minute then startTime = hour * 100 + minute else luup.log("ERROR: invalid start time") return false end hour = tonumber( endTime:sub( endTime:find("%d+") ) ) minute = tonumber(endTime:sub(-2)) if hour and minute then endTime = hour * 100 + minute else luup.log("ERROR: invalid end time") return false end local currentTime = os.date("*t") currentTime = currentTime.hour * 100 + currentTime.min luup.log("startTime = " .. startTime .. "; currentTime = " .. currentTime .. "; endTime = " .. endTime) if startTime <= endTime then -- Both the start time and the end time are in the same day: -- if the current time is in the given interval, run the scene. if startTime <= currentTime and currentTime <= endTime then return true end else -- The start time is before midnight, and the end time is after midnight: -- if the current time is not outside the given interval, run the scene. if not (endTime < currentTime and currentTime < startTime) then return true end end return false
This code allows the scene to run only between 22:30 and 5:30.
[edit] Scene that turns on the light when it detects movement and turns off the light when it doesn't detect movement for a while
1. Create a scene to turn on the light when a sensor is tripped.
2. Add this code in the Luup code section:
local sensorDeviceNo = 16 -- Motion Sensor device number local lightDeviceNo = 13 -- Light device number local period = 10 -- Seconds local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID local SP_SID = "urn:upnp-org:serviceId:SwitchPower1" -- Switch Power Service ID 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 <period> seconds. end end luup.call_delay ("checkLastTrip", period) return true
[edit] Scene that runs only if the light level is below a user set threshold
1. Create a scene to turn on the light when the sensor is tripped.
2. Add this code in the Luup code section:
local LOW_LEVEL = 20 -- the light level threshold for night local DEVICE_NO = 33 -- the light sensor device number local LS_SID = "urn:micasaverde-com:serviceId:LightSensor1" -- the LightSensor service ID local currentLevel = luup.variable_get (LS_SID, "CurrentLevel", DEVICE_NO) or 0 currentLevel = tonumber(currentLevel) if currentLevel <= LOW_LEVEL then return true else return false end