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

Decoding JSON


ROBBshop

Question

Hi everyone,

I am trying to pull a few values out of a json response from another source. I already found many examples but they don't seem to work for me.

The LUA script so far:

Please login or register to see this code.

I want to pull 3 values marked in bold. I know it should become somthing like this, but not sure how exact:

current_usage = response.body[1].states[2].value[4] (or am I completely wrong here??)

current_total = response.body[1].states[2].value[??]

current_gas = response.body[1].states[2].value[??]

The unsorted json response:

Please login or register to see this code.

Any help is welcome

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

I haven't played yet with JSON on HC2 so this might not work but it looks to me like what you want is:

# Watts

current_usage = response.body[1].Device_Num_77.states[0].value

# KWH

current_total = response.body[1].Device_Num_77.states[2].value

# GAS

current_gas = response.body[1].Device_Num_77.states[3].value

Hope it's correct

Please login or register to see this image.

/emoticons/default_icon_smile.gif" alt=":-)" />

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Unfortunatly does not seem to work. Is there a way to debug a bit more which actual value this is pulling?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • errrrr, paste this into my HC2? Or how do I parse the body there?

    Link to comment
    Share on other sites

    • 0

    I moved bold tags inside the quotes to have a working JSON bit.

    Then here:

    Please login or register to see this link.

    Proves I should be right

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Ok, So apart from my attempt to make a bold number (hence the code) the problem may be that it is in quotes when trying to write it to a variable....

    Guess more stripping required then

    [ Added: 2014-01-14, 15:14 ]

    but with or without body?

    current_usage = response.body[1].Device_Num_77.states[0].value;

    or

    current_usage = response.Device_Num_77.states[0].value;

    Please login or register to see this code.

    does not write the any value

    Link to comment
    Share on other sites

    • 0

    fibaro:debug(response.body[1].Device_Num_77.states[0].value)

    fibaro:debug(response.body.Device_Num_77.states[0].value)

    fibaro:debug(response.Device_Num_77.states[0].value)

    Try them all

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • First 2 attempts:

    [ERROR] 15:32:14: line 6: attempt to index field 'body' (a nil value)

    Last attempt:

    [ERROR] 15:34:08: line 7: attempt to index field '?' (a nil value)

    So still a no joy

    Please login or register to see this image.

    /emoticons/default_sad.png" alt=":(" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    [ Added: 2014-01-14, 16:35 ]

    So, I created a virtual device and assigned 1 button. In this button I programmed this LUA:

    Please login or register to see this code.

    Unfortunatly there is no debug option in here and it does not change the number in the variable panel. If I use another value:

    Please login or register to see this code.

    It correctly updates the value to 1.

    Not sure where the difference is .......

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Maybe the index starts from 1, then:

    fibaro:debug(response.Device_Num_77.states[1].value)

    BINGO!!!!!!!!!!!!!

    Thank a gazillion!!!!!

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Now that I have it working (sort of) I want to clean up my code so that I can share it to others and have them easily customize it.

    The original code:

    Please login or register to see this code.

    The goal:

    Please login or register to see this code.

    Most of it actually does work, except the pasring of the JSON_WATTS/JSON _GAS bits.

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