Why is the lua code giving an error?

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.

I am new at scripting and that code was patched together from different sources.

However, I pasted your code in, and it still says that there is an error in the luup code.

Thank you for your help.

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

What error message does your log return:

http://<vera_ip>/cgi-bin/cmh/log.sh?Device=LuaUPnP

Lua uses [tt]~=[/tt] instead of [tt]!=[/tt], which most other languages use.

So fixing that up, the server no longer gives an error.

However, when I run the code, nothing happens:

[code]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[/code]

I have it in the luup section of the scene.

Thanks

Are you sure the door sensor isn’t a SecuritySensor1 serviceId, using the standard list of service ids?

It is a HRDS1, when I hover my mouse over the variables it says ZWaveDevice1.

Thanks

I realized I had the wrong ID for the sensor.

However, now it just keeps on going. It does not stop by after five times.

Thanks.

Sorry, it was late and when I’ve got a cold I flip between languages unpredictably.

Thanks anyway, the main code worked.

How would I get it that the light stayed on when finished? As of now it ends off in the off position.

Thanks

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

Sorry, it was late and when I’ve got a cold I flip between languages unpredictably.[/quote]

LOL, the reason I recognized it was because I’d done it myself a zillion times :slight_smile: