Inserting function parameters into http post string

#1

Hi I have the folowing code working:

–Purpose : Sets aircon zone 1 (Master Bed) to cool on–
–Inputs : N/A–
–Outputs : N/A–
–Notes : Control bridge IP xx.x.x.xxx call twice to change to auto and setpoint, global function–
–Version : 0–
–Revision : 4–

–Zones 1(Master Bed) on–

function zone1coolOn()
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)

local path = "http://xx.x.x.xxx/ZoneCommand?"
local payload = [[ {“ZoneCommand”:{“ZoneNo”:“1”,“Command”:“22”}} ]]

local status = http.request
{
url = path,
method = “POST”,
headers =
{
[“Content-Type”] = “application/json”,
[“Content-Length”] = payload:len()
},
source = ltn12.source.string(payload)
}
end
local output = pcall(zone1coolOn)
–call twice–
local output = pcall(zone1coolOn)

I want to pass the zone and temp parameters through the function when I called it (as per below local payload) but cant get it to work. Any help would be much appreciated.

–Zones 1(Master Bed) on–

function zonecoolOn(ax, bx)
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)

local path = "http://10.1.1.159/ZoneCommand?"
local payload = [[ {“ZoneCommand”:{“ZoneNo”:ax,“Command”:bx}} ]]

local status = http.request
{
url = path,
method = “POST”,
headers =
{
[“Content-Type”] = “application/json”,
[“Content-Length”] = payload:len()
},
source = ltn12.source.string(payload)
}
end
local output = zonecoolOn(1,22)
–call twice–
local output = zonecoolOn(1,22)

0 Likes

#2
cant get it to work.

…is not a very comprehensive diagnostic. However, you need to read up a bit on Lua string construction, because you definitely have a problem here:

local payload = [[ {"ZoneCommand":{"ZoneNo":ax,"Command":bx}} ]]

which puts the literal variable names into the string, not their values.

My preferred pattern would be:

local command = [[ {"ZoneCommand":{"ZoneNo":"%s","Command":"%s"}} ]]
local payload = command: format(ax, bx)
0 Likes

#3

Thanks akbooer, I had tried a few iterations with brackets etc and was getting frustrated… will definitely need to read up some more but thanks heaps for your pointers, much appreciated. Will post how I go, just putting the final touches on an IZone435 aircon integration. Will post once “perfected”

0 Likes

#4

I currently have code that works perfect when tested in the “Test Luup code (Lua)” of my vera, but I get

LuaInterface::CallFunction_Timer-5 function coolcheck failed attempt to call a nil value <0x750c0520>

in the LuaLog… I have tried placing all functions in the start up file but to no avail: code is as follows:

–Purpose : Sets aircon Mode to Cool & checks status–
–Inputs : N/A–
–Outputs : N/A–
–Notes : Control bridge IP 10.1.1.159 –
–Version : 1–
–Revision : 0–

function coolcheck()
local currentmode = luup.variable_get(“urn:toggledbits-com:serviceId:SiteSensor1”, “Value2”, 107)

 if currentmode == "cool" then
          luup.log("Aircon COOL is set")
                                  
      else
          luup.log("Aircon COOL is NOT set")
          pcall(coolmodeOn)
      end
return true

end

function coolmodeOn()
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
local path = "http://10.1.1.159/SystemMODE?"
local payload = [[ {“SystemMODE”:“cool”} ]]

local status = http.request
{
url = path,
method = “POST”,
headers =
{
[“Content-Type”] = “application/json”,
[“Content-Length”] = payload:len()
},
source = ltn12.source.string(payload)
}
luup.log(“Aircon COOL command Sent”)
luup.call_delay(“coolcheck”,30)
return true
end

pcall(coolmodeOn)

0 Likes

#5

Solved the problem:

It should be noted that the function being called must be in the global space. [glow=red,2,300]Depending on where that function is in your program this might not be automatic[/glow]. If you are getting an error that says the function is nil this is likely the case. A quick fix is to add the line:

_G.functionname = functionname

No more errors :smiley:

0 Likes