Decoding measurement data from HTTP

I have a question regarding general Lua programming. The code will be for Fibaro Home Center 2 but I hope someone can help me with the general coding.

I get data by using this:

HC2 = Net.FHttp(“192.168.2.34”)
response, status, errorCode = HC2:GET(“/arduino/weather/1”)

The return is something like this:

Rain: 0.3 UV: 1.23 Wind: 23.0 WindVane: S Temperature: 23.3 ...

How can I save this data in separate variables? It’s probably possible with string patterns but I don’t really understand what to do here.

Thanks for any help!

As an example, this code:

local text = [[
Rain: 0.3
UV: 1.23
Wind: 23.0
WindVane: S
Temperature: 23.3 ]]

local x = {}
for a,b in text: gmatch "(%w+):%s*([^%s]+)" do
  x[a] = b
end

print(pretty(x))

when run in Rex Beckett’s excellent LuaTest gives:

LuaTest 1.5.2

Lua file: /etc/cmh-ludl/luatest.lua

Results
No errors
Runtime: 1.6 ms
Code returned: nil

Print output
{
    Rain="0.3",
    Temperature="23.3",
    UV="1.23",
    Wind="23.0",
    WindVane="S"}     

Thank you very much. This is exactly what I wanted!

I have a similar task, but it is way more complicated.

I get this:

{"status":"ok","body":[{"beg_time":1404840727,"step_time":308,"value":[[0],[0]]},{"beg_time":1404841355,"step_time":308,"value":[[0],[0]]},{"beg_time":1404841970,"step_time":295,"value":[[0],[0]]}, {"beg_time":1404842536,"step_time":307,"value":[[0],[0]]},{"beg_time":1404843201,"step_time":295,"value":[[0],[0]]}, {"beg_time":1404858222,"step_time":358,"value":[[0],[0]]},{"beg_time":1404858875,"step_time":308,"value":[[0],[0],[0]]}, {"beg_time":1404859797,"step_time":295,"value":[[0],[0]]},{"beg_time":1404860363,"step_time":307,"value":[[0],[0]]}, {"beg_time":1404861028,"step_time":295,"value":[[0],[0]]},{"beg_time":1404861643,"step_time":308,"value":[[0],[0]]}, {"beg_time":1404862258,"step_time":295,"value":[[0],[0]]},{"beg_time":1404862861,"step_time":307,"value":[[0],[0],[0]]}, {"beg_time":1404863770,"step_time":270,"value":[[0],[0]]},{"beg_time":1404864348,"step_time":357,"value":[[0],[0]]}, {"beg_time":1404865000,"step_time":308,"value":[[0],[0],[0]]},{"beg_time":1404865910,"step_time":308,"value":[[0],[0],[0]]}, {"beg_time":1404866820,"step_time":307,"value":[[0],[0]]},{"beg_time":1404867435,"step_time":271,"value":[[0],[0]]}, {"beg_time":1404868014,"step_time":357,"value":[[0],[0]]},{"beg_time":1404868666,"step_time":308,"value":[[0],[0]]}, {"beg_time":1404876587,"step_time":358,"value":[[0],[0.334]]},{"beg_time":1404877163,"step_time":319,"value":[[0.167],[0]]}, {"beg_time":1404877790,"step_time":308,"value":[[0],[0]]},{"beg_time":1404878392,"step_time":309,"value":[[0],[0]]}, {"beg_time":1404879009,"step_time":294,"value":[[0],[0]]},{"beg_time":1404879611,"step_time":282,"value":[[0],[0]]}, {"beg_time":1404880201,"step_time":359,"value":[[0],[0]]},{"beg_time":1404880854,"step_time":307,"value":[[0],[0]]}, {"beg_time":1404926272,"step_time":308,"value":[[0],[0]]}],"time_exec":0.019207000732422,"time_server":1404926861}

The amount of “lines” can vary from 1 to 1024.
The amount of values per line can be 0?, 1, 2, 3 or perhaps even more.

What I need is all the values in brackets added up to one value. (which probably makes it easier.)

This is one value: [0]

Thanks for your help!

This is JSON, so a standard decoder will do the heavy lifting.

Specifically, this will add up and print the sum of the “value” fields if your input string is x:

local y = json.decode (x)
local sum = 0
for _,z in ipairs (y.body) do
  for _, b in ipairs (z.value) do
    sum = sum + b[1]
  end
end
print (sum)

That works!

Thanks a lot for your kind help!