We have moved at community.getvera.com

Author Topic: Inserting function parameters into http post string  (Read 465 times)

Offline m13kern

  • Newbie
  • *
  • Posts: 9
  • Karma: +1/-0
Inserting function parameters into http post string
« on: February 17, 2019, 08:09:33 pm »
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)

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Inserting function parameters into http post string
« Reply #1 on: February 18, 2019, 02:32:20 am »
Quote
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:

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

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

My preferred pattern would be:

Code: [Select]
local command = [[ {"ZoneCommand":{"ZoneNo":"%s","Command":"%s"}} ]]
local payload = command: format(ax, bx)
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 m13kern

  • Newbie
  • *
  • Posts: 9
  • Karma: +1/-0
Re: Inserting function parameters into http post string
« Reply #2 on: February 18, 2019, 06:11:31 am »
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"
« Last Edit: February 18, 2019, 06:23:16 am by m13kern »

Offline m13kern

  • Newbie
  • *
  • Posts: 9
  • Karma: +1/-0
Re: Inserting function parameters into http post string
« Reply #3 on: February 20, 2019, 04:56:32 pm »
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)

Offline m13kern

  • Newbie
  • *
  • Posts: 9
  • Karma: +1/-0
Re: Inserting function parameters into http post string
« Reply #4 on: February 22, 2019, 12:51:34 am »
Solved the problem:

It should be noted that the function being called must be in the global space. Depending on where that function is in your program this might not be automatic. 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 :D