Page 1 of 1

lua json opensensemap

Posted: Saturday 29 July 2017 9:10
by grpfge
Hello
With the following script, I want to read data from this address https://api.opensensemap.org/boxes/5968 ... d1/sensors

Code: Select all

commandArray = {}

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
 
       local config=assert(io.popen('curl https://api.opensensemap.org/boxes/5968f05d1900bf0011ffb0d1/sensors' ))
       local location = config:read('*all')
       config:close()
       local jsonLocation = json:decode(location)
       t1 = jsonLocation.sensors[0].lastMeasurement.value
       lf = jsonLocation.sensors[1].lastMeasurement.value
       ld = jsonLocation.sensors[2].lastMeasurement.value
       lx = jsonLocation.sensors[3].lastMeasurement.value
       uv = jsonLocation.sensors[4].lastMeasurement.value
       
       print('Temperatur: '..t1)
       
The result is
Error: EventSystem: in sensebox1: [string "..."]:13: attempt to index field '?' (a nil value)

Can you look at my snytax for json, i think there is an mistake

Re: lua json opensensemap

Posted: Saturday 29 July 2017 11:24
by mivo
Hi,

try this syntax for LUA table values:

Code: Select all

t1 = jsonLocation.sensors[1].lastMeasurement['value']

Re: lua json opensensemap

Posted: Saturday 29 July 2017 11:41
by grpfge
It works, thanks

Re: lua json opensensemap

Posted: Saturday 29 July 2017 11:42
by grpfge
It works, thanks

Re: lua json opensensemap

Posted: Saturday 29 July 2017 12:05
by mivo
Great 8-)