UV index readings realtime Topic is solved

Moderator: leecollings

User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

UV index readings realtime

Post by capman »

I use wunderground from my weather value readings. But the pws for my location is no UV Index. Or not so accurate that I can use it.
So I found a site who have some UV readings (API + Json call). But I have no knowledge how to begin for using the data in a script into domoticz.
Is there anybody who know how to do is , and to set me in the right direction ? I'm not a scripter , just copy past to get the result a want ;) .
Thanks.

Site a found >>> https://www.openuv.io/
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: UV index readings realtime

Post by remb0 »

User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: UV index readings realtime

Post by capman »

:D :D My search string UV was not working on the site. To short ;)
Thanks remb0 , I will try this 8-) .
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: UV index readings realtime

Post by Toulon7559 »

A plugin for Domoticz is a nice way to extract the info from OpenUV.io, but as part of a Python-script I just need the JSON-response from OpenUV.io
Should be possible with just one scriptline (as adapted line from the examples at the Website of OpenUV.io), like

Code: Select all

page1 = urllib.urlopen('https://api.openuv.io/api/v1/uv?lat=LATITUDE&lng=LONGITUDE&altitude=ALTITUDE' -H 'x-access-token: API_KEY')
But the URL-call in that line fails with error report

Code: Select all

SyntaxError: invalid syntax
pointing to the last but one character of the line: '

Tried several variants for the end of the line with the token-input, but then get reply 'No API-key provided'
No difference whether Python2.x or Python3.x
Must be something subtle.

Anybody a suggestion for correction?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: UV index readings realtime

Post by FireWizard »

Hi,

Quick guess. Did you try:

Code: Select all

page1 = urllib.request.urlopen('https://api.openuv.io/api/v1/uv?lat=LATITUDE&lng=LONGITUDE&altitude=ALTITUDE' -H 'x-access-token: API_KEY')
Regards
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: UV index readings realtime

Post by Toulon7559 »

Unfortunately the insertion of 'request' is not making a difference.

In some way the problem is related to the end-section of the line, because that is the difference with other scripts

Code: Select all

 -H 'x-access-token: API_KEY'
Looking 'under the hood' at the website of OpenUV.io I have seen the API-key-insertion in various ways, but none of those constructions could make my scriptline error-free.
Suspicion coming up that the packaging & syntax around the API-Key is not correct for this script-line:
see https://docs.python.org/2/library/urllib.html
But no idea yet how to remedy ......

;-) Perhaps using the plugin is an easier solution.
Last edited by Toulon7559 on Wednesday 03 June 2020 22:27, edited 3 times in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: UV index readings realtime

Post by FireWizard »

Ho,

@Toulon7559

If you have Mosquitto and Node Red installed, I have described a solution, using these tools at:
https://www.domoticz.com/forum/viewtopi ... 12&t=32438

Perhaps as alternative for the script.

Regards
Last edited by FireWizard on Friday 29 May 2020 18:19, edited 1 time in total.
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: UV index readings realtime

Post by Toulon7559 »

MQTT & NodeRed is on my ToDo-list.
;-) Therefore time required for the learning curve:
this may be a good exercise ........
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: UV index readings realtime

Post by waaren »

Toulon7559 wrote: Thursday 28 May 2020 16:25 MQTT & NodeRed is on my ToDo-list.
;-) Therefore time required for the learning curve.

Meanwhile looking for something quick&dirty-but-effective .......
In case you are interested.... I have a working dzVents script
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: UV index readings realtime

Post by FireWizard »

@Toulon7559

@waaren wrote:
In case you are interested.... I have a working dzVents script
In that case, I should accept this generous offer :) .

Regards
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: UV index readings realtime

Post by Toulon7559 »

@waaren
Thanks for the offer!
;-) Sure better (and much easier) than reinventing the wheel ....

As earlier indicated, looking for a script getting hold of the localised json-file, aiming at extraction of some data.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: UV index readings realtime

Post by waaren »

Toulon7559 wrote: Thursday 28 May 2020 22:59 @waaren
Thanks for the offer!
;-) Sure better (and much easier) than reinventing the wheel ....

As earlier indicated, looking for a script getting hold of the localised json-file, aiming at extraction of some data.
Here you go...

Probably some quite old constructs in the script because I have not touched it since a long time but it is still functional.

Code: Select all

--[[ openUV.lua for [ dzVents >= 2.4 ] 
]]--

return {
    on      =   {   timer           =   { "every 30 minutes at daytime" }, -- Triggers the getJsonPart
                    httpResponses   =   { "openUV_Response" }               -- Trigger the handle Json part
                },
                
    logging =   {   level     =   domoticz.LOG_ERROR,                                   
                    marker    =   "openUV" 
                },                                           

    data    =   {   openUV_Longitude          = {initial = "4.51234"                           },  -- I store my longitude, latitude, APIkey, filename in data
                    openUV_Latitude           = {initial = "51.81234"                          },  -- You can get them from domoticz settings
                    openUV_Filename           = {initial = "scripts/dzVents/data/OpenUV.json"   },  -- path relative to domoticz directory
                    openUV_APIKey             = {initial = "8xxxxxxxxxxxxd5e004ea781"   },  -- get your free API key from https://www.openuv.io/
                    openUV_currentUV_device   = {initial = 887                                  },  -- You have to create a couple of virtual devices 
                    openUV_maxUV_device       = {initial = 888                                  },  -- (first two as UV sensors and other two as Alert)
                    openUV_UV_Alert_device    = {initial = 894                                  },  -- Remember device ID"s and replace the "xxx" with them
                    openUV_ozone_Alert_device = {initial = 895                                  },  -- Look at https://www.domoticz.com/wiki/Hardware_Setup#Dummy_Hardware 
                },
                    
    execute = function(dz, trigger)
            
        local function UV_Index2Alert(index)                            -- Levels as from KNMI
            local alert = dz.ALERTLEVEL_RED
            if      index < 3 then alert = dz.ALERTLEVEL_GREY
            elseif  index < 5 then alert = dz.ALERTLEVEL_GREEN
            elseif  index < 7 then alert = dz.ALERTLEVEL_YELLOW
            elseif  index < 9 then alert = dz.ALERTLEVEL_ORANGE
            end
            return alert
        end

        local function ozone2Alert(value)                               -- Levels as found on various WIKI pages
            local alert = dz.ALERTLEVEL_RED
            if      value > 400 then alert = dz.ALERTLEVEL_GREY
            elseif  value > 340 then alert = dz.ALERTLEVEL_GREEN
            elseif  value > 310 then alert = dz.ALERTLEVEL_YELLOW
            elseif  value > 280 then alert = dz.ALERTLEVEL_ORANGE
            end
            return alert
        end
       
        local function getOpenUV_json()
            local openUV_url        = "https://api.openuv.io/api/v1/uv?lat=" .. dz.data.openUV_Latitude .. "&lng=" .. dz.data.openUV_Longitude 
             dz.openURL({
                            url = openUV_url ,
                            method = "GET",
                            callback = "openUV_Response",
                            headers = { ['x-access-token'] =  dz.data.openUV_APIKey },
                      })
        end
                
        local function handleOpenUVResponse()
            -- Read response into table and close file
            openUVResponse      = trigger.json
            dz.log(openUVResponse,dz.LOG_DEBUG)
            
            if openUVResponse == nil or
               openUVResponse.result.uv                      == nil or
               openUVResponse.result.uv_max                  == nil or
               openUVResponse.result.uv_max_time             == nil or 
               openUVResponse.result.ozone                   == nil or
               openUVResponse.result.safe_exposure_time.st3  == nil then
               dz.log("Some values are missing; check it out",dz.LOG_ERROR) 
               dz.log(openUVResponse,dz.LOG_ERROR) 
               return
            end    

            -- Update UV devices    
            dz.devices(dz.data.openUV_currentUV_device).updateUV(dz.utils.round(openUVResponse.result.uv,1))
            dz.devices(dz.data.openUV_maxUV_device).updateUV(dz.utils.round(openUVResponse.result.uv_max,1))

            -- convert to local time
            local commandString     = "date -d " .. openUVResponse.result.uv_max_time ..  "  '+%k:%M uur' "
            local dateOutput        = assert(io.popen(commandString))   
            local openUVMaxUVtime   = dateOutput:read("*all")
            dateOutput:close()
            
            -- Construct Alertlevel and text and update UV alert device (type = Alert)
            UV_AlertText = "Max UV index today: " .. dz.utils.round(openUVResponse.result.uv_max,1) .. " at " .. openUVMaxUVtime 
            dz.log("Current UV index = " .. dz.utils.round(openUVResponse.result.uv,1) .. ", " ..  UV_AlertText,dz.LOG_INFO)
            dz.devices(dz.data.openUV_UV_Alert_device).updateAlertSensor(UV_Index2Alert(openUVResponse.result.uv_max), UV_AlertText)

            -- Construct Alertlevel and text and update ozone alert device (type = Alert)
            ozoneAlertText = "Ozone (Dobson unit) is " .. openUVResponse.result.ozone .. "\n Max. exposure to sun (Skintype 3) is " ..
                            math.min(openUVResponse.result.safe_exposure_time.st3,720) .. " minutes."         
            dz.log(ozoneAlertText,dz.LOG_INFO)
            dz.devices(dz.data.openUV_ozone_Alert_device).updateAlertSensor(ozone2Alert(openUVResponse.result.ozone), ozoneAlertText)
        end
        
        if trigger.isTimer or trigger.isDevice then
            getOpenUV_json()
        else     
            handleOpenUVResponse()
        end
    end
}
 
uv1.png
uv1.png (38.9 KiB) Viewed 2785 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: UV index readings realtime

Post by EdwinK »

If this works, I will stop with trying to get the data through mqtt, which I still can't get to work.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: UV index readings realtime

Post by EdwinK »

When I use this, I get an error:

2020-05-29 10:00:07.118 Error: Error opening url: https://api.openuv.io/api/v1/uv?lat=51. ... g=4.412694
2020-05-29 10:00:07.262 Error: dzVents: Error: (3.0.8) openUV: HTTP/1.1 response: 28 ==>> Timeout was reached
2020-05-29 10:00:07.262 Error: dzVents: Error: (3.0.8) openUV: Some values are missing; check it out
2020-05-29 10:00:07.262 Error: dzVents: Error: (3.0.8) openUV: nil

I have put the api key in the settings, but somehow it looks it isn't used.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: UV index readings realtime

Post by Toulon7559 »

:D For me waaren's script works like a breeze
= Quick start to get hold of the values in the JSON-file
[Planned for generation (in RRDTool) of reference curve for my own UV-sensors, and for display of some values at my website]

In that perspective a question for understanding.

With following scriptline would expect to see the indicated JSON-file in the indicated folder

Code: Select all

openUV_Filename           = {initial = "scripts/dzVents/data/OpenUV.json"   },  -- path relative to domoticz directory
However, nothing visible.
Expecting too much, or wrong understanding?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: UV index readings realtime

Post by waaren »

Toulon7559 wrote: Friday 29 May 2020 10:24 Expecting too much, or wrong understanding?
A bit of both :)

This line is no longer in use. I used it to test / develop parts of the script because the number of daily allowed calls to openUV are limited. So I stored the JSON there during test / development to be able to test the processing of the JSON without the actual call.

If you need the JSON string in the file you could add below snippet after line 59 " dz.log(openUVResponse,dz.LOG_DEBUG)"

Code: Select all

            local f = io.open(dz.data.openUV_Filename, 'a')
            f:write(trigger.data)
            f:close()
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: UV index readings realtime

Post by waaren »

EdwinK wrote: Friday 29 May 2020 10:12 I have put the api key in the settings, but somehow it looks it isn't used.
If the script did not use the API key you would see something different like

Code: Select all

2020-05-29 12:02:35.825  Status: dzVents: Info: Handling httpResponse-events for: "openUV_Response"
2020-05-29 12:02:35.825  Error: dzVents: Error: (3.0.8) openUV: HTTP/1.1 response: 403 ==>> Forbidden
2020-05-29 12:02:35.825  Error: dzVents: Error: (3.0.8) openUV: An error occurred when calling event handler 
Can you double check to see if you have any typos and try again. If no luck you can send you script as it is now via PM
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: UV index readings realtime

Post by Toulon7559 »

@waaren

Exactly same argument (= limited daily free allowance for calls) at my side to have OpenUV called only by one application, and then source-information locally available for all other users, including possibility to directly read the JSON-file.
In lua I would use a scriptline to set the required properties/attributes for the JSON-file, like

Code: Select all

os.execute("chmod a+rw /path/filename.txt")
After check, that construction also seems valid for dzVents.

;-) waaren probably has more clever (=shorter&general) solution, but this line added to the snippet makes the specific JSON-file accessible for all users

Code: Select all

 os.execute("chmod a+rw /home/pi/domoticz/scripts/dzVents/data/OpenUV.json")
Addition 2020June03:
The JSON-file is the extended source for info available from OpenUV.
By changing the attribute in the scriptline from 'a ' to 'w+' (is 'append-mode' versus 'rewrite'-mode), you get only the latest issue of the JSON-file, not the whole collection.

Code: Select all

local f = io.open(dz.data.openUV_Filename, 'w+')
;-) For getting the 'elementary' UV-info it is much easier to install the plugin (but you have to be aware that each computer-with-plugin needs it's own API-key from OpenUV, because otherwise you are quickly consuming your daily free allowance of calls) ......
Last edited by Toulon7559 on Thursday 04 June 2020 7:55, edited 7 times in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: UV index readings realtime

Post by Jan Jansen »

waaren wrote: Friday 29 May 2020 0:12 Probably some quite old constructs in the script because I have not touched it since a long time but it is still functional.
@ Waaren,

Thank you for sharing your script. I want to customize the script.
OpenUv.png
OpenUv.png (45.62 KiB) Viewed 2680 times
I want to get rid of the blank line between "Max UV index today ... and Max exposure ...", no success.

Code: Select all

-- Construct Alertlevel and text and update UV alert device (type = Alert)
            UV_AlertText = "Max UV index today: " .. dz.utils.round(openUVResponse.result.uv_max,1) .. " om " .. openUVMaxUVtime .."\n Max blootstelling aan de zon (huidtype 3) is " ..
                            math.min(openUVResponse.result.safe_exposure_time.st3,720) .. " minuten!"
            dz.log("Current UV index = " .. dz.utils.round(openUVResponse.result.uv,1) .. ", " ..  UV_AlertText,dz.LOG_INFO)
            dz.devices(dz.data.openUV_UV_Alert_device).updateAlertSensor(UV_Index2Alert(openUVResponse.result.uv_max), UV_AlertText)
Is it possible to avoid this blank line, if so how?

Thanks in advance!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: UV index readings realtime

Post by waaren »

Jan Jansen wrote: Saturday 30 May 2020 14:13 Is it possible to avoid this blank line, if so how?
If you remove the \n before Max blootstelling you wont' have the blank line
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