Translate curl command to get UV data to URL Topic is solved

Post Reply
User avatar
waaren
Posts: 1079
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Translate curl command to get UV data to URL

Post by waaren » Wednesday 23 May 2018 18:50

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
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

elmortero
Posts: 233
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Translate curl command to get UV data to URL

Post by elmortero » 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 :( )

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
}

User avatar
waaren
Posts: 1079
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Translate curl command to get UV data to URL

Post by waaren » Thursday 24 May 2018 16:20

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.
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

elmortero
Posts: 233
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Translate curl command to get UV data to URL

Post by elmortero » Thursday 24 May 2018 17:15

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.

User avatar
EddyG
Posts: 315
Joined: Monday 02 November 2015 6:54
Target OS: Raspberry Pi
Domoticz version: 4.9999
Location: Rhenen, Netherlands
Contact:

Re: Translate curl command to get UV data to URL

Post by EddyG » Thursday 24 May 2018 19:26

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
Regards,
Eddy

elmortero
Posts: 233
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Translate curl command to get UV data to URL

Post by elmortero » Thursday 24 May 2018 22:51

Excellent, couldn't figure that one out last night..
Thanks a lot!

User avatar
waaren
Posts: 1079
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Translate curl command to get UV data to URL

Post by waaren » Friday 25 May 2018 1:32

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
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests