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

Converting response data to variable


Symbiot78

Question

Hi

 

I'm trying to figure out how to use some response data. I want to convert specific data to a variable.

 

I request data from a URL

I get the below response.

I want to get the following into a variable:

shadeId

position

 

so the result is that I know that shadeId 50173 is in position1:65483

 

 

 

{"shadeIds":[50173,40999,7342],"shadeData":[{"id":50173,"type":55,"capabilities":4,"batteryKind":1,"smartPowerSupply":{"status":0,"id":0,"port":0},"batteryStatus":0,"batteryStrength":0,"roomId":59205,"firmware":{"revision":1,"subRevision":8,"build":1944},"name":"U3R1ZSBMYW1lbA==","signalStrength":0,"motor":{"revision":50,"subRevision":48,"build":11826},"positions":{"posKind1":1,"position1":65483,"posKind2":3,"position2":400},"groupId":11973},{"id":40999,"type":54,"capabilities":4,"batteryKind":1,"smartPowerSupply":{"status":0,"id":0,"port":0},"batteryStatus":0,"batteryStrength":0,"roomId":36806,"firmware":{"revision":1,"subRevision":8,"build":1944},"name":"S0EgQWxrb3Zl","signalStrength":0,"groupId":6735,"motor":{"revision":50,"subRevision":48,"build":11826},"positions":{"posKind1":1,"position1":65534,"posKind2":3,"position2":58360}},{"id":7342,"type":55,"capabilities":4,"batteryKind":1,"smartPowerSupply":{"status":0,"id":0,"port":0},"batteryStatus":0,"batteryStrength":0,"roomId":36806,"firmware":{"revision":1,"subRevision":8,"build":1944},"name":"S0EgTlkgVsOmZw==","positions":{"posKind1":1,"position1":65535,"posKind2":3,"position2":20},"signalStrength":4,"groupId":45116,"motor":{"revision":50,"subRevision":48,"build":11826}}]}

 

The code I run in a scene is:

 
local url = "http://10.20.10.17/api/shades"
net.HTTPClient():request(url, {
    options={
      headers = {
        ['Content-Type'] = 'application/json',
      },
      method = 'GET',
    },
    success = function(response) 
            print(response.status)
            print(response.data)
            --print(response.data.shadeIds)
            end,
    error = function(message)
            print("error:", message)
    end
})
Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

Please login or register to see this code.

You need to create fibaro globals with name

Shade50173

Shade40999

Shade7342

Edited by jgab
Link to comment
Share on other sites

  • 0

 

8 hours ago, jgab said:

You need to create fibaro globals with name

@

Please login or register to see this link.

 

What are the advantages setting global versus locals .

Please login or register to see this code.

@

Please login or register to see this link.

you should always use a json pretty printer to know the exact path to your data.

Just paste the entire json response into

Please login or register to see this link.

 and run it.

Edited by mike79
Link to comment
Share on other sites

  • 0
44 minutes ago, mike79 said:

 

@

Please login or register to see this link.

 

What are the advantages setting global versus locals .

Please login or register to see this code.

@

Please login or register to see this link.

you should always use a json pretty printer to know the exact path to your data.

Just paste the entire json response into

Please login or register to see this link.

 and run it.

It was just that he wanted it in fibaro globals.

The data returned has info about several shades and their positions and they need to be stored somewhere I guess.

Maybe he has other scenes/QAs that needed the values?

The function saveData can be inlined in the success function instead of calling a separate functions - just a matter of taste.

Link to comment
Share on other sites

  • 0
  • Inquirer
  • @jgab - this is beautiful.

    What I'd like is to be able to use the data.

    It's to control my shades in a scene. So open @ 8 close @ 2100 for instance.

    Then have a button or similar that I can click and then the shades close or open. But 1 click should control both open & close. So if shade is closed.. open.. and vice versa.

    So I'd need to get the position of the shade/shades first and then act upon that info. 

    A scene that updates a variable every X minutes will probably be faster than reading the data directly from the powerview controller each time I click the button.. or ? 

    Link to comment
    Share on other sites

    • 0
    14 hours ago, Symbiot78 said:

    @jgab - this is beautiful.

    What I'd like is to be able to use the data.

    It's to control my shades in a scene. So open @ 8 close @ 2100 for instance.

    Then have a button or similar that I can click and then the shades close or open. But 1 click should control both open & close. So if shade is closed.. open.. and vice versa.

    So I'd need to get the position of the shade/shades first and then act upon that info. 

    A scene that updates a variable every X minutes will probably be faster than reading the data directly from the powerview controller each time I click the button.. or ? 

     

    How i do it for now. Maybe this is not exactly what you want. But works perfect.

    Well, i just bougt a 8 button in wall switch. That fits nicely next to my normal wall switches.

    For now i made 12 LUA scene's - 4 for the left row, 4 for the right row, 4 for the left row(long press) ( got 2 shades next to each other).

    So when i press the firts right button, the shade on the right goes to that posittion. The first on the left does the same for the other,

    When i long press the first left button, both go to the same position. Those are scene's made on the powerhub itself.

     

    I can make easely a scene that tells the powerview hub to run a certain scene at a specific time, or when there is higher lux then 10.000

    Please login or register to see this image.

    Edited by ppeterr
    Link to comment
    Share on other sites

    • 0
    1 hour ago, ppeterr said:

    For now i made 12 LUA scene's 

     

    you could of course made one scene, with all triggers (example for one button, all trigger possibilities - for different button simply change keyId. The 147 is the id of my Scene Controller device)

     

    Please login or register to see this code.

     

    and evaluate the sourceTrigger to run specific actions when something has been triggered (again only one button here, for more add some extra elseif statements and change keyid)

     

    Please login or register to see this code.

     

     

     

    • Like 1
    • Thanks 1
    Link to comment
    Share on other sites

    • 0
    37 minutes ago, tinman said:

     

    you could of course made one scene, with all triggers (example for one button, all trigger possibilities - for different button simply change keyId. The 147 is the id of my Scene Controller device)

     

     

     

    Thanks for this, for sure much better to have one scene for that specific decive.

    I'll try to make this one day, good practice also :)

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • On 7/13/2021 at 6:57 PM, mike79 said:

     

    @

    Please login or register to see this link.

     

    What are the advantages setting global versus locals .

    Please login or register to see this code.

    @

    Please login or register to see this link.

    you should always use a json pretty printer to know the exact path to your data.

    Just paste the entire json response into

    Please login or register to see this link.

     and run it.

    Hi @jgab

     

    could you please explan this:

     

    Please login or register to see this code.

     

    so shadeData I see in the json2table

    but [2] ?

    I thought it would be 0 for 50173

     

    Please login or register to see this attachment.

     

    Edited by Symbiot78
    Link to comment
    Share on other sites

    • 0
    10 hours ago, Symbiot78 said:

    Hi @jgab

     

    could you please explan this:

    Please login or register to see this code.

    so shadeData I see in the json2table

    but [2] ?

    I thought it would be 0 for 50173

     

    local shader1 = bob.shadeData[2].positions[1].position1

    was from @mike79's example and I don't know why he choose index 2.

     

    In Lua all array indexes start on 1 which means that 50173 is in bob.shadeData[1] in the example.

    If you go to a json viewer on the web they may have a '0' index as it is the way in ex. javascript.

     

    We can convert all json expression to Lua tables - but we can't convert all Lua tables to json expressions.

    Correction: we can't convert all json expression to Lua tables either - any guesses?

    Edited by jgab
    Link to comment
    Share on other sites

    • 0

    It was a example howto get data, the right positions wasent part of it.

    but position 0 in table is [1] 

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @jgab - bringing this back to life a little bit.

    I found out that the API doesn't agree with the way we try to get the shades to refresh. 

    we need to tell the hub which shades we want to refresh.

    so for each shade we need to call the refresh like this:

     

    local url = "http://10.20.10.17/api/shades/40999?refresh=true"

     

    Can you help me rebuild the http call so it does what is needed. 

    I tried splitting the function into 3 (I have 3 shades) http calls but I struggle to get the data to update.

     

    fibaro.setGlobalVariable("ShadePos"..data.id,tostring(data.positions.position1))  <--- this doesn't work either.

     

    This for instance doesn't seem to work. I cannot get data from 's'. I just get an empty error. I've tried multiple ways of getting the data out but nothing seems to work.

     

    -- 0 = alkove = 40999
    -- 1 = ny = 7342
    -- 2 = stue = 50173
    fibaro.debug("SCENE236""alkove")
    local url = "http://10.20.10.17/api/shades/40999?refresh=true"
    local function saveData(data)
      data = json.decode(data)  
      for _,s in ipairs(data.shadeData) do
        fibaro.Debug(SCENE236, s)
      end
      end
     
    net.HTTPClient():request(url, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)            
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
                
                fibaro.debug("SCENE236""error 123")
        end
    })

    I had this running - which seemed to work - but now it does not. I know the for _,s is pointless as there is only data from 1 shade each time but it worked.. so didn't change anything.. 

     

    -- 0 = alkove = 40999
    -- 1 = ny = 7342
    -- 2 = stue = 50173




     
    fibaro.debug("SCENE154""alkove")
    local url0 = "http://10.20.10.17/api/shades/40999?refresh=true"
    local function saveData(data)
      data = json.decode(data)
      for _,s in ipairs(data.shadeData) do
        fibaro.setGlobalVariable("ShadePos"..s.id,tostring(s.positions.position1))
      end
    end
    fibaro.debug("SCENE154""NY")
    local url1 = "http://10.20.10.17/api/shades/7342?refresh=true"
    local function saveData(data)
      data = json.decode(data)
      for _,s in ipairs(data.shadeData) do
        fibaro.setGlobalVariable("ShadePos"..s.id,tostring(s.positions.position1))
      end
    end
    fibaro.debug("SCENE154""stue")
    local url2 = "http://10.20.10.17/api/shades/50173?refresh=true"
    local function saveData(data)
      data = json.decode(data)
      for _,s in ipairs(data.shadeData) do
        fibaro.setGlobalVariable("ShadePos"..s.id,tostring(s.positions.position1))
      end
    end
     
    net.HTTPClient():request(url0, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
        end
    })
     
    fibaro.sleep(15000)
     
    net.HTTPClient():request(url1, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
        end
    })
     
    net.HTTPClient():request(url2, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
        end
    })
     
    fibaro.debug("SCENE154", response)
    fibaro.debug("SCENE154", message)
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I made .... something... work :)

     

    the below runs and updates as I want it.. it's probably not even close to pretty..

     

     

     

    -- 0 = alkove = 40999
    -- 1 = ny = 7342
    -- 2 = stue = 50173




     
    fibaro.debug("SCENE154""alkove")
    local url0 = "http://10.20.10.17/api/shades/40999?refresh=true"
    local function saveData(data)
      data = json.decode(data)
        s = data.shade
        fibaro.setGlobalVariable("ShadePos"..s.id,tostring(s.positions.position1))
    end
    fibaro.debug("SCENE154""NY")
    local url1 = "http://10.20.10.17/api/shades/7342?refresh=true"
    local function saveData(data)
      data = json.decode(data)
        s = data.shade
        fibaro.setGlobalVariable("ShadePos"..s.id,tostring(s.positions.position1))
    end
    fibaro.debug("SCENE154""stue")
    local url2 = "http://10.20.10.17/api/shades/50173?refresh=true"
    local function saveData(data)
      data = json.decode(data)
        s = data.shade
        fibaro.setGlobalVariable("ShadePos"..s.id,tostring(s.positions.position1))
    end
     
    net.HTTPClient():request(url0, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
        end
    })
     
    fibaro.sleep(15000)
     
    net.HTTPClient():request(url1, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
        end
    })
     
    net.HTTPClient():request(url2, {
        options={
          headers = {
            ['Content-Type'] = 'application/json',
          },
          method = 'GET',
        },
        success = function(response) 
                print(response.status)
                saveData(response.data)
                end,
        error = function(message)
                print("error:", message)
        end
    })
     
    fibaro.debug("SCENE154", response)
    fibaro.debug("SCENE154", message)
    Link to comment
    Share on other sites

    • 0

    I guess it works, but the problem is that you don't get the sequential behaviour you expect.

    In effect it will run through the whole scene incl. sleeps and logs and then after that, it will run the http requests.

     

    Structuring it this way will chain the http requests getting the sequential behaviour.

     

    Please login or register to see this code.

     

    Edited by jgab
    • Like 1
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi @jgab - thx for this. 

    I set up a new scene to test.

     

    what is the first part.. where it loads a local file? is that something I should omit?

     

    Anyway.. with.. or without it.. I get an error.. and this is one of the things that annoy me in hc3/lua.. it's just empty.. no error message or anything.. just [SCENE238]: 

     

    it was the same before I made the other scene work.. just empty error message.. of course I realise.. if I knew more about lua I would be able to catch these errors .. but alas..  :-)

     

    I get no response from:

     

    debug("ShadePos%s = %s",s.id,s.positions.position1)
    Link to comment
    Share on other sites

    • 0

    You can ignore the "local _..." it's ignored on the HC3 and is for my offline emulator where I test stuff.

    I may have miss-typed the tag for the debug statement - try the edited code above again. I now declare the tag to "SCENE154"

     

    Is it a scene or a QA you develop?

    Edited by jgab
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi @jgab

     

    It works now :-) 

    I am not developing a QA.. I did start on one at one point but moved away from that idea. 

     

    Basically the above scene just updates the state/positions of powerview curtains. If you move your curtains outside the app - the hub doesn't really update the positions.

    generally the powerview / hunter douglas hub is crap. You have to be careful sending it too many commands at the same time. If you do.. it does and needs a reboot.

     

    I have a logic-group matrix 4-button zwave unit that I've set up to control the 3 curtains I have. the Matrix has it's own scene where I do stuff based on what button you click - how many times - if you hold it. Each scenario then calls another scene to manipulate the curtains and a scene to update the positions. (the one above).

    I've then also set the update scene to run periodically.

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