LogicMachine Forum
JSON items into groups - Printable Version

+- LogicMachine 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

(14.01.2025, 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

(15.01.2025, 07:51)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.


RE: JSON items into groups - Ceros2112 - 12.03.2026

Hi, I have the same situation but i need to switch on or off the device, is it possible?


RE: JSON items into groups - admin - 12.03.2026

Try this. Event script mapped to a binary object. Change url, systemID and zoneID as needed:
Code:
require('json') http = require('socket.http') url = 'http://192.168.1.111:3000/api/v1/hvac' on = event.getvalue() and 1 or 0 data = json.encode({   systemID = 1,   zoneID = 1,   on = on, }) resp, code = http.request({   url = url,   method = 'PUT',   body = data,   headers = {     ['Content-Type'] = 'application/json',   } }) log(resp, code)



RE: JSON items into groups - Ceros2112 - 12.03.2026

Thanks! It works but there is a little mistake here:

Code:
data = json.encode({   data = {     systemID = 1,     zoneID = 1,     on = on,   } })

here is my version:

Code:
data = json.encode({   systemID = 1,   zoneID = 1,   on = on })