Logic Machine Forum
JSON items into groups - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: JSON items into groups (/showthread.php?tid=5836)



JSON items into groups - alexll - 13.01.2025

Hi there,

I'm working with Airzone local API, and I need to write every item into LogicMachine group objects.

This is my script:

Code:
require 'ltn12'
require 'socket.http'
json = require("json")

local request_url = "http://192.168.1.111:3000/api/v1/hvac?systemid=01&zoneid=15"
local response_body = {}

local body, code, hdrs, stat = socket.http.request
  {
    url = request_url;
    sink = ltn12.sink.table(response_body);
  }

log (response_body)
 
And this is the response I get:

Code:
* table:
[1]
  * string: {
    "data":    [{
            "systemID":    1,
            "zoneID":    15,
            "name":    "ZONA 15",
            "thermos_type":    2,
            "thermos_firmware":    "4.53",
            "thermos_radio":    0,
            "on":    1,
            "double_sp":    0,
            "coolsetpoint":    22,
            "coolmaxtemp":    30,
            "coolmintemp":    15,
            "heatsetpoint":    22,
            "heatmaxtemp":    30,
            "heatmintemp":    15,
            "maxTemp":    30,
            "minTemp":    15,
            "setpoint":    22,
            "roomTemp":    22.9,
            "sleep":    0,
            "temp_step":    0.5,
            "mode":    3,
            "speed":    0,
            "coldStage":    2,
            "heatStage":    2,
            "coldStages":    2,
            "heatStages":    2,
            "humidity":    22,
            "units":    0,
            "errors":    [],
            "air_demand":    0,
            "floor_demand":    0,
            "cold_demand":    0,
            "heat_demand":    0,
            "heatangle":    0,
            "coldangle":    0,
            "master_zoneID":    15,
            "eco_adapt":    "off",
            "antifreeze":    0
        }]
}

I'm a newbie at json stuff. Please, can you help me with writing every item (systemID, zoneID, name, etc) into LogicMachine group objects? Thanks in advance!


RE: JSON items into groups - RomansP - 14.01.2025

Hi alexll

Here is a script

Code:
require('socket.http')
require('json')

url = 'http://192.168.1.111:3000/api/v1/hvac?systemid=01&zoneid=15'
response = socket.http.request(url)

--log (response)

decoded = json.pdecode(response)

--log(decoded)

status = decoded.data[1]

grp.checkwrite('50/1/4', status.systemID)
grp.checkwrite('50/1/5', status.zoneID)
grp.checkwrite('50/1/6', status.name)



RE: JSON items into groups - alexll - 14.01.2025

(Yesterday, 07:58)RomansP Wrote: Hi alexll

Here is a script

Code:
require('socket.http')
require('json')

url = 'http://192.168.1.111:3000/api/v1/hvac?systemid=01&zoneid=15'
response = socket.http.request(url)

--log (response)

decoded = json.pdecode(response)

--log(decoded)

status = decoded.data[1]

grp.checkwrite('50/1/4', status.systemID)
grp.checkwrite('50/1/5', status.zoneID)
grp.checkwrite('50/1/6', status.name)

Great! Thank you so much!   Smile


RE: JSON items into groups - Erwin van der Zwart - 15.01.2025

Should it not be status = decoded[1].data ?


RE: JSON items into groups - alexll - 15.01.2025

(7 hours ago)Erwin van der Zwart Wrote: Should it not be status = decoded[1].data ?

I don't know, but I have tried it and it works flawlessly.