Author Topic: Set & Get variable on remote Vera  (Read 4761 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Set & Get variable on remote Vera
« Reply #15 on: August 19, 2017, 02:53:43 pm »
Doh !!

I forgot to set the other power entry as a string "0" too !!!

Working now I think..

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Set & Get variable on remote Vera
« Reply #16 on: August 19, 2017, 04:00:23 pm »
Code: [Select]
if ((power == 0) and ((light < 20) or daynight == 1)) thenyou need to check for power equal to "0".  But why not make it an 'else' instead of checking again?

Code: [Select]
if (power == "1") then 
   print("Light does not need to be turned on")
   return  false 
elseif (light < 20) or daynight == 1 then
  print("Light Should Be Turned On")
  return true
else
  print("failed")
  return false  -- need to return false
end

Also, tonumber() didn't work for you because it can take 2 parameters, the second one is the base.  A straightforward way to do get the number would be to get the power variable as a string first, then convert it to a number afterwards.
Code: [Select]
local status1, powerString = luup.inet.wget("http://192.168.1.183:3480/data_request?id=variableget&DeviceNum=103&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status")
local power = tonumber(powerString)

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Set & Get variable on remote Vera
« Reply #17 on: August 19, 2017, 04:19:46 pm »
Also, tonumber() didn't work for you because it can take 2 parameters...

This, in fact, is not the case here, since the luup.variable_get() parameter has been surrounded with (double) parentheses within the function call, turning it into an evaluation which returns only one value.

For example:

Code: [Select]
local function test(...) return ... end

print (test(1,2))
print ((test(1,2)))

prints the results:

Code: [Select]
1       2
1

However, there are certainly clearer ways to write the code!
« Last Edit: August 19, 2017, 04:29:37 pm by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Set & Get variable on remote Vera
« Reply #18 on: August 19, 2017, 04:40:01 pm »
Quote
Also, tonumber() didn't work for you because it can take 2 parameters...

This, in fact, is not the case here, since the luup.variable_get() parameter has been surrounded with (double) parentheses within the function call, turning it into an evaluation which returns only one value.

Agree with you regarding the luup.variable_get, but the problematic statement was the inet.get which would be a problem, correct?  I believe @parkerc was asking about this line:
Code: [Select]
local status1, power = tonumber(luup.inet.wget("http://192.168.1.183:3480/data_request?id=variableget&DeviceNum=103&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status"))  to_number() wouldn't work even with extra parentheses, true?

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Set & Get variable on remote Vera
« Reply #19 on: August 19, 2017, 05:05:28 pm »
Agree with you regarding the luup.variable_get, but the problematic statement was the inet.get which would be a problem, correct? 

Ah yes, I'm with you now.  Indeed, that will always just return the status!

Always better to split things into separate lines rather than composing functions.  Much clearer, much easier to debug, fewer errors in the first place!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Set & Get variable on remote Vera
« Reply #20 on: August 19, 2017, 06:47:57 pm »
Huge thanks to you both.

Thanks as always for your patience. Due to having very  limited 'play' time (family and the day job) I'm only able to dip in and out of trying to code something on my vera.

When you have time I'm curious about the use of luup.inet.wget and why do I only get the required variable value using a direct call via a browser,  yet it seems returns two different values when called wrapped within luup.inet.wget ?

The first value is a status (which seems to always be a "0" string?) and the second being the required variable value as a string.


Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Set & Get variable on remote Vera
« Reply #21 on: August 19, 2017, 07:09:21 pm »
Here's the documentation for the wget call:
http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_wget

It actually returns three values these days.  Many Lua functions do return multiple values.  The string is what's returned in the HTTP response body, and it's just that which the browser shows, unless the status returns an error code.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.