Get Solar data with lua

Moderator: leecollings

Post Reply
WaTTe
Posts: 5
Joined: Saturday 20 May 2017 21:54
Target OS: Windows
Domoticz version: 3.8153
Location: Hallum, The Netherlands
Contact:

Get Solar data with lua

Post 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
WaTTe
Posts: 5
Joined: Saturday 20 May 2017 21:54
Target OS: Windows
Domoticz version: 3.8153
Location: Hallum, The Netherlands
Contact:

Re: Get Solar data with lua, problems

Post by WaTTe »

Someone a little more understanding of LUA, who can help me?
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Get Solar data with lua

Post 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:

Code: Select all

local lua_value =(raw_json_text)
... and Data1 is never decoded but simply filled with the raw data, so all will be filled with nil (is my guess). :)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
WaTTe
Posts: 5
Joined: Saturday 20 May 2017 21:54
Target OS: Windows
Domoticz version: 3.8153
Location: Hallum, The Netherlands
Contact:

Re: Get Solar data with lua

Post 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)
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Get Solar data with lua

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
WaTTe
Posts: 5
Joined: Saturday 20 May 2017 21:54
Target OS: Windows
Domoticz version: 3.8153
Location: Hallum, The Netherlands
Contact:

Re: Get Solar data with lua

Post 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 :D , 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
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests