Logic Machine Forum
Data from http request - to JSON - to KNX - 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: Data from http request - to JSON - to KNX (/showthread.php?tid=1304)



Data from http request - to JSON - to KNX - Rune - 21.03.2018

Hi!

I need a little help with this:

I now got a energy-gateway that can send a JSON-file when requested. The way to do it, is type "http://10.0.0.xxx/output.json" in the web browser.

I then get a file called "output.json" and the string is like this:

{"0015BC001B024257":{"ApplicationHeartbeat":"1","CurrentSummationDelivered":"12812.1","CurrentSummationReceived":"0.0","Tamper":"0","InstantaneousDemand":"4342.0","Battery":"0"}}

The information I need is CurrentSummationDelivered and InstantaneousDemand and the goal is to write this to two KNX groups with 30 seconds intervals.

Any tips where to start? Thanks from a LM5-newbie.  Smile


RE: Data from http request - to JSON - to KNX - Erwin van der Zwart - 21.03.2018

Hi,

Try something like this:
Code:
require('socket.http')
require('json')

socket.http.TIMEOUT = 15

data, code = socket.http.request('http://10.0.0.xxx/output.json')

if data then
 data = json.pdecode(data)
end

if data then
 CurrentSummationDelivered = data['0015BC001B024257'].CurrentSummationDelivered
 InstantaneousDemand = data['0015BC001B024257'].InstantaneousDemand
 grp.update('1/1/1', tonumber(CurrentSummationDelivered))
 grp.update('1/1/2', tonumber(InstantaneousDemand))
end
BR,

Erwin


RE: Data from http request - to JSON - to KNX - Rune - 21.03.2018

(21.03.2018, 18:21)Erwin van der Zwart Wrote: Hi,

Try something like this:
Code:
require('socket.http')
require('json')

socket.http.TIMEOUT = 15

data, code = socket.http.request('http://10.0.0.xxx/output.json')

if data then
 data = json.pdecode(data)
end

if data then
 CurrentSummationDelivered = data['0015BC001B024257'].CurrentSummationDelivered
 InstantaneousDemand = data['0015BC001B024257'].InstantaneousDemand
 grp.update('1/1/1', tonumber(CurrentSummationDelivered))
 grp.update('1/1/2', tonumber(InstantaneousDemand))
end
BR,

Erwin

Something tells me that I am forgetting something? No action on the KNX-bus. (Picture attached)


RE: Data from http request - to JSON - to KNX - admin - 21.03.2018

Add log(data, code) after http request and log(data) at the end of the script, then check Logs tab for results. Do you have objects 4/0/20 and 4/0/21 created in LM?


RE: Data from http request - to JSON - to KNX - Rune - 21.03.2018


Here is the solution, thank to you that know who you are. Wink

And thanks for all the help also! Smile

Code:
sock = require('socket').tcp()
sock:settimeout(5)

res, err = sock:connect('10.0.0.112', 80)
if res then
 sock:send('GET /output.json HTTP/1.1\r\n\r\n')
 res = sock:receive('*a')
 log(res)

 if res then
   p, e = res:find('\r\n\r\n', 1, true)
   if e then
     data = res:sub(e + 1)
     data = require('json').pdecode(data)
     log(data)
   end
 end
end

if data then
 CurrentSummationDelivered = data['0015BC001B024257'].CurrentSummationDelivered
 InstantaneousDemand = data['0015BC001B024257'].InstantaneousDemand
 grp.write('4/0/20', tonumber(CurrentSummationDelivered))
 grp.write('4/0/21', tonumber(InstantaneousDemand))
end