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

How jtable replace hardcoded value into a variable


Question

Posted

Simple statement where I update the led temperature of a lightbulb.

 

The first works like a charm.

jtable = '{"properties":{"parameters":[{"id":81,"size":2,"value":4999}]}}'

HC2:PUT("/api/devices/240",jtable);

 

 

[DEBUG] 13:28:04: {"properties":{"parameters":[{"id":81,"size":2,"value":4999}]}}
 

 

Now I want to have the value 2700 in a variable so I can dynamically update the bulb.


jtable = '{"properties":{"parameters":[{"id":81,"size":2,"value":TempKelvin}]}}' or

jtable = '{"properties":{"parameters":[{"id":81,"size":2,"value": ".. TempKelvin .."}]}}'

 

 

[DEBUG] 13:29:02: {"properties":{"parameters":[{"id":81,"size":2,"value":TempKelvin}]}}

[DEBUG] 13:30:45: {"properties":{"parameters":[{"id":81,"size":2,"value":".. TempKelvin .."}]}}
 

and whatever I have tried it does not replace the variable with its value.

Any suggestions?

Ron

 

 

6 answers to this question

Recommended Posts

  • 0
Posted (edited)

Yes, just work with pure jSon table array and then encode it.

 

jtable={

  properties={

                        {id=81,size=2,value=TempKelvin}

                        }

         }

now

HC2:PUT("/api/devices/240",json.encode(jtable));

Much easier to understand and deal with that and not trying to convert jSon table to string manually.

Edited by cag014
  • 0
  • Inquirer
  • Posted

    Thanks @cag014. This idea works. 

     

    [DEBUG] 11:19:47: Temp:3919
    [DEBUG] 11:19:47: {"properties":[{"id":81,"value":3919,"size":2}]}
     

    However, the order of size and value has been swapped. Can you enforce a specific order?

    Ron

    • 0
    Posted (edited)
    24 minutes ago, RonS said:

    Thanks @cag014. This idea works. 

     

    [DEBUG] 11:19:47: Temp:3919
    [DEBUG] 11:19:47: {"properties":[{"id":81,"value":3919,"size":2}]}
     

    However, the order of size and value has been swapped. Can you enforce a specific order?

    Ron

    Not by default.

    json.encode(), pairs() functions return values in the table, in an arbitrary order, but it doesn't have any impact on functionality.

    (Some extra code could be applied to keep the order.)

     

    Edited by cag014
    • 0
    Posted
    22 hours ago, RonS said:

    Simple statement where I update the led temperature of a lightbulb.

    and whatever I have tried it does not replace the variable with its value.

    Any suggestions?

    Ron

     

    So, I would recommend @cag014 advise to create a Lua table - and the order of the elements in the json representation doesn't matter for the code accepting the structure - it could affect the readability though.

     

    The reason your two first attempts failed is problem with how you construct the json string

    Please login or register to see this code.

    This doesn't replace the TempKelvin variable as it is inside the string - and the json expression is malformed so the API called probably just rejects the call.

    Please login or register to see this code.

    This has the same issue, everything is between the outer single quotes ' .... ' (see how the whole string is green according the the Lua style formatter in the post?)

    This works:

    Please login or register to see this code.

    See how TempKelvin is black according the the Lua style formatter in the post.

     

    If you absolutely want to create a json string with a specific order, you have to write your own json.encoder ;-) 

    The reason why one would want to enforce the key order in a json string (a stable order) is when you write out Lua tables with json.encode for debugging, and you want them to look the same so you don't have to search the printout for the fields that could appear randomly...

    In my own code I deal with "sourceTriggers" a lot, tables with a 'type' field and a 'deviceID' field that I want to have first in all json.encoded strings.

    Looks like this

    Please login or register to see this code.

    The other advantage is that it is a bit more forgiving than the the regular json.encoder - The drawback is that it may not cater for all esoteric encoding formats...

    • Like 2
    • 0
  • Inquirer
  • Posted

    @cag014 @jgab Thanks a lot for your contributions. Indeed the order does not prevent the execution but I was just curious and indeed in some cases it does matter. Also thanks for the explanation. Now it does make sense and will help along the way. Ron.

    • 0
    Posted (edited)

    I have maybe the same issue here.

    in the HC2 i have a coolcam sirene alarm.

    in a VD i had the possibility to change sound, volume etc.

    now i like to do that in my HC3 with an QA.

    in the HC2 the code is:

     

    local selfId = fibaro:getSelfId();
    local port = fibaro:get(selfId, 'TCPPort');

    HC2 = Net.FHttp("127.0.0.1", 11111)

    jtable = '{"properties":{"parameters":[{"id":2,"size":1,"value":3}]}}'
    HC2:PUT("/api/devices/" ..port ,jtable)

     

     when i put this in an QA in HC3 i got the message 

    [ERROR] [QUICKAPP256]: QuickApp crashed

    [ERROR] [QUICKAPP256]: Unknown error occurred: handleJsonRpc

    can anyone help me so i understand this problem.

     

    thanks

     

    Edited by RobS

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