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

HC3 QuickApp JSON decode


Question

Posted

Hello,

I'm new to LUA and guessing or using manual is not really working as expected.

 

In the manual 

Please login or register to see this link.

, it give an example to post or get an http url and then decode the result, but it's not working for me, and I don't know what's wrong.
Most help I found on that is for HC2 and not really working/helping.

 

Please login or register to see this code.

On this, it debugs the "Rien"... so the if isn't working.

The result of the page (so the content) is :

Please login or register to see this code.

So I'm not sure about the "if data.contents and data.contents.quotes and data.contents.quotes[1] then"

If I debug the response.data,  I successfully see the line of the content {"result":"success","version":"1.0.1","mac":"00:00:00:00:00:00","position":100}

But after, it's a mystery !

 

Thanks

 

10 answers to this question

Recommended Posts

  • 0
Posted (edited)

@iS21 I would add some debugging statements for data and response.data and copy paste the content to and see how the structure looks like in

Please login or register to see this link.

 

Then you can see if data.contents, data.contents.quotes, data.contents.quotes[1] and data.contents.quotes[1].quote do exist. 
 

Perhaps some debugging statements for data.contents, data.contents.quotes, data.contents.quotes[1] and data.contents.quotes[1].quote and see how far the code runs. 
 

 

 

Edited by SmartHomeEddy
  • 0
  • Inquirer
  • Posted

    When I do self:debug(data) then I have

    table: 0x3fcdb20

    So, it's an array, no surprise, but then yes I don't know how to process. self:debug(data[0]) for example just end the script.

    Same for self:debug(data[0][1]) - I tried LOL 

    Also here is the result from json2table.com

     

    Please login or register to see this attachment.

    • 0
    Posted
    2 hours ago, iS21 said:

    if data.contents and data.contents.quotes and data.contents.quotes[1] then


    But somehow you expect the elements above in the table?

     

    Or is it an example? 
     

    The intention is to check if there is content and if so, do something, in this case add the value to the properties. 
     

     

    • 0
  • Inquirer
  • Posted

    This is the exact example in Fibaro's manual.

    What I need in my example would be the position value (here 100).

    That's also a good training for other devices later.

    • 0
    Posted

    Ahaaaaa

     

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted

    Oh my :) it's working !

    ok, I'll try to play with this knowledge now :)

    Thanks a lot!

    • 0
    Posted

    Your welcome

     

    You could use my Water Level Quickapp to experiment. It gets information from the internet, you don’t need any other device and it has a lot of QuickApp elements like a lot of fields you can retrieve, labels, QuickApp variables, updateProperty, updateView, etcetera. 
     


     

     

     

    • 0
    Posted
    4 hours ago, SmartHomeEddy said:

    @ iS21

    Please login or register to see this link.

     bych několik ladicích příkazů pro data a response.data a copy vložte obsah a podívejte se, jak vypadá struktura na

    Please login or register to see this link.

     

    Pak můžete zjistit, zda data.contents, data.contents.quotes, data.contents.quotes [1] a data.contents.quotes [1] .quote existují. 
     

    Možná některé ladicí příkazy pro data.contents, data.contents.quotes, data.contents.quotes [1] a data.contents.quotes [1] .quote a uvidíte, jak daleko kód běží. 
     

     

     

    @iS21

    Hi
    your problem has the same basis as here:

    You use a complete example from the manual. This example only works for a limited number of queries (after a while it stops returning results and you have no idea why it is simply restricted against overloading) and if I remember correctly, it resolves some voting preferences.

    Try to follow the advice in the note and modify the whole example for your needs.
    eM.

     

    Edit: aha, SmartHomeEddy has already helped you .. so maybe I am
    at least he clarified what it was with the example from the manual why it didn't work. As Eddy wrote, it did not contain the correct names of variables.

    • 0
  • Inquirer
  • Posted

    I have another dumb question. In a Scène, I can add easily a condition for the actual Profile.

    But is it possible to check this condition in the execution ?

    I want only 1 scene for a motion detection in the hall, to http:put a command to my hue bridge.

    But I would like to define the luminosity to different value, if at home or in sleeping mode, according to the profile.

    Is there a way to get the actual profile ID or name, in the script itself?

     

    • 0
    Posted (edited)

    the following code does not work - seems related to same HC manual example

     

     

    function QuickApp:turnOn()
        self:debug("binary switch turned on")
        self:updateProperty("value"true)
    end
     
    function QuickApp:turnOff()
        self:debug("binary switch turned off")
        self:updateProperty("value"false)    
    end



     
    function QuickApp:loop(text)
     
        self:debug("Text from loop:" , text)
      --self:debug("AAAAAAAAAAAAA") 
        local address = "http://server/json"
            self.http:request(address, {
            options={
                headers = { 
                    Accept = "application/json"
                },
                checkCertificate = true,
                method = 'GET'
            },
            success = function(response)
                self:debug("response status:", response.status) 
                self:debug("headers:", response.headers["Content-Type"]) 
                self:debug("response data:", response.data)
               local data = json.decode(response.data)   
                 self:debug(" data:", data)
           
               self:debug(" data.contents:", data.contents)  <-------------------------------------This is always empty  ; same if 
    I use instead of data = json.decode(response.data)     data = json.decode("{'value':'A' ,'value1':'B' }")  
     
    output 
    [QUICKAPP71]: Text from loop: 2
    [QUICKAPP71]: Text from loop: 3
    [QUICKAPP71]: response status: 200
    [QUICKAPP71]: headers: application/json
    [QUICKAPP71]: response data: {'value':'A' }
    [QUICKAPP71]: data: table: 0x33bf3220
     
     
     
     
     
                if data.contents and data.contents.quotes and data.contents.quotes[1then
                    local quote = data.contents.quotes[1].quote
                     self:debug("quote")
                    self:debug(quote)
                    self:updateView("label""text", quote) 
                end
            end,
            error = function(error)
                self:debug('error: ' .. json.encode(error))
            end
        }) 
           fibaro.setTimeout(1000function() 
            self:loop(text+1)
        end)
    end



     
    function QuickApp:onInit()
     --   ip = ""
        self:debug("onInit")
        self.http = net.HTTPClient() 
    --    self.baseUrl = "http://".. self:getVariable("ip")
        self:loop(1);
        self:debug("onInit out")
    end
    Edited by vifefref45

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