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

Working with tables and GlobalVars


Question

Posted

I am building this for my own educational purpose and got stuck thinking I was almost there.

I build a table that holds my devices and their id’s. The table tutorial helped a great deal.

Please login or register to see this image.

/uploads/monthly_2021_11/image.png.6d663f504fed1c1881de45b42d665435.png" />

can traverse through the table. I store the table via an encode into a GlobalVar. Which seem to succeed.

image.png.f142eb08d071839cbf907931bee9c34e.png

Next I do a Get on the GlobalVar which is successful (status 200), I get a table when I extract the response (response.value). 

 image.png.f71dd5df6da595c8bdd6d27583716689.png

But when I traverse with the “in pairs” construct I receive an error. Apparently, the response is now a string.

 

function getDeviceTable()

 

  response, status = api.get("/globalVariables/DeviceTable/",

    {

    name="DeviceTable"

    });

  

  fibaro:debug(status); 

  fibaro:debug(response);

  fibaro:debug(response.value);

 

  local table = response.value;

 

  --local jtable = json:decode(table);

  --fibaro:debug(jtable);

  

  for k,v in pairs(table) do

    print("first level key: " ..k..", first level value: ".. tostring(v))

    --for k2,v2 in pairs(v) do -- This works because we KNOW v is a table.

      --print(" second level key: " ..k2..", second level value: "..v2)

    --end;

  end;

 

 

 

If I json:decode response.value the outcome is the same.

So, how do I get a response from the GlobalVar that I can work with? 

Thanks for your help.

 

5 answers to this question

Recommended Posts

  • 0
Posted

You don't end the path with a slash or add the extra table value....

response, status = api.get("/globalVariables/DeviceTable/",

    {

    name="DeviceTable"

    });

You do 

response, status = api.get("/globalVariables/DeviceTable")

or simpler

fibaro.getGlobalVariable("DeviceTable") and you get the value directly - and then you json.decode the value.

  • 0
  • Inquirer
  • Posted

    Thanks! However, the code both give the same output and result. For whatever reason this picture did not make it into the post.

    Please login or register to see this image.

    /monthly_2021_11/image.png.97e3c228b23e330b99a3c9052df81a9a.png" />

    I just do not understand how to work with the output as I keep getting errors.

    Ron

    • 0
    Posted

    local table = response.value

    local jtable = json.decode(table)   --- json.decode, not json:decode

    for k,v in pairs(jtable) do   -- jtable, not table

        print("first level key: " ..k..", first level value: ".. tostring(v))

    end

     

    • Thanks 1
    • 0
  • Inquirer
  • Posted

    @jgabI am flabbergasted. Spend day and a half on this and it turned out I should use json.decode instead of json:decode. I tried so many variations ...

    So how does this work in terms of convention? I use fibaro:debug but must use json.decode; when to use a ":" and when a ".".

    But thanks a lot for your help .. small things do matter.

    • 0
    Posted
    51 minutes ago, RonS said:

    @jgabI am flabbergasted. Spend day and a half on this and it turned out I should use json.decode instead of json:decode. I tried so many variations ...

    So how does this work in terms of convention? I use fibaro:debug but must use json.decode; when to use a ":" and when a ".".

    But thanks a lot for your help .. small things do matter.

    Please login or register to see this code.

     

    : is used when calling function in an object oriented way, passing on the object as the first argument (self)

    On the HC2 fibaro used the : calling convention even though they really didn't leverage the objeeecet model. On the HC3 it's the dot notation again. fibaro being just a table with functions. You use : when  calling QuickApp functions  as QuickApp is a class that creates the runtime QuickApp object. Inside QuickApp function you refer to the  object with the variable 'self'

    You could also do

    Please login or register to see this code.

     

    but it's more elegant to do

    Please login or register to see this code.

     

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