2 checks in 1 Luup, how to do this?

I want to have 2 checks in a LUUP, how do i do this?

If 1 of the 2 virtual switches are switched ‘ON’ then a scene should not run, i came up with the following luup, is this correct?


Status1 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",67)
Status2 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",68)

if (tonumber(Status1) == 1) OR (tonumber(Status2) == 1)  then
  return false
end

return true

I program mysql in php, so it is very simular, but can someone explain to me how the if and else statements of a luup exactly work and how to set them?

In php this would be:

if (($Status1 ==1) || ($Status2==1))
{
return false;
}
else 
{
return true;
}

Could some ‘quicky’ explain how luup works with multi if/else values?

you have it, really. If-then statements are as straightforward as they are written

With Vera, you are trying to test for True (Let the scene run) or False (cancel the scene).

(comments in Lua are preceded with ‘–’ and Lua ignores comments after these and before a carriage return, and my comments are in the code)

local status1 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",67)  --you will see Lua programmers define variables in the 'local' routine versus global
local status2 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",68)
if (tonumber(status1) == 1) OR (tonumber(status2) == 1) -- you are testing here to see if either switch is ON **see note
   then -- the then statement tells Lua what to do if 'if' is true
   return false -- cancels scene and stops the Lua
end -- ends this part of the program and the program proceeds to the next part
return true -- while the default return in vera's Lua is true, good programming includes this to set the variable in clean code, many of us will leave this out.

Does that help?

**Note
Lua does a great job of coercion of variables into integers and numbers so you may see [if (tonumber(status1) == 1)] written by some programmers as [if status1 ==1] so don’t get confused. Lua is pretty smart like that and will automatically convert string and number types to the correct format in order to perform calculations.

Lua does do coercions but not for comparisons.

Thus, you can write:

X = 12 + "30"

and expect the answer to be 42, but:

42 == "42"

will never be true.

[quote=“Bulldoglowell, post:2, topic:180393”]you have it, really. If-then statements are as straightforward as they are written

With Vera, you are trying to test for True (Let the scene run) or False (cancel the scene).

(comments in Lua are preceded with ‘–’ and Lua ignores comments after these and before a carriage return, and my comments are in the code)

local status1 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",67)  --you will see Lua programmers define variables in the 'local' routine versus global
local status2 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",68)
if (tonumber(status1) == 1) OR (tonumber(status2) == 1) -- you are testing here to see if either switch is ON **see note
   then -- the then statement tells Lua what to do if 'if' is true
   return false -- cancels scene and stops the Lua
end -- ends this part of the program and the program proceeds to the next part
return true -- while the default return in vera's Lua is true, good programming includes this to set the variable in clean code, many of us will leave this out.

Does that help?

**Note
Lua does a great job of coercion of variables into integers and numbers so you may see [if (tonumber(status1) == 1)] written by some programmers as [if status1 ==1] so don’t get confused. Lua is pretty smart like that and will automatically convert string and number types to the correct format in order to perform calculations.[/quote]

Thx.

But i don’t get it to work…

I used the following lua:

local Status1 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",53)
local Status2 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",68)
if (tonumber(Status1) == 1) OR (tonumber(Status2) == 1)
then
  return false
end
return true

When i save it vera reports:
Error: error in lua for scenes and events

I really don’t see what is wrong here?

Is there somewhere an option where it is listed in which scene and lua line the error is?

When i comment out some parts the error is gone:

local Status1 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",53)
--local Status2 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",68)
if (tonumber(Status1) == 1) -- OR (tonumber(Status2) == 1)
then
  return false
end
return true

the above code give no lua error anymore, so i am pretty sure the error is somewhere in my lua code in this scene, buit i dont see it…

edit:

it seems that the command OR is not reqonized in lua, when i change OR into AND the lua works, but with the wrong effect…
I need and OR statement, not an AND statement…

Can someone advise how to fix this?

Try this:

local Status1 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",53) local Status2 = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",68) if (tonumber(Status1) == 1) or (tonumber(Status2) == 1) then return false end return true

thx, got it…

It seems in lua ‘OR’ in capital letters is not the same as ‘or’ in lower letter …
In lower letters the error is gone…

Yup. Lua is sensitive to case.