@oTi@ and @big517
Here’s the better version, which I think is the right way to do this 8)
@big517:
Please copy the updated version I just posted - it doesn’t involve counters and all that weird logic. This is the better version and should do what you were expecting. Please do let me know if this works well.
-------------------------------------------------------------------------
-- CODE FROM MYSTICJAY
-- http://forum.micasaverde.com/index.php/topic,8283.0.html
-- put your devices here--MAKE SURE the array_devices and
-- array_device_type have same # of elements
-- S is for a Switch, D is for a dimmer
-- example: local array_devices = {59,89,106,15,112,135}
-- example: local array_device_types = {"D","D","D","S","S","S"}
-------------------------------------------------------------------------
local array_devices = {5}
local array_device_types = {"S"}
local device_type
-- SD_ID = StateDevice ID (THIS IS NOT THE MOTION SENSOR ID
-- COULD BE ANYTHING THAT IS A BINARY SWITCH OR A STATE DEVICE)
local SD_ID = 5
-- turn device after sometime -- 1 minute delay
local delay = 60 * 1
local Switch_ServiceVar = "urn:upnp-org:serviceId:SwitchPower1"
local Dimmer_ServiceVar = "urn:upnp-org:serviceId:Dimming1"
-----State Device
local SD_ServiceVar = "urn:upnp-org:serviceId:SwitchPower1"
-------------Store / Get State / Device On/Off related
function set_state(device,value,SD_ID)
luup.variable_set(SD_ServiceVar, "Device_State" .. device, value, SD_ID )
end
function get_state(device,SD_ID)
return luup.variable_get(SD_ServiceVar,"Device_State" .. device, SD_ID)
end
function set_device_state(device,value,devicetype)
if devicetype=="S" then
luup.call_action(Switch_ServiceVar,"SetTarget",{ newTargetValue=value },device)
elseif devicetype=="D" then
luup.call_action(Dimmer_ServiceVar,'SetLoadLevelTarget', { newLoadlevelTarget=value }, device)
end
end
function get_device_state(device,devicetype)
local retvalue
if devicetype=="S" then
retvalue = luup.variable_get(Switch_ServiceVar,"Status", device)
elseif devicetype=="D" then
retvalue = luup.variable_get(Dimmer_ServiceVar,"LoadLevelStatus", device)
end
return retvalue
end
-------------Store / Get State / Device On/Off related
-- State Save / Restore Functions
function save_state()
--save the states of devices
for i, device in ipairs(array_devices) do
device_type = array_device_types[i]
local cur_state = get_device_state(device,device_type)
set_state(device,cur_state,SD_ID)
end
set_state("state_stored","yes",SD_ID)
end
function restore_state()
--restore to their original state
for i, device in ipairs(array_devices) do
device_type = array_device_types[i]
set_device_state(device,get_state(device,SD_ID),device_type)
end
set_state("state_stored","no",SD_ID)
end
-- State Save / Restore Functions
function turn_off()
local last_trip_time = get_state("last_trip_time",SD_ID)
local cur_time = os.time()
if (cur_time - last_trip_time) < delay then
--we dont want to turn off anything yet!!!
else
-- turn the devices off after saving state
save_state()
for i, device in ipairs(array_devices) do
device_type = array_device_types[i]
set_device_state(device,"0",device_type)
end
end
end
---turn devices back to their state if the state was saved previously
if get_state("state_stored",SD_ID) == "yes" then
restore_state()
end
local this_trip_time = os.time()
set_state("last_trip_time",this_trip_time,SD_ID)
luup.call_delay( 'turn_off', delay )