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

Darn Json


Question

Posted

Hi,

 

Can't find anywhere Json info for noobs:) So i might try here if someone would be kind enough to help me.

 

I make a http request to an external server from a VD and get a string like : "wall thermostat 0DB5D2, automatic, initialized, 23.7, 22.5 -> 22.5, panel unlocked, summer time",

 

Now i would like to get the "automatic", "23.7", and "initialized" values and write them into some global variables. I created the variables, predefined ones for the words and normal ones for the numbers in the variables panel.

Values should go like this in the variables:

Global Variable Temp - 23.7

Global Predefined Variable Mode - automatic

Global Predefined Variable Status - initialized

 

Now i got this far:

 

httpSession  = Net.FHttp("192.168.1.50", 8050)  
response ,status, errorCode = httpSession:GET("/get-set-temperature?cube=0192ac&device=0db5d2") 
 
jsonTable = json.decode(response)
fibaro:debug(response) 
 
The debug returns:  "expected value but found invalid token at character 1"
 
Can someone please help out? Or at lest point me to some sources where i can read about json ?
 
Thank you very much

10 answers to this question

Recommended Posts

  • 0
Posted

That's because it's not json. I looks like it is more like "comma separated values". Please try this:

 

Please login or register to see this code.

If you are new to programming, string matching can be daunting... I'd start with chapter 20 (and sub chapters) in the official lua manual

Please login or register to see this link.

It is quite possible that your source produces strings that are not correctly split by this function. You'd need a complete description of the data format to do a better job. For example, let's assume the the name of the thermostat contains a comma and a space... then it'll get split!

  • 0
  • Inquirer
  • Posted

    Hi, 

     

    Thank you for responding:) 

     

    This looks very close. looks like it is splitting the string "wall thermostat 0DB5D2, automatic, initialized, 23.7, 22.5 -> 22.5, panel unlocked, summer time"

     I can import the splits into variables just fine.

     

    But how do i change "wall thermostat 0DB5D2, automatic, initialized, 23.7, 22.5 -> 22.5, panel unlocked, summer time" with the actual string response from my http get command ?

     

    Regards

    • 0
  • Inquirer
  • Posted

    I think i got it

     

    httpSession  = Net.FHttp("192.168.1.50", 8050)  
    response = httpSession:GET("/get-set-temperature?cube=0192ac&device=0db5d2") 
    local string=response
    local split=splitComma(string)
     
    looks like it is working and update info according to changes

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> 
     
    Thank you very much Peter

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> 
    • 0
    Posted

    You're welcome! Code is OK... This is a bit cleaner: remove the line "local string=response" and then change the next line to "local split=splitComma(response)".

    • 0
  • Inquirer
  • Posted

    Thank you.

     

    Now as you have been so kind i have a another question about JSON for real now. 

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" />

    I can retrieve a json string from a bunch of devices.

     

    Regular json i can decode no problem and use the data in virtual devices and variables

    Here is a working example:

     

    httpSession  = Net.FHttp("192.168.1.50", 8050) 
    response = httpSession:GET("/get-status-json?cube=0172ba&device=0dad7d") 
    Device = json.decode(response)
     
    fibaro:call(222, "setProperty", "ui.Label1.value", Device.temperature.. " ")
    fibaro:call(222, "setProperty", "ui.Label2.value", Device.set_temperature.. " ")
    fibaro:call(222, "setProperty", "ui.Label3.value", Device.mode.. " ")
     
    fibaro:setGlobal('LivingActualTemp', Device.temperature)
    fibaro:setGlobal('LivingSetTemp', Device.set_temperature)
    fibaro:setGlobal('LivingMode', Device.mode)
     
    The json string was { "address":896381, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"wall thermostat", "set_temperature":22.5, "new_temperature":22.5, "temperature":23.0}
     
     
    So far so good:
    BUT i got also a json string which is multi layered, it has the string above repeating for about 10 times with different values (different devices).
    I checked the string in jsonprettyprint and the result is below.  Do you know how could i decode the multi layer JSON please ?
    The approach i took above does not work for this. I think i need somehow to split into smaller tables.
     
     
    Regards
     
     
    {
    "devices": [
    {
    "address": 838603,
    "error": false,
    "initialized": true,
    "battery_low": false,
    "link_error": false,
    "panel_locked": false,
    "gateway_known": true,
    "summer_time": true,
    "mode": "automatic",
    "type": "wall thermostat",
    "set_temperature": 22.5,
    "new_temperature": 22.5,
    "temperature": 23.1
    },
    {
    "address": 585992,
    "error": false,
    "initialized": true,
    "battery_low": false,
    "link_error": false,
    "panel_locked": true,
    "gateway_known": true,
    "summer_time": true,
    "mode": "automatic",
    "type": "radiator thermostat plus",
    "set_temperature": 22.5,
    "new_temperature": 22.5,
    "valve": 0
    },
    {
    "address": 839104,
    "error": false,
    "initialized": true,
    "battery_low": false,
    "link_error": false,
    "panel_locked": false,
    "gateway_known": true,
    "summer_time": true,
    "mode": "automatic",
    "type": "wall thermostat",
    "set_temperature": 23,
    "new_temperature": 23,
    "temperature": 23
    }
    ]
    }
    • 0
    Posted

    Can you please try this and tell me if this helps?

     

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted

    Thank you for replying

    Please login or register to see this image.

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

     

    Seems to be working:

     

    result is:

     

    [DEBUG] 22:49:31: First device address: 838603
    [DEBUG] 22:49:31: Third device set_temperature: 22.5
    [DEBUG] 22:49:31: Number of devices in data: 27
    [DEBUG] 22:49:31: Device # 1, temperature: 23
    [DEBUG] 22:49:31: Device # 2, temperature: not defined
    [DEBUG] 22:49:31: Device # 3, temperature: 23.9
    [DEBUG] 22:49:31: Device # 4, temperature: not defined
    [DEBUG] 22:49:31: Device # 5, temperature: 23.6
    [DEBUG] 22:49:31: Device # 6, temperature: not defined
    [DEBUG] 22:49:31: Device # 7, temperature: 21.5
    [DEBUG] 22:49:31: Device # 8, temperature: not defined
    [DEBUG] 22:49:31: Device # 9, temperature: not defined
    [DEBUG] 22:49:31: Device # 10, temperature: not defined
    [DEBUG] 22:49:31: Device # 11, temperature: 23.5
    [DEBUG] 22:49:31: Device # 12, temperature: not defined
    [DEBUG] 22:49:31: Device # 13, temperature: not defined
    [DEBUG] 22:49:31: Device # 14, temperature: not defined
    [DEBUG] 22:49:31: Device # 15, temperature: not defined
    [DEBUG] 22:49:31: Device # 16, temperature: not defined
    [DEBUG] 22:49:31: Device # 17, temperature: not defined
    [DEBUG] 22:49:31: Device # 18, temperature: not defined
    [DEBUG] 22:49:31: Device # 19, temperature: not defined
    [DEBUG] 22:49:31: Device # 20, temperature: not defined
    [DEBUG] 22:49:31: Device # 21, temperature: 22.8
    [DEBUG] 22:49:31: Device # 22, temperature: 23
    [DEBUG] 22:49:31: Device # 23, temperature: not defined
    [DEBUG] 22:49:31: Device # 24, temperature: 23.5
    [DEBUG] 22:49:31: Device # 25, temperature: not defined
    [DEBUG] 22:49:31: Device # 26, temperature: not defined
    [DEBUG] 22:49:31: Device # 27, temperature: not defined

     
     
     
    My entire json looks like
     
    {"devices" : [{ "address":838603, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"wall thermostat", "set_temperature":22.5, "new_temperature":22.5, "temperature":23.2} ,{ "address":585992, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":22.5, "new_temperature":22.5, "valve":0} ,{ "address":839104, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"wall thermostat", "set_temperature":23.5, "new_temperature":23.5, "temperature":23.9} ,{ "address":725066, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"radiator thermostat plus", "set_temperature":23.5, "new_temperature":23.5, "valve":0} ,{ "address":839347, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"wall thermostat", "set_temperature":23.5, "new_temperature":23.5, "temperature":23.7} ,{ "address":725051, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"radiator thermostat plus", "set_temperature":23.5, "new_temperature":23.5, "valve":0} ,{ "address":898519, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"wall thermostat", "set_temperature":12.0, "new_temperature":12.0, "temperature":21.5} ,{ "address":911590, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"radiator thermostat plus", "set_temperature":12.0, "new_temperature":12.0, "valve":0} ,{ "address":913681, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"radiator thermostat plus", "set_temperature":12.0, "new_temperature":12.0, "valve":0} ,{ "address":587075, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":23.0, "new_temperature":23.0, "valve":0} ,{ "address":898514, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"wall thermostat", "set_temperature":20.0, "new_temperature":20.0, "temperature":23.5} ,{ "address":911148, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":585945, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":587259, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":873235, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":587095, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":904323, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":895168, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":913025, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":571943, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":20.0, "new_temperature":20.0, "valve":0} ,{ "address":896381, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"wall thermostat", "set_temperature":20.0, "new_temperature":20.0, "temperature":22.8} ,{ "address":839055, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"wall thermostat", "set_temperature":22.5, "new_temperature":22.5, "temperature":23.0} ,{ "address":725031, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"manual", "type":"radiator thermostat plus", "set_temperature":22.5, "new_temperature":22.5, "valve":0} ,{ "address":763440, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":false, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"wall thermostat", "set_temperature":23.0, "new_temperature":23.0, "temperature":23.5} ,{ "address":728416, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":23.0, "new_temperature":23.0, "valve":0} ,{ "address":728609, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":23.0, "new_temperature":23.0, "valve":0} ,{ "address":586933, "error":false, "initialized":true, "battery_low":false, "link_error":false, "panel_locked":true, "gateway_known":true, "summer_time":true, "mode":"automatic", "type":"radiator thermostat plus", "set_temperature":23.0, "new_temperature":23.0, "valve":0} ]}
    • 0
  • Inquirer
  • Posted

    A first issue would be that the battery_low and link_error the debug sees as not defined instead of true or false.

     

    Regards 

    • 0
    Posted

    If you are certain the boolean fields always exist, then use this to print their value (in the "for loop"):

    Please login or register to see this code.

    Or

     

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted

    Peter you are a wizard

    Please login or register to see this image.

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

     

    I even added some notifications like

     

     if v.battery_low then
        
        fibaro:call(156, 'sendPush', "Device # "..k..", BATTERY LOW")
        local subject = "Device # "..k..", BATTERY LOW"
        local message = "Device # "..k..", BATTERY LOW CHANGE BATTERIES !"
        fibaro:call(2, "sendEmail", subject, message)
        
        fibaro:debug("Device # "..k..", Battery Low")
      else
        fibaro:debug("Device # "..k..", Battery OK")
      end
     
    Now a trick question

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />
     
    It can happen that my http server stops or computer reboots or whatever. In that case as far as i tested the entire LUA script (which is stored in the main loop of a VD) just stops.
     
    Is there any way to restart the loop after some time? That would also work in case that some devices loose radio link and restart is needed. 
     
    I have added some number of devices check like below and it works.  But how to include some restart function ?
     
    Numberofdevices = #decoded.devices
    i = 1
    if (Numberofdevices ~= 27) then
        fibaro:debug('At least one device lost radio link !');
        fibaro:call(156, 'sendPush', 'At least one device lost radio link !')
        local subject = "At least one device lost radio link !"
        local message = "Check all the thermostats at once !"
        fibaro:call(2, "sendEmail", subject, message)
        i = i + 1
    else
        fibaro:debug("Number of devices in data: "..#decoded.devices)
    end
     
     
    Regards

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