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

get device ID in API


pos

Question

Hi

 

I have for a while used my own functions to do what is missing in the API. Now... I have for example code similar to this in both buttons and main section of VDs. It would be nice if someone could tell me if there has been added something  built-in like "getIDFromName" so I can remove such functions from my code and instead rely on Fibaro built in functions. Asking as I have a few VDs with big code bases. And I have hit the Fibaro max code base limit several times. So... To be able to extend the code, I have to make it smaller first....
 

 

function getidfromname(z)
HOST = Net.FHttp("127.0.0.1",11111);
response ,status, err = HOST:GET("/api/virtualDevices");
local jsonTable = json.decode(response);
for virtualIndex, virtualData in pairs(jsonTable) do
if (virtualData.type=="virtual_device") then
if virtualData.name == z then
return virtualData.id
end
end
end
return nil;
end

 

ID = virtualdeviceexist('vd_name_to_get_ID_from')

 

 

Tnx

Peo

 
Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

you can use single line, if name does not exists, you will get nil, if duplicate names exists, you will get the first one only

 

Please login or register to see this code.

 

Edited by tinman
Link to comment
Share on other sites

  • 0
  • Inquirer
  • Hi

     

    I tested this, and it works perfect.

     

    Many thanks @tinman

     

    This means I can slim my code.

     

    /Peo

    Link to comment
    Share on other sites

    • 0
    you welcome!
    The getDevicesId is very nice, one can set lot of filters, to catch really specific things.
    Btw, the other way is to replace all these

    Please login or register to see this code.

    with 

    Please login or register to see this code.

    note: api.xx deos json.decode for you as well

    • Thanks 1
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • This is also nice to know.

     

    But I liked your first suggestion better :)

     

    /Peo

    Link to comment
    Share on other sites

    • 0

    @pos

     

    "And I have hit the Fibaro max code base limit several times. "

     

    I experience sometimes strange problems at a fairly large piece of VD LUA code.
    1. What are the symptoms when you hit the code base limit?

    2. Is there a function to watch memoy usage during runtime?

     

    TIA

    Edited by BitBucket
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 1

    The code wont run when you hit save. And you will get no hints at all. I have no exact info. But mostly  somewhere between 1600-2000 rows of code. 

     

    If I remember correctly, @Sankotronic have written posts about this as well.

     

    2

    without hacking into the HC2, I only know the web gui memory meter...

    Link to comment
    Share on other sites

    • 0

    @pos

     

    1. OK, I am not that close. Good to know.
     

    2. Sankotronic has so many posts, I cannot find anything related.
        Do you remember some context or keywords?

    Link to comment
    Share on other sites

    • 0

    First you can see some of the limits here:

    Limit is in number of characters and not lines of code. There are several methods to shorten your code to squeeze more into this limited space:

    • At the beginning of code add this line: local hc = fibaro. After that you can write all Fibaro functions as hc:getValue or hc:startScene etc.
    • At the beginning of code rename global variables for example using local tod = "TimeOfDay". Then in code you can use short names like hc:getGlobalValue(tod)
    • Use code snippets or functions for repeating actions, try to split complex code to more simple routines and then use one routine for similar actions. This actually requires better explanation but I hope it is clear enough. More can be found in

      Please login or register to see this link.

    • If you use some larger tables of data then store them in global variables using separate code. This will considerably reduce amount of coding in other code that need those tables. For example HomeTable that keeps all devices and scenes ID numbers, or table of weather conditions etc.

     

    Hope this helps a bit.

    Enjoy coding! :-) 

     

    Edited by Sankotronic
    typos
    Link to comment
    Share on other sites

    • 0

    Hi Sankotronic,

     

    Thank you for your tips!
    Is there a LUA function or fibaro library call to watch memory usage during runtime from within the VD or Scene?

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