Cam anyone tell me what is wrong with this luup code? I am getting an error in lua.
I am trying to code that when a scene is triggerd, it will check the status of a door sensor (HRDS1). If the door sensor is open, then it should flash the rooms lights. If the door is closed it wont do anything.
I know the current code doesnt have the amount time of times it should flash. Iwould want it going 5 times.
Thanks
local Door1status= luup.variable_get(“urn:micasaverde-com:serviceId:ZWaveDevice1”, “Tripped”, 11)
if (Door1status == “1”) then
function DipOnPorchLight(stuff)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },6)
if (sBlink == “1”) then
luup.call_timer(“DipOffPorchLight”, 1, “10”, “”, “”)
end
end
function DipOffPorchLight(stuff)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },6)
if (sBlink == “1”) then
luup.call_timer(“DipOnPorchLight”, 1, “10”, “”, “”)
end
end
if (sBlink == “1”) then
sBlink = “0”
else
sBlink = “1”
luup.call_timer(“DipOffPorchLight”, 1, “10”, “”, “”)
end
return false
else
You’ve put a function definition into the middle of an if statement. That’s legal Lua, but probably not what you want. Also, you’ve ended the code snippet with an else keyword, which is definitely an error: else keywords can appear only in between if and end.
Thanks for describing what you intended. It means I can probably show you how the code should look.
-- Untested, may contain syntax errors.
function DipOnPorchLight(times)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },6)
luup.call_timer("DipOffPorchLight", 1, "10", "", times)
end
function DipOffPorchLight(times)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },6)
if (times != "0") then
luup.call_timer("DipOnPorchLight", 1, "10", "", tostring(tonumber(times)-1))
end
end
local Door1status= luup.variable_get("urn:micasaverde-com:serviceId:ZWaveDevice1", "Tripped", 11)
if (Door1status == "1") then
luup.call_timer("DipOnPorchLight", 1, "10", "", "5") -- on-off five times.
end
(I used to teach programming, so I recognize the code of a student who hasn’t grokked functions and the difference between declaration and use. Don’t feel bad. Luup’s weird syntax doesn’t make this stuff easy to learn.)
Don’t be surprised if this works less than optimally even when you’ve got it working. There are multiple points of failure in this kind of daisy-chaining of commands.
It appears that the error is in the middle section because if I cut it out then the server doesnt give me an error when I save it
function DipOffPorchLight(times)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },6)
if (times != "0") then
luup.call_timer("DipOnPorchLight", 1, "10", "", tostring(tonumber(times)-1))
end
end
function DipOffPorchLight(times)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },6)
if (times ~= “0”) then
luup.call_timer(“DipOnPorchLight”, 1, “10”, “”, tostring(tonumber(times)-1))
end
end
local Door1status= luup.variable_get(“urn:micasaverde-com:serviceId:ZWaveDevice1”, “Tripped”, 11)
if (Door1status == “1”) then
luup.call_timer(“DipOnPorchLight”, 1, “10”, “”, “5”) – on-off five times.
end[/code]
I pasted the code into the MiOS Developers > Test LuuP Code and it ran only five times. If it’s running forever for you then it must be getting triggered repeatedly.
You say you’ve got this code in a scene. Is your scene triggered by a timer or by an event?
You could alter it so that it counts down for both the on- and off-stages. Start with an even number and it will finish off. Start with an odd number and it will finish on.
function DipOnPorchLight(times)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },6)
if (times ~= "0") then
luup.call_timer("DipOffPorchLight", 1, "10", "", tostring(tonumber(times)-1))
end
end
function DipOffPorchLight(times)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },6)
if (times ~= "0") then
luup.call_timer("DipOnPorchLight", 1, "10", "", tostring(tonumber(times)-1))
end
end
local Door1status= luup.variable_get("urn:micasaverde-com:serviceId:ZWaveDevice1", "Tripped", 11)
if (Door1status == "1") then
luup.call_timer("DipOnPorchLight", 1, "10", "", "9") -- on five times, off four times.
end