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

Reading gas data SUPLA


ptekien

Question

Hello, can anyone help me to read the item data from the json table. When debugging, I see data but
I cannot display them in the label.

 

Json answer looks like this:

 

{"connected":true,"totalCost":11301.16,"pricePerUnit":1.3,"impulsesPerUnit":10,"counter":86932,"calculatedValue":8693.2,"currency":"PLN","unit":"m\u00b3"}

 

Please login or register to see this attachment.

 

 

 

-- Energy meter type have no actions to handle
-- To update energy consumption, update property "value" and "energy" with the same floating point number
-- Eg. 
-- self:updateProperty("value", 226.137) 
-- self:updateProperty("energy", 226.137)
-- To update controls you can use method self:updateView(<component ID>, <component property>, <desired value>). Eg:  
-- self:updateView("slider", "value", "55") 
-- self:updateView("button1", "text", "MUTE") 
-- self:updateView("label", "text", "TURNED ON") 
 
-- This is QuickApp inital method. It is called right after your QuickApp starts (after each save or on gateway startup). 
-- Here you can set some default values, setup http connection or get QuickApp variables.
-- To learn more, please visit: https://manuals.fibaro.com/
 
   function QuickApp:onInit()
    self.http = net.HTTPClient({timeout=3000})
 
    local address = 'https://svr24.supla.org/direct/931/ZjzzNhjATmYVdN/read'
    self.http:request(address, {
        options={
            headers = { 
                Accept = "application/json"
            },
            method = 'GET'
        },
        success = function(response)
            self:debug("response status:", response.status) 
            self:debug("headers:", response.headers["Content-Type"]) 
            local data = json.decode(response.data)  
 
            local apiResult = json.encode(response.data)
            apiResult = apiResult:gsub("%[", "") 
            apiResult = apiResult:gsub("%]", "") 
            self:debug("apiResult",apiResult)
 
            local jsonTable = json.decode(apiResult) 
            self:debug("jsonTable",jsonTable)
 
            -- Get the values
            local gaz_consumption = string.format("",jsonTable.calculatedValue)
            self:debug("Aktualne zużycie GAZU:",gaz_consumption.." m3")
        
 
        -- Update the labels
        self:updateView("label", "text", "Aktualne zużycie GAZU: " ..gaz_consumption .." m3")
            
 
        end,
        error = function(error)
            self:debug('error: ' .. json.encode(error))
        end
 
    }) 
end
 
Edited by ptekien
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
function dump(o)
    if type(o) == 'table' then
       local s = '{ '
       for k,v in pairs(o) do
          if type(k) ~= 'number' then k = '"'..k..'"' end
          s = s .. '['..k..'] = ' .. dump(v) .. ','
       end
       return s .. '} '
    else
       return tostring(o)
    end
 end
 
local gopa = '{"connected":true,"totalCost":11301.16,"pricePerUnit":1.3,"impulsesPerUnit":10,"counter":86932,"calculatedValue":8693.2,"currency":"PLN","unit":"m\\u00b3"}';
print(dump(json.decode(gopa)))
 

 

 

Please login or register to see this attachment.

local blabla = json.decode(gopa)
print(blabla["calculatedValue"])

 

 

 

8693.2

 

  • Thanks 1
Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks for your help @10der. Below, the entire code, if someone would like to change it, in the link variable we provide a direct link from the SUPLA cloud.

     

    -- Energy meter type have no actions to handle
    -- To update energy consumption, update property "value" and "energy" with the same floating point number
    -- Eg. 
    -- self:updateProperty("value", 226.137) 
    -- self:updateProperty("energy", 226.137)
    -- To update controls you can use method self:updateView(<component ID>, <component property>, <desired value>). Eg:  
    -- self:updateView("slider", "value", "55") 
    -- self:updateView("button1", "text", "MUTE") 
    -- self:updateView("label", "text", "TURNED ON") 
     
    -- This is QuickApp inital method. It is called right after your QuickApp starts (after each save or on gateway startup). 
    -- Here you can set some default values, setup http connection or get QuickApp variables.
    -- To learn more, please visit: https://manuals.fibaro.com/
     
       function QuickApp:onInit()
        self.http = net.HTTPClient({timeout=3000})
     
        local address = self:getVariable("link")
        self.http:request(address, {
            options={
                headers = { 
                    Accept = "application/json"
                },
                method = 'GET'
            },
            success = function(response)
                self:debug("response status:", response.status) 
                self:debug("headers:", response.headers["Content-Type"]) 
                local data = json.decode(response.data)  
     
                local apiResult = json.encode(response.data)
                apiResult = apiResult:gsub("%[", "") 
                apiResult = apiResult:gsub("%]", "") 
                self:debug("apiResult",apiResult)
     
                local jsonTable = json.decode(apiResult) 
                self:debug("jsonTable",jsonTable)
                function dump(o)
        if type(o) == 'table' then
           local s = '{ '
           for k,in pairs(o) do
              if type(k) ~= 'number' then k = '"'..k..'"' end
              s = s .. '['..k..'] = ' .. dump(v) .. ','
           end
           return s .. '} '
        else
           return tostring(o)
        end
     end
     
    local gopa = jsonTable;
    print(dump(json.decode(gopa)))
    local decode = json.decode(gopa)
    --print("Aktualne zużycie gazu:",decode["calculatedValue"]," m3")
     
                -- Get the values
                local gaz_consumption = string.format(decode["calculatedValue"])
                --self:debug("Aktualne zużycie:",gaz_consumption.." m3")
            
     
            -- Update the labels
            self:updateView("label1", "text", "Aktualne zużycie Gazu:  ".. gaz_consumption.."  m3")
                
     
            end,
            error = function(error)
                self:debug('error: ' .. json.encode(error))
            end
     
        }) 
    end

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @10der Maybe you could help me read the data from this answer:

     

    {"connected":true,"support":40959,"currency":"PLN","pricePerUnit":0.2,"totalCost":1024.65,"phases":[{"number":1,"frequency":50,"voltage":242.44,"current":0.749,"powerActive":95.82806,"powerReactive":-76.06786,"powerApparent":178.04242,"powerFactor":0.544,"phaseAngle":141.7,"totalForwardActiveEnergy":1203.26096,"totalReverseActiveEnergy":0,"totalForwardReactiveEnergy":0.8014,"totalReverseReactiveEnergy":526.2808},{"number":2,"frequency":50,"voltage":240.92,"current":1.499,"powerActive":261.24704,"powerReactive":-157.6632,"powerApparent":354.11944,"powerFactor":0.734,"phaseAngle":149.4,"totalForwardActiveEnergy":1760.55192,"totalReverseActiveEnergy":4.0e-5,"totalForwardReactiveEnergy":88.34716,"totalReverseReactiveEnergy":409.14398},{"number":3,"frequency":50,"voltage":240.99,"current":2.415,"powerActive":484.21536,"powerReactive":-238.9743,"powerApparent":570.62374,"powerFactor":0.848,"phaseAngle":153.7,"totalForwardActiveEnergy":2159.44256,"totalReverseActiveEnergy":0,"totalForwardReactiveEnergy":85.11984,"totalReverseReactiveEnergy":1010.6825}]}

     

    I would like to read the voltage and power from phases 1, 2, 3 and the total cost

    Link to comment
    Share on other sites

    • 0

    witam. 

     

    ctrl+C

    Please login or register to see this link.

    ctrl+V

    [PROCESS]

    [] - to jest array (aaa[1] aaa[2] ...)

     

    uczyс sie...

     

     

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