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


  • 0

HTTP using Scene and/or VD


Question

Posted

I am trying to call Fibaro REST APIs from either Lua script within a scene, or from a VD and I have struggled for hours without success.  I think I've read nearly every post here and still no joy.

 

I have been able to run node.js calls to my HC2 so I know it is just my Lua code.  This is my latest attempt from a scene, which has been as much progress as I've made in 4 hours.

Please login or register to see this code.

This gets me the furthest (Note: I've changed the method and url combinations for these sets of tests also):

[DEBUG] Success: 401

[DEBUG] Failed: 401

 

I know this is an authorisation failure but i cannot find any documentation for how to include it.  I've tried the

Please login or register to see this link.

and various others.

 

I think I've tried every combination of everybody's code snippets in one way or another.  The documentation for this is a little lacking.... 

Please login or register to see this image.

/emoticons/default_icon_confused.gif" alt=":-?" />

 

My basic requirement:

To use the REST APIs because there seems to be much more [json] data available than using the Lua code alone (unless someone can suggest otherwise).  Before I can get to using this data, I need to know some first principles on how to GET it (think I should be okay to json.decode and use after that), and POST/PUT it, primarily from a scene, but possibly using a VD too which I believe maybe different.

 

Your help, documents, post-pointers etc. would be sooooooo much appreciated.  Thanks.

5 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Posted

    It always happens after I'm about to give up, and post something... well it looks like my first thanks go to the seemingly legend on here Krikroff for this post 

    Please login or register to see this link.

    .  I will eventually get around to using his Sonos VD too 

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" />

     

    I had to search via Google because this forum's search function is useless.

     

    If anybody stumbles across this post, I used Krikroff's code mixed with my previous, in a scene as follows:

    Please login or register to see this code.

    Also in case you're unfamiliar with the base64 encoding/encoding, use this site 

    Please login or register to see this link.

     to encode your 'username:password' (without quotes).

     

    Also, also, this may help generate some quick test URLs based on your own HC_IP and devices details 

    Please login or register to see this link.

     

    Using the testUrl I was able to get the following JSON from which I'll now start to have a real play

    Please login or register to see this code.

    I hope this helps some other newbie-to-Fibaro-lua.  I haven't tested it fully but the api's above work okay enough to give me confidence.

    • 0
    Posted

    If you want to do a call to HC2 you can use

    Please login or register to see this link.

    , without the need of authentication.

     

    so change 192.168.1.161:80 to 127.0.0.1:11111 and you can remove the header

    • 0
  • Inquirer
  • Posted

    If you want to do a call to HC2 you can use

    Please login or register to see this link.

    , without the need of authentication.

     

    so change 192.168.1.161:80 to 127.0.0.1:11111 and you can remove the header

     

    This simpler the better for me and this works perfectly thank you!

     

    As you're another legend on here boerremk and I have your attention, can you help me understand how I would get the status.data outside of the callback so I can use it outside of the request or success callbacks?  For example:

    Please login or register to see this code.

    How could I get that jtRes object (Lua table, or JSON prior to decoding to a Lua table) outside of the httpClient:request so I can use it further down in my code?  Your advice (or anyone's) would be very much appreciated.

     

    Thanks again.

    • 0
    Posted

    There is one problem with http calls and scenes and that is that is not waits till it ends, but the code continuous, so what you have to do is continue in your success part. I try to explain with some (stripped) beta code of a Parrot Flower Power:

    Please login or register to see this code.

    In the success part of the first function Authentication it is calling the next function getSensorData with data from the Authentication function, the getSensorData is again calling an other function, doing some stuff.

     

    I hope it is a little bit clear...

     

    It is different then what you should have done, if you program in other languages, put this is the way it works in scenes

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

     

    If you explain what you want and share you code I know for sure that we will make it work.

    • 0
  • Inquirer
  • Posted

    Thanks boerremk that makes perfect sense.

     

    I presume all of these foibles are Fibaro foibles and not Lua.  Nothing seems to work quite like it does in an open programming environment like Java, Python, JavaScript etc.  I'm sure I'll get there and thanks for the offer for and continue help.

    Join the conversation

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

    Guest
    Answer this question...

    ×   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...