Jump to content

[TUTORIAL} Introduction to API calls and http requests


Recommended Posts

Hi @AutoFrank

 

Brand new to HTTP and api requests and thanks for your post.

 

Why am I getting this :

[ERROR] 16:25:17: line 7: Expected value but found T_END at character 1

 

when I copy past your http request?

 

local device = fibaro:getSelfId()
localhost = '127.0.0.1'
local port = 11111
diag = Net.FHttp(localhost, port);
response = diag:GET("/api/devices/176")
result = json.decode(response);
fibaro:debug("name: "..result.name)
fibaro:debug("properties.value: "..result.properties.value)

I'm using my HC2 IP after local host and have inserted my device 64 in place of 176.

 

What am I missing?

 

Anybody?

 

edit: OK I was too smart for my own good. DON'T CHANGE IP

 

However, What is that IP local host number? how does it relate so I can understand?

 

 

Edited by sarf77
found original answer
Link to post
Share on other sites
  • 2 weeks later...

Hi Frank,

 

This is very useful thread; by any chance do you have any examples that show the Syntax required for a POST/PUT command?  So for example i am trying to understand how to send a command that will change the set point temperate of a heating controller to a passed in temperature value.

 

Kind Regards

 

Paul

Link to post
Share on other sites
  • 1 month later...

Hello AutoFrank, i release a presence-detection by use the solution from domotique with wifi and macadress. This works fine but... my son got an iphone and now isn't it work fine. 

I look around the internet and found a solution that works with FRITZBOX (this is a very popular internetrouter in germany). The guy on this forum wrote a script in php with an SOAP-Action request: https://www.schlaue-huette.de/apis-co/fritz-tr064/anwesenheitserkennung-mit-der-fritzbox/ I'am not a newbie on LUA but for all that effort i didn't found an entry for a solution.

Now i look on your tutoriual and you make a mention to SOAP-Action. 

If you can spare a little time for support this problem  - i'm sure many other users will be satisfied.

Thank you very much in advance.

Link to post
Share on other sites
  • 2 months later...

Hello @AutoFrank,

 

thank you very much for this tutorial, I am not a developer, and I found it really helpful!

I just came across a problem, where my knowlede of Json calls ends:

I bought a new AC Unit (Innova 2.0) , and I have been able to sniff the api from their app. Now I don't know how to solve this problem: The api usually is setup like this GET ip/api/v/1/status or POST ip/api/v/1/power/on

But when it comes to setting a temperature the api delivered: POST ip/api/v/1/set/setpoint ; and then in the data "field" it said "p_temp=27' (for setting 27 degrees).

How can I POST this data in a VD (preferably) or scene?

 

Any help is highly appreciated

 

cheers

joystick

Link to post
Share on other sites
  • 2 weeks later...

 

W dniu 27.02.2017 o 20:06, AutoFrank napisał:

 

function updateTable()
  if fibaro:getGlobalValue("NetworkTable") == nil then
    local http = net.HTTPClient()
    http:request("http://127.0.0.1:11111/api/globalVariables", {
        options = { method = 'POST', headers = {}, data = '{"name":"NetworkTable","value":""}', timeout = 2000 },
          success = function(status)
            setTimeout(popTable, 3000)
          fibaro:debug(status.status)
          if status.status ~= 200 and status.status ~= 201 then print("failed"); end
          --print(status.data);
        end,
        error = function(err)
          print('[ERROR] ' .. err)
        end
      })
    else
      popTable()
    end
end

 

Hi Frank,
I'm green with it and I'm just starting to play with the API, but based on your entry with an example, I read the value of a given rainDelay from the panel drenchers.

local http = net.HTTPClient()
http:request("http://127.0.0.1:11111/api/panels/drenchers", {
	options = { method = 'GET', timeout = 2000 },
    success = function(status)
    print(status.status)
    if status.status ~= 200 and status.status ~= 201 then print("failed"); end
    --print(status.data);
    local results = json.decode(status.data)
    print(results.rainDelay)
	end,
    error = function(err)
    print('[ERROR] ' .. err)
    end
})

Generally, I wanted to ask for an explanation of how to save rain.delay or drenchers, cycles for the chosen sprinkler id, using the API.

I understand that the way of saving the value for the selected device will be the same?
Thank you in advance for your help

Mirek

Link to post
Share on other sites
  • 1 month later...
On 2/17/2017 at 9:09 PM, AutoFrank said:

To access an api you need 2 things

 

  • ip address, port, etc to make the http/https request
  • Structure/format of the response so you know how to traverse it and extract what you want

@AutoFrank Hello AutoFrank!

I´ve been looking for information about making HTTPS requests from a scene (or VD). I´ve been told that HC2 supports HTTP only, but looking at your post it seems like HTTPS is possible after all?

 

Link to post
Share on other sites
  • 2 months later...

Hi,

I want to create an api to execute a command to remove all notifications. I want to rund this every 2nd hour in a scene.

I have very little experience using these kind of toolkits.

Si there someone here who can help me out?

 

I have used this string in a button, but does not seem to work properly;

 

api.delete('/panels/notifications')

 

Thank you in advance for helping!

Link to post
Share on other sites
On 9/8/2019 at 9:19 PM, tnesheim said:

I´ve been looking for information about making HTTPS requests from a scene (or VD). I´ve been told that HC2 supports HTTP only, but looking at your post it seems like HTTPS is possible after all?

Hi @tnesheim,

 

in VD indeed only HTTP is possible, but you can use HTTPS in scenes.

 

Enjoy coding :-D

 

  • Thanks 1
Link to post
Share on other sites
12 hours ago, vclor said:

I have used this string in a button, but does not seem to work properly;

 

api.delete('/panels/notifications')

 

Hi @vclor,

the correct syntax for deleting notification is:

api.delete('/panels/notifications/{notificationID}')

So you will need the correct id of the notification to be deleted.

First you can retrieve the notifications to get the notification ID's:

api.get('/panels/notifications')

Enjoy coding :-)

 

Link to post
Share on other sites
  • 7 months later...

Hello, 

 

I am quite new to Fibaro, LUA and using API calls, so excuse me if I am stupid. 

 

I would like to make an API call to execute a command on my air conditioning. At the moment looks like I can only do this via an external target which is app.melview.net. There may be a way to do via the LAN IP of the controller, but I can't workout how to authenticate against that as yet.  I can successfully auth to the external API however, and then post actions to execute on off etc. However, I have no idea how to translate this into a Scene or VD? 

 

From what I am reading I may have to do this in a scene and not in a VD? also, having trouble finding the format that I need to structure the request. 

 

Can I just re-authenticate everytime, or can I leverage a cookie which will need to be refreshed every now and then? 

 

Following are the API calls that I can use successfully outside of Fibaro, I just need to understand how to translate this into a scene or VD. Sorry if this doesnt make sense, Im just starting out here. Would appreciate any input. 

 

This is my API call to Authenticate and obtain a cookie:

 

POST /api/login.aspx HTTP/1.1
Host: api.melview.net
Authorization: Basic YWRhbw
Content-Type: text/plain

{
    "user": "[email protected]",
    "pass": "mypassword",
    "appversion": "4.3.1010"
}

 

Then to execute a power on command I can send this... 

 

POST /api/unitcommand.aspx HTTP/1.1
Host: api.melview.net
Authorization: Basic YWRhbww
Content-Type: text/plain
Cookie: auth=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

{
                "unitid": "151157",
                "v":2,
                "commands":"PW1"

            }

 

 

Thanks in Advance !!!

 

Edited by adamgab
removed pwd
Link to post
Share on other sites

Hi

Looks like you are using the «script» feature of the VD and not the LUA. I have used LUA only.

 

Right now I only have my phone, please remind me in a week or so, then I’m back at my computer, and I’ll send you some examples.

 

In the meantime do some Googleing for «API fibaro lua» etc...this will hopefully give you some examples... If you search for posts created by me in this forum you’ll find some examples for communicating with Homey, but this can be used as a starting point. Please note that there are differenced between VD and scenes both in syntaks and capabilities...

Edited by tnesheim
Link to post
Share on other sites

Thanks @tnesheim !, I will continue to have more of a look, all my searches seem to come up with using the Fibaro API to make calls to Fibaro devices, but I will continue to read of course!

Link to post
Share on other sites
  • 2 weeks later...

Hi @tnesheim, Ive not had a great deal of luck as yet. So if you have any spare time to provide some input that would be fantastic. 

 

looking at your initial reply, you mention scripting vs LUA. 

 

I am not using scripting, I am happy to do within LUA as a part of a scene. The example I posted is just the actual API call that I want to make.

 

What I am wondering is how do I translate that into a scene or VD.  This is actually the API for my AC. I just want to be able turn it on or off based on the temperature reported by one of my zwave sensors.

 

Thanks Again. 

 

 

Link to post
Share on other sites
  • 1 month later...

Hi guys I need some help again :)

 

I'm new in using a rest api ... so have some basic question ... 

In below example, I have found the right information in a Virtual Device ... and want to change a label ... but somehow it is not writing back the .. any idea what I'm doing wrong ?

 

--- example:= SCENE 

 

local http = net.HTTPClient()
http:request("http://127.0.0.1:11111/api/virtualDevices/257", {
    options = { method = 'GET', timeout = 2000 },
    success = function(status)
    print(status.status)
    
    if status.status ~= 200 and status.status ~= 201 then
      print("failed"); 
    end
      
    print(status.data);
    --- current Info
    local jsonTable = json.decode(status.data)
    print(jsonTable.properties.rows[12].elements[1].caption)
      
       --changening caption  
      jsonTable.properties.rows[12].elements[1].caption="Off3"
      
    --sending it back  
      local response, status, errorCode = api.put(status.data, json.encode(jsonTable))
      
    end,
    error = function(err)
    print('[ERROR] ' .. err)
    end
})
 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...