Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


[TUTORIAL} Introduction to API calls and http requests


AutoFrank

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?

 

Please login or register to see this code.

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 comment
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 comment
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: 

Please login or register to see this link.

 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 comment
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 comment
Share on other sites

  • 2 weeks later...

 

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

 

Please login or register to see this code.

 

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.

Please login or register to see this code.

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 comment
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 comment
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 comment
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 comment
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:

Please login or register to see this code.

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:

Please login or register to see this code.

Enjoy coding :-)

 

Link to comment
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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

  • 2 months later...

How do I provide user authentication when calling from a scene with HTTPClient ?

 

In a VD, I can use use the setBasicAuthentication method. But this does not work in a scene.

 

Simple call in a VD, works fine:

Please login or register to see this code.


 

Same call in a scene, does not work:

Please login or register to see this code.

 

 

 

 

 

Edited by perjar
Typos
Link to comment
Share on other sites

I have never needed to use credentials that way, so sorry, I do not know. This is however an example of a functioning POST in a scene and you can hopefully take it from here:

 

local http = net.HTTPClient()
  http:request(("http://127.0.0.1:11111/api/devices/1037/action/setThermostatSetpoint"), {
    options = {
    method = 'POST',
    headers = {
        ['Accept'] = 'application/json',
        ['Content-Type'] = 'application/json'
    },
    data = '{"args":[1,21]}'

      
  },
  success = function(response)
              local result = response.data;
              if response.status == 200 then
                  fibaro:debug(response.status)
                fibaro:debug(response.data)
              else
                  fibaro:debug(response.status)
                fibaro:debug(response.data)
              end
            end,
  error = function(err)
              fibaro:debug('[ERROR] ' .. err)

          end
  })

Edited by tnesheim
Link to comment
Share on other sites

I've done some further research into the issue and found a way to address this by using some LUA code found here:

Please login or register to see this link.

 

So, here is how to add an authorization header to a HTTPClient call in a scene:

Please login or register to see this code.

 

 

 

Edited by perjar
Link to comment
Share on other sites

  • 2 weeks later...

Hi

I am trying to convert my olde hc code to my new hc3 but iam doing something wrong ??

 

old code 

 

local http = net.HTTPClient()  
  http:request('https://192.168.1.103/control/control?set&section=imagecontrol&obscure_image=disable', {
     options = {
        checkCertificate = false ,
        method = 'get',
        headers = { 
            ['Authorization'] = 'xxxxxxxxxxxxxxxxxxxx'
        }
    },
    
        success = function(resp)
        if resp.status == 100 then
          fibaro:debug(resp.status)
          
        else
          fibaro:debug(resp.status)
        
       
        end

      end
    })

 

 

 

 

 

 

 

 

new code :

 

function logger(msg)  
    l=net.HTTPClient({timeout=2000});     
    l:request("https://192.168.1.103/control/control?set&section=imagecontrol&obscure_image=disable']"", 
  {options = {checkCertificate = false ,
              method = 'POST',
        headers = {['Authorization'] = 'Basic xxxxxxxxxxxxxxxx'("user:pass")},
        },
                   success = function(status) 
                      -- Some code if all went well
                   end,
                   error = function(error)
                      -- some code if error response
                   end})  
end
 
 
 
 
 
can some body help ?
Edited by Stef4321
Link to comment
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...