Author Topic: Adding http code in Luup Scenes  (Read 3810 times)

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 307
  • Karma: +2/-1
Adding http code in Luup Scenes
« on: August 26, 2011, 11:57:01 am »
I need to find the status of a switch on my second Vera using HTTP commands.  So far this works in a browser:

http://my_ip:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=12

This return the state of my device 12 as a 1 or 0, exactly what I'm looking for.  Now I need to place this in the Luup tab of a scene.

I think this should work but as far as I can tell it is not: (with a 2 second timeout)

luup.inet.wget( "http://192.168.81.3:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=12", 2)

I need to place the result in a local variable for testing using logic.

local test=luup.inet.wget( "http://192.168.81.3:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=12", 2)

Can anyone let me know where I'm going wrong?

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0

Offline guessed

  • Master Member
  • *******
  • Posts: 5293
  • Karma: +90/-22
  • Release compat is not a bolted-on afterthought
Re: Adding http code in Luup Scenes
« Reply #2 on: August 26, 2011, 09:13:34 pm »
@JimMac,
Can you provide more details on how you know if it's working (or not)?   The snippet looks correct, and similar(ish) to something @JOD was doing the other day for calling a remote Scene, but in your case you want data back.

You could also run the URL:
    http://192.168.x.x/cgi-bin/cmh/log.sh?Device=LuaUPnP

on both Vera's at the same time in order to see whatever error is coming back when it's being run (caller Vera), and what's happening on the other end (callee Vera)

BTW: After you get the value back, what are you doing in the rest of the Scene script?  It's possible that logic has a problem, and this bit is working fine.


@Ap15e,
I don't think he can do that, as it sounds like he has two [unrelated] Vera units operating... hence the URL method.  If the Vera's were registered with each other he may be able to use your method (only person I know that's ever tried that is @JOD)

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 307
  • Karma: +2/-1
Re: Adding http code in Luup Scenes
« Reply #3 on: August 27, 2011, 09:20:18 am »
If I place the first code example in my local browser and my device 12 (virtual switch) if off my browser returns "0" and a "1" if the switch is on.  To test the code in a Luup scene I'm using a if-then-end logic to turn on/off a virtual switch.  So far it is only returning a logical 1 all the time.

Normally I us UPnp to connect my two Vera's together as full devices but I'm having issues with the connections and I have submitted a trouble ticket.  So as an alternative I am starting to pass HTML-UPnP commands between both units.  I can not use luup.variable_get(...) coding since the device I want to control exists on another Vera.

I'll explain what I'm trying to do; I have a rental cabin, when a valid keypad code is entered it turns on the water heater and other devices BUT the water supply comes from the main house where my other Vera is.  In order to turn on the water supply I need to determine the current state of the water control switch or the state of my house "Are we home" virtual switch.    If the water is off I can turn it on with this:

-- Turn on the water supply(Device 43) in the house, timeout 6 seconds.
luup.inet.wget("http://local_ip:49451/data_request?id=lu_action&DeviceNum=43&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1",6)

Since the HTML-UPnP command string in my previous post checking the state of my device 12 and works within a browser I now need to get the correct syntax to use it in the Luup tab of a scene.  Ideally I need to place the returning 1 or 0 in a local variable.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Adding http code in Luup Scenes
« Reply #4 on: August 27, 2011, 12:13:10 pm »
I would tend to agree with guessed that the syntax of your code is correct.

Please double check the IP address and the device ID, try increasing the timeout.

I tried to reproduce the problem with my two Veras and my WAI plugin:


http://192.168.x.y:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=4
does work in a browser.


Code: [Select]
local status, content = luup.inet.wget( "http://192.168.x.y:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=4" )

luup.variable_set( 'urn:upnp-ap15e-com:serviceId:WAI1', 'Location', tostring( status )..'*'..tostring( content )..'*', 36 )

does work in the "Test Luup code" window and in a scene.

The first return argument is the HTTP status code (usually 0) ...

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 307
  • Karma: +2/-1
Re: Adding http code in Luup Scenes
« Reply #5 on: August 27, 2011, 02:43:34 pm »
I have the code working, thanks Ap15e for your help.  I forgot about the command status being returned as the first parameter.  I also found that if I try to run a local scene that is testing device status or changing device status the code does not work.  Change the device numbers and IP address to another Vera the same code works.  I even tried using 127.0.0.1 instead of the local IP address and that didn't work either.
I also found that if Vera is in switch mode none of the Luup UPnP commands work or are totally unreliable, switch your network setting to Automatic and everything works again. Is this a bug?

Anyways for others who want to experiment with this here is one example of my final code.  This is the exit scene from my cabin rental.  If the main house is not occupied and the water is on (for the rental unit) then shut the water off.  This makes sure that if someone leaves the rental unit they don't shut the water supply off if the house is occupied.

-- Set the "Cabin occupied" to "0"
luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,12)

-- Find the status of the house "Are we home" virtual switch (Vera #2)
local status, weRhome= luup.inet.wget( "http://ip:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=55" )

-- Find the status of the house water supply switch  (Vera #2)
local status, HouseWater= luup.inet.wget( "http://IP:49451/data_request?id=lu_variableget&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status&DeviceNum=43" )

-- If the house is empty and the water is on then turn it off.
if weRhome== "0" and HouseWater=="1" then
-- Turn off the water supply(Device 43) in the house.
luup.inet.wget("http://IP:49451/data_request?id=lu_action&DeviceNum=43&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0",6)
end
« Last Edit: August 28, 2011, 11:38:52 am by JimMac »