Page 1 of 1
Translate curl command to get UV data to URL
Posted: Wednesday 23 May 2018 18:50
by waaren
I want to add a virtual UV sensor to my domoticz environment and found
https://www.openuv.io to get the data from.
My preference is to use a standard URL because I want to process the JSON return directly in dzVents without storing the result in an OS file first.
The command to get the JSON in curl is
Code: Select all
curl -X GET 'https://api.openuv.io/api/v1/uv?lat=-51,11&lng=4,99' -H 'x-access-token: blablablablablablablablabla'
Does anyone know how to translate this into a regular url ?
I am also interested from which sites other forum members get their UV data from. Preferable with a JSON return
Thx in advance for any reaction
Re: Translate curl command to get UV data to URL
Posted: Thursday 24 May 2018 3:08
by elmortero
Hi,
You cannot pass your token (apikey) to Openiv.io as a query string like with darksky for example.
As an alternative you can use io.popen
(sorry Dannybloe, I tried to pass the -H x-access-token with your asynchronous http requests but gave up on it

)
Here is the script I came up with(dzvents):
Code: Select all
return {
on = {
timer = { 'every 30 minutes' }
},
execute = function(domoticz, triggerItem)
--check offset for DST/non-DST
--these are for timezone Brussels. change to match your timezone
local situation = os.date("%Z")
if situation == 'CET' then
offset = 1 --non-DST
else offset = 2 --DST
end
json = (loadfile "/home/root/domoticz/scripts/lua/JSON.lua")() -- For Linux
local token = 'xxxxxxxx' -- fill in your key here
local lat = 'xxxxx' --latitude
local lng = 'xxxxxx' --longitude
local config=assert(io.popen("curl -X GET 'https://api.openuv.io/api/v1/uv?lat="..lat.."&lng="..lng.."' -H 'x-access-token: '"..token))
local location = config:read('*all')
config:close()
local response = json:decode(location)
uv = tonumber(response.result.uv)
uv_max = domoticz.utils.round((response.result.uv_max),0)
uv_max_time = response.result.uv_max_time
--get hour and minutes only from time string
local hora = tonumber(string.sub(uv_max_time, 12,13)) + offset --add offset for timezone and DST to hours
local mins = string.sub(uv_max_time, 15,16)
maxtime = tostring(hora..':'..mins) --combine to HH:MM
ozone = domoticz.utils.round((response.result.ozone),0)
print('\nCurrent UV value : '..uv)
print('Highest UV value today: '..uv_max)
print('Highest UV value at: '..maxtime)
print('Current ozone value: '..ozone)
-- update your sensors with aquired values here
end
}
Re: Translate curl command to get UV data to URL
Posted: Thursday 24 May 2018 16:20
by waaren
elmortero wrote: ↑Thursday 24 May 2018 3:08
Hi,
You cannot pass your token (apikey) to Openiv.io as a query string like with darksky for example.
As an alternative you can use io.popen
(sorry Dannybloe, I tried to pass the -H x-access-token with your asynchronous http requests but gave up on it

)
@elmortero, thx for the script. I cherry picked a little bit from it
see my solution here but I only want aSync type of calls to external URL's or to OS functions that could cause a delay to the event system. I now made something with the use of a domoticz user variable. First part starts the curl in the background and second part (triggered by the updated uservar) reads the json return and update the virtual sensors / devices.
Again thanks for your reaction and script.
Re: Translate curl command to get UV data to URL
Posted: Thursday 24 May 2018 17:15
by elmortero
waaren wrote: ↑Thursday 24 May 2018 16:20
but I only want aSync type of calls to external URL's or to OS functions that could cause a delay to the event system.
Again thanks for your reaction and script.
That's what I want too

But sometimes when it cannot be as it must be, it must be as it can.
Re: Translate curl command to get UV data to URL
Posted: Thursday 24 May 2018 19:26
by EddyG
My bad (syntax BUG). This works.
Code: Select all
domoticz.openURL({
url = 'https://api.openuv.io/api/v1/uv?lat=<lat>&lng=<lon>',
method = 'GET',
callback = 'UV-data',
headers = { ['x-access-token'] = '<api-key>' },
And later to get the UV from the response:
Code: Select all
local UVvalue = item.json.result.uv
Re: Translate curl command to get UV data to URL
Posted: Thursday 24 May 2018 22:51
by elmortero
Excellent, couldn't figure that one out last night..
Thanks a lot!
Re: Translate curl command to get UV data to URL
Posted: Friday 25 May 2018 1:32
by waaren
EddyG wrote: ↑Thursday 24 May 2018 19:26
This works.
Code: Select all
domoticz.openURL({
url = 'https://api.openuv.io/api/v1/uv?lat=<lat>&lng=<lon>',
method = 'GET',
callback = 'UV-data',
headers = { ['x-access-token'] = '<api-key>' },
You are the man !
Thx