We have moved at community.getvera.com

Author Topic: 2 checks in 1 Luup, how to do this?  (Read 2145 times)

Offline B3rt

  • Full Member
  • ***
  • Posts: 232
  • Karma: +2/-8
2 checks in 1 Luup, how to do this?
« on: March 25, 2014, 09:00:46 pm »
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?

Code: [Select]

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:
Code: [Select]
if (($Status1 ==1) || ($Status2==1))
{
return false;
}
else
{
return true;
}

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

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: 2 checks in 1 Luup, how to do this?
« Reply #1 on: March 25, 2014, 09:45:59 pm »
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)

Code: [Select]
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.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +291/-70
  • "Less is more"
Re: 2 checks in 1 Luup, how to do this?
« Reply #2 on: March 26, 2014, 04:25:49 am »
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:
Code: [Select]
X = 12 + "30"

and expect the answer to be 42, but:
Code: [Select]
42 == "42"

will never be true.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline B3rt

  • Full Member
  • ***
  • Posts: 232
  • Karma: +2/-8
Re: 2 checks in 1 Luup, how to do this?
« Reply #3 on: March 26, 2014, 07:46:24 pm »
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)

Code: [Select]
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.

Thx.

But i don't get it to work..

I used the following lua:
Code: [Select]
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:
Code: [Select]
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?
« Last Edit: March 26, 2014, 08:09:39 pm by B3rt »

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: 2 checks in 1 Luup, how to do this?
« Reply #4 on: March 26, 2014, 08:13:07 pm »
Try this:

Code: [Select]
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

Offline B3rt

  • Full Member
  • ***
  • Posts: 232
  • Karma: +2/-8
Re: 2 checks in 1 Luup, how to do this?
« Reply #5 on: March 26, 2014, 08:18:21 pm »
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....

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: 2 checks in 1 Luup, how to do this?
« Reply #6 on: March 26, 2014, 08:21:11 pm »
Yup. Lua is sensitive to case.