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

Moderator: leecollings

Post Reply
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Translate curl command to get UV data to URL

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Translate curl command to get UV data to URL

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Translate curl command to get UV data to URL

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Translate curl command to get UV data to URL

Post 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.
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Translate curl command to get UV data to URL

Post 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
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Translate curl command to get UV data to URL

Post by elmortero »

Excellent, couldn't figure that one out last night..
Thanks a lot!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Translate curl command to get UV data to URL

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest