Page 1 of 1
Get Solar data with lua
Posted: Monday 19 November 2018 21:16
by WaTTe
Want to get the data from these links:
https://services.swpc.noaa.gov/products ... speed.json
https://services.swpc.noaa.gov/products ... field.json
There you will find these dates:
{"WindSpeed":"318","TimeStamp":"2018-11-19 20:02:00.000"}
{"Bt":"5","Bz":"-1","TimeStamp":"2018-11-19 20:01:00.000"}
For this I had created the following script:
Code: Select all
(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\lib.lua")()
JSON = assert(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\JSON.lua")()
local connection
local contt
local connection = assert(io.popen("C:\\curl\\curl https://services.swpc.noaa.gov/products/summary/solar-wind-mag-field.json"), 'unable to download noaa field data')
local raw_json_text = connection:read('*all')
connection:close()
local lua_value = JSON:decode(raw_json_text)
local lua_value =(raw_json_text)
updateSValue(53, lua_value.Bz)
updateSValue(71, lua_value.Bt)
local connection = assert(io.popen("C:\\curl\\curl https://services.swpc.noaa.gov/products/summary/solar-wind-speed.json"), 'unable to download noaa speed data')
local content = connection:read('*all')
connection:close()
local data1 =(content)
updateSValue(52, data1.WindSpeed)
return commandArray
Do not get error messages, but the data has not been updated since September.
Where am I doing something wrong?
Works with domoticz version V4.9700 under Windows10
Waling
Re: Get Solar data with lua, problems
Posted: Saturday 01 December 2018 17:50
by WaTTe
Someone a little more understanding of LUA, who can help me?
Re: Get Solar data with lua
Posted: Saturday 01 December 2018 18:44
by jvdz
Doubt this script ever worked as shown as the lua_value table filled by the JSON:DECODE() is overridden the next line with the raw received data:
... and Data1 is never decoded but simply filled with the raw data, so all will be filled with nil (is my guess).
Jos
Re: Get Solar data with lua
Posted: Saturday 01 December 2018 20:36
by WaTTe
i changed the code in this:
Code: Select all
--(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\lib.lua")()
JSON = assert(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\JSON.lua")()
local connection
local contt
local lua_value
local connection = assert(io.popen("C:\\curl\\curl https://services.swpc.noaa.gov/products/summary/solar-wind-mag-field.json"), 'unable to download noaa field data')
local raw_json_text = connection:read('*all')
connection:close()
local data = JSON:decode(raw_json_text)
updateSValue(53, data.Bz)
updateSValue(71, data.Bt)
local connection = assert(io.popen("C:\\curl\\curl https://services.swpc.noaa.gov/products/summary/solar-wind-speed.json"), 'unable to download noaa speed data')
local content = connection:read('*all')
connection:close()
local data_1 =JSON:decode(content)
updateSValue(52, data_1.WindSpeed)
return commandArray
But still I get this error message:
2018-12-01 20:35:01.029 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_noaa.lua: ...am Files (x86)\Domoticz\scripts\lua\script_time_noaa.lua:14: attempt to index local 'data' (a nil value)
Re: Get Solar data with lua
Posted: Saturday 01 December 2018 22:34
by jvdz
I have added some print statements to check what is happening and this simple test script works fine for me:
Code: Select all
JSON = assert(loadfile "JSON.lua")()
local connection
local contt
local lua_value
local connection = assert(io.popen("curl https://services.swpc.noaa.gov/products/summary/solar-wind-mag-field.json"), 'unable to download noaa field data')
local raw_json_text = connection:read('*all')
connection:close()
print(raw_json_text)
local data = JSON:decode(raw_json_text)
print(data.Bz)
print(data.Bt)
I get when running it manually:
Code: Select all
lua53 "test.lua"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 57 100 57 0 0 24 0 0:00:02 0:00:02 --:--:-- 24
100 57 100 57 0 0 24 0 0:00:02 0:00:02 --:--:-- 24
{"Bt":"6","Bz":"4","TimeStamp":"2018-12-01 21:29:00.000"}
4
6
Notice I have changed the first line
as for my purpose, so change that back to the appropriate location for your system and give it a try to see what it says.
Jos
Re: Get Solar data with lua
Posted: Sunday 02 December 2018 20:59
by WaTTe
Have the script modified as indicated, using 'print' commands.
The reference to the 'JSON.lua file must therefore be complete because I work with Windows.
After that still error messages, this was because an outdated CURL is referred to, which does not support 'HTTPS'. Now everything works correctly

, thanks for the tips.
The code is now:
Code: Select all
(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\lib.lua")()
JSON = assert(loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\JSON.lua")()
local connection
local contt
local lua_value
local connection = assert(io.popen("C:\\curl1\\bin\\curl https://services.swpc.noaa.gov/products/summary/solar-wind-mag-field.json"), 'unable to download noaa field data')
local raw_json_text = connection:read('*all')
connection:close()
print (raw_json_text)
local data = JSON:decode(raw_json_text)
print (data.Bz)
updateSValue(53, data.Bz)
updateSValue(71, data.Bt)
local connection = assert(io.popen("C:\\curl1\\bin\\curl https://services.swpc.noaa.gov/products/summary/solar-wind-speed.json"), 'unable to download noaa speed data')
local content = connection:read('*all')
connection:close()
local data_1 =JSON:decode(content)
updateSValue(52, data_1.WindSpeed)
return commandArray