Edge Scene LUA code

For my first LUA code I wanted to add a simple toggle switch. Presumably all I need to do is grab an example, paste it into the LUA code page in the scene creation Wizard and away I go…nope doesn’t work. OK lets paste that into the test box. Yep doesn’t work. Unfortunately, there is no feedback from the “Test Code” box other than nope doesn’t work. Well, since it didn’t turn the light on I sort of know that when I execute the scene.

So here is what I put into the test box and into the Scene which “does not work”:

[tt]
function toggle()
local switchOnOff = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, 38)
if (switchOnOff == “1”) then
– Switch is on
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, 38)
else – switch is off
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “1”}, 38)
end if
end
[/tt]

After making several guesses aka the example is wrong, cut it down to one line etc no matter what I do it doesn’t work in the test box. So there is something missing in my concept set. I tried this with and without the function declaration. The syntax checker in Notepad++ is happy nad device 38 is for sure a binary switch.

Can someone please fill in the blanks?

Add to the end …

return true

And if you want to test the function, not just make sure the syntax is correct … add to the end:

toggle()
return true;

or try to use the Utility LUA test …
http://forum.micasaverde.com/index.php/topic,24018.0.html

or have you tried to see if your device responds to:

luup.call_action("urn:micasaverde-com:serviceId:HaDevice1", "ToggleState", "", 38)

Thanks for the quick response. My example was incorrect because it used “end if” vs just “end”. Or I had a brain fart and just typed in end if.

When I put your code (with a luup instead of uup) into the test code box and test it the light toggles but I get a prompt box:

“Failed to test code, please try again”

So it worked but the message says failed.

When I put my “end if” code in the prompt box says:

“Code failed”

BTW: Toggle was an example what I am really doing is humidifier control which I think I can get to work now.

That was a typo on my part …
The thing is the Test window needs the code to return true otherwise it always reports a failure.

Yes that makes sense. Now that I have a basic understanding I can do anything…well we shall see.

The humidifier control is a bit more challenging. I understand how to log to the master log file. The Vera 2-3 documentation says log in and tail a particular file. I understand that concept. Is there a way to view the log file from the browser? Is that the log that pulls down (yellow background) on the Dashboard? Do you have to plug in a USB to view that log?

There are apps which make viewing the log convenient, but for a start you can simply look at it in a browser:

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

You can’t stop it scrolling, but you can capture all the previous text by simply selecting all and cutting then pasting into your faviourite word processor.

Thanks to all. The log works pretty much as expected.

By the way this is the working lua code. Just paste into a scene for lua code and you can toggle a light. This demonstrates simple logic, getting information from the device and initiating an action in a device based on the state. If all you want to do is toggle a binary light there is an easier way:

    local switchOnOff = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 38)
    if (switchOnOff == "1") then
      -- Switch is on
      luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, 38)
    else -- switch is off
	  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 38)
    end
    return true

Still gives me the same failed message prompt box for the “Test Window” even with the return true in there.

Maybe you have some weird hidden characters in the string.
The following works for me (Note I used a different device#)

local switchOnOff = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 9)
if (switchOnOff == "1") then
    -- Switch is on
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, 9)
else -- switch is off
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 9)
end
return true

Pasted that in changed the device to 38 it works and I get a prompt box which says:
Failed to test code, please try again

This is Vera Edge U17.

OK, I tested on UI5 … probably another UI7 bug.
I suggest you use the LUA Test Utility I mentioned earlier.

The log file is great albeit busy. Most code errors are enumerated in the log file and give the line number and the error.

You REALLY should be use the Test Utility that I mentioned earlier:

http://forum.micasaverde.com/index.php/topic,24018.0.html

Hi,

I tested the sample code in “Test luup code” on VeraEdge and Vera 3 with UI7 and it worked. I searched for this bug internally and it was confirmed as fixed at the beginning of this month. Is true that I used an intermediate build for testing, but this means that the fix will be available in our next release.

Best Regards,

  • Andrei -

Thanks.