Configurable 'KNMI Weerlive' script

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

Oh, I see the original are transparent. I now have just resized the floor icons from the original and left the ON/OFF png files untouched.
As said before: "it's not my hobby to endlessly work with pictures. That's usually bad for my mood."
Also in my theme I do not see the difference, so did not notice before. Also I am not looking at every pixel. When I recognize the picture, is usually good enough :-)
But I will look at it later today and see what I can do for you to make them 48x48 with transparent background.
I do not know who takes care of the icon generator I pointed at, but that does not seem to do the right job when producing the 0 kb floor icons and the 50x50 ON/OFF icons that are not transparent.
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

Again replaced the icons in ZIP file in the second post. 48x48 and 16x16 transparent.
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
Kedi
Posts: 553
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by Kedi »

I appriciate the nice work. When I have time I will redo your transparency work and will post them here.
But could take sometime.
Logic will get you from A to B. Imagination will take you everywhere.
janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

Kedi wrote: Wednesday 09 October 2024 10:11 When I have time I will redo your transparency work and will post them here.
No, all should be transparent now. Is it not?
Actually the original images where transparent, and I thought it still was after using the icon generator.
Did not see it on my side, because it appeared to be the same as my background. Also I did not check the pixels. Just imported and it worked. Also not noticed the 0 kb file, because I do not use the floorplan.
So, as far as I know it meets all the requirements now.
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
Kedi
Posts: 553
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by Kedi »

It meets the requirements, but the transparancy was not executed right. Bad image with an extra outline.
Not your fault, but the program with which you created the tranparancy.
Logic will get you from A to B. Imagination will take you everywhere.
jberinga
Posts: 56
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: The Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by jberinga »

Since there has been some updates in the script lately, can you post the latest version of the script? Or is there another location where the script is already updated and I can find the latest version?

Thanks!
janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

jberinga wrote: Sunday 17 November 2024 14:07 Since there has been some updates in the script lately, can you post the latest version of the script? Or is there another location where the script is already updated and I can find the latest version?
For some reason I did not received the notification of new messages. So I have missed your post. Sorry for that!
Are you still interested, or have you found it in the meantime?
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
jberinga
Posts: 56
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: The Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by jberinga »

janpep wrote: Friday 03 January 2025 19:11
jberinga wrote: Sunday 17 November 2024 14:07 Since there has been some updates in the script lately, can you post the latest version of the script? Or is there another location where the script is already updated and I can find the latest version?
For some reason I did not received the notification of new messages. So I have missed your post. Sorry for that!
Are you still interested, or have you found it in the meantime?
No problem!
It would be nice if you could share the latest version of the script(s) or redirect me to the location where the script(s) can be found.
janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

Again I did not receive notification of your message.
I will check if I can find a reason. I do not look every day to see if there are new messages.
I will get all the parts together and post it as I have it at this moment.
Last edited by janpep on Friday 10 January 2025 17:25, edited 1 time in total.
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

jberinga wrote: Saturday 04 January 2025 11:56 It would be nice if you could share the latest version of the script(s) or redirect me to the location where the script(s) can be found.
Here is the script. Note that you make the needed devices and you have to replace the 99999 idx'es for the devices you use.
Also note that 99999 can remain for some devices that you do not want to use.

I tried to contact weerlive several times per E-mail and with their contact form about the Wind Gust that is shown on the website, but NOT part of the api result. I did not get an answer. Although they are inviting to contact, I don't think ANYONE looks at this. Or they don't think it's worth responding to. Either way, bad.

Code: Select all

-- 12-05-2024 script by Jan Peppink, https://ict.peppink.nl
-- Started for weather alarm Expanded as renewed custom script instead of Buienradar.
-- Reduce the number of weather information sources.
--
-- Extra explaining comments and choices made ------------------------------------
-- Explanation of the api can be found at https://weerlive.nl/delen.php, where you also can obtain an api key.
-- Optional use of devices. You can skip them by giving them a dummy idx of 99999.
-- The BARO device forcast uses the 'image' text that is found for tomorrow.
-- WindGust is not yet part of weerlive api result. Hardcoded set to 0
-- Optional use of custom icons. Activate by setting wl_icons_installed = 1
--   NB. DOUBLE check function getIconNumber and correct your iconNumbers !!
-- The Weatherforcast device gets the icon that comes with forcast tomorrow.
--   May sometimes be a bit confusing with the text that is shown.
-- The Rainforcast device gets the icon rain or NO rain, when not expected < 24 hours.
-- The script translates mm rainfall to l/m2 which is easier to understand.
-- The script calculates expected sun hours given as zond_perc_dag of the time between sunrise and sunset to hh:MM.
-- An explanation about this can be found at https://www.knmi.nl/kennis-en-datacentrum/achtergrond/de-weersverwachting
-- 17-05-2024 Added 4 optional temperature devices for Min and Max Temp.
-- 24-05-2024 Added 1 optional text device for extensive forcast text.
-- 25-05-2024 Removed function 'getWindDirectionString'. Instead call _h.getDirectionfromDegree in global_data
-- 30-05-2024 Added call to function _h.trimLRspaces in global_data.
--            Added wl_hideNowarning. When active (=1) hide device when there is no warning.
-- 27-09-2024 Added optional link to forcast of city. Find this link at https://weerlive.nl/plaatsnamen.php


--- Your settings ----------------------------------------------------------------
-- First set the used device index numbers and variables you might want to change.
---#################################################################
-- NB! To configure, set the devices you do NOT want to use to 99999
-- Easy to temporarily or permanently not use a device without rearanging the code.
---#################################################################
local wl_alarm_idx = 99999   --idx of the custom Alert device for Weather alarm.
local wl_thb_idx = 99999     --idx of the Temperature+humidity+barometer device adapter.
local wl_gtemp_idx = 99999   --idx of the Temperature device adapter.
local wl_wind_idx = 99999    --idx of the Wind device adapter.
local wl_zicht_idx = 99999   --idx of the Visibility device.
local wl_gr_idx = 99999      --idx of the Global Solar radiation device adapter.
local wl_verw_idx = 99999    --idx of the weatherforcast Text device.
local wl_rain_idx = 99999    --idx of the Rain forcast Text device.
local wl_txt_idx = 99999     --idx of the extensive forcast Text device. 
local wl_mintoD_idx = 99999    --idx of minTemp toDay Temperature device adapter.
local wl_maxtoD_idx = 99999    --idx of maxTemp toDay Temperature device adapter.
local wl_mintoM_idx = 99999    --idx of minTemp toMorrow Temperature device adapter.
local wl_maxtoM_idx = 99999    --idx of maxTemp toMorrow Temperature device adapter.

local wl_apikey = '<yourapikey>' -- Access for weerlive.nl with your apikey.
local wl_locationurl = 'https://weerlive.nl/weer.php?plaatsnaam=<yourcityasfoundatweerlive>'
local wl_uselocationurl = 1     -- 1 or 0 when wl_lkocationurl is used or not.
local wl_icons_installed = 1    -- 1 or 0 when custom icons have been installed or not.
local wl_hideNowarning = 1   -- 1 or 0 sets the device hidden when there is no warning.

----------------------------------------------------------------------------------
return {
	on = {
		timer = { 
            --'every 2 minutes',  -- Only for testing.
            'every 10 minutes', 
        },
        httpResponses = {
		    'weerlive'       -- matches callback string below
        },
	},
	logging = {
		-- Level can be domoticz.LOG_INFO, domoticz.LOG_STATUS, domoticz.LOG_ERROR or domoticz.LOG_DEBUG
		level = domoticz.LOG_STATUS,
		--level = domoticz.LOG_DEBUG,
		marker = 'Weerlive-',
	},
	execute = function(dz, triggeredItem)
        -- Set Local environment=================
        local _u = dz.utils       -- Holds subset of handy utilities.
        local _h = dz.helpers     -- Holds the global functions
        --local _d = dz.globalData  -- Holds the global data

        -- Get location coordinates
        local lat = dz.settings.location.latitude
        local long = dz.settings.location.longitude

        -- Use some color variables
        local cGreen = '#008000;'
        local cYellow = '#ffff00;'
        local cOrange = '#ffa500;'
        local cRed = '#ff0000;'	
        local cWhite = '#FFFFFF;'
        local cBlue = '#0000FF;'
        local cGrey = '#8d8d8d;'

        local htmlColor = '#3b3bc4;'
        --Adjust lineheight
        local lineHeight = 1.2
        
        local weeralarm_value = dz.devices(wl_alarm_idx).sensorValue  -- Gets last stored value 
        local verw = ''     --Holds weather forcast
        local samenv = ''   --Holds weather summary
        local d1date = ''   --Holds date for today
        local d1nslperc = ''    --Holds neerslperc for today
        local d1znperc = ''     --Holds sun perc for today
        local d2date = ''       --Holds date for tomorrow
        local d2nslperc = ''    --Holds neerslperc for tomorrow
        local d2znperc = ''     --Holds sun perc for tomorrow
        local firstRain = 'norain'  -- Holds first rain hour.
        local lastRain = 'norain'   -- Holds first rain hour.
        local countRain = 0         -- Holds total expected rain in mm.
        local rainText = ''         -- Holds text to update rain forcast device.

        -- Local Functions go here =============
        local function gethumstat( lv )
            -- Calculate the humidity status.
            lv = tonumber( lv )
            if lv < 35 then return dz.HUM_DRY
                elseif lv >= 35 and lv < 45 then return dz.HUM_NORMAL
                elseif lv >= 45 and lv < 55 then return dz.HUM_COMFORTABLE
                elseif lv >= 55 and lv < 65 then return dz.HUM_NORMAL
                else return dz.HUM_WET
            end
        end
    
        local function getforecast( image ) 
            -- Define the BARO forcast.
            if image == "zonnig" or image == "helderenacht" then return dz.BARO_SUNNY
                elseif image == "halfbewolkt" or image == "mist" or image == "lichtbewolkt" or image == "nachtmist" then return dz.BARO_PARTLYCLOUDY
                elseif image == "bewolkt" or image == "zwaarbewolkt" or image == "wolkennacht" then return dz.BARO_CLOUDY
                elseif image == "regen" or image == 'halfbewolkt_regen' or image == "buien" or image == "hagel" or image == "sneeuw" then return dz.BARO_RAIN
                elseif image == "bliksem" then return dz.BARO_THUNDERSTORM
            else 
                return dz.BARO_NOINFO
            end
        end
            
        local function getIconNumber( image )
            if image == 'bewolkt' then iconNumber = 121 end
            if image == 'bliksem' then iconNumber = 122 end
            if image == 'hagel' then iconNumber = 123 end
            if image == 'halfbewolkt' then iconNumber = 124 end
            if image == 'helderenacht' then iconNumber = 125 end
            if image == 'lichtbewolkt' then iconNumber = 126 end
            if image == 'mist' then iconNumber = 127 end
            if image == 'nachtmist' then iconNumber = 128 end
            if image == 'regen' or image == 'buien' or image == 'halfbewolkt_regen' then iconNumber = 129 end
            if image == 'sneeuw' then iconNumber = 130 end
            if image == 'wolkennacht' or image == 'nachtbewolkt' then iconNumber = 131 end
            if image == 'zonnig' then iconNumber = 132 end
            if image == 'zwaarbewolkt' then iconNumber = 133 end
            -- For not existing image tekst called when no rain expected.
            if image == 'geenregen' then iconNumber = 134 end
            return iconNumber
        end

        -- Now start to do something ============
        -- Get the data.
		if (triggeredItem.isTimer) then
            -- Retrieve the data.
			dz.openURL({
				url = 'https://weerlive.nl/api/weerlive_api_v2.php?key=' .. wl_apikey .. '&locatie=' .. lat .. ',' .. long,
				method = 'GET',
				callback = 'weerlive'
			})
		end	

	    if (triggeredItem.isHTTPResponse) then
            -- Process the obtained data.
	        -- Check the response and process the data.
    		if (triggeredItem.ok and triggeredItem.isJSON) then
                dz.log( 'Item and JSON - OK', dz.LOG_DEBUG )
				-- We have some result. Store in table.
				local result_table = triggeredItem.json
                if type(result_table) == "table" then
                    dz.log( 'result_table: type = ' .. type(result_table), dz.LOG_DEBUG )
                    
                    --- API ---------------------------------------
                    local api_table = result_table['api']
                    if type(api_table) == "table" then
                        dz.log( 'api_table: type = ' .. type(api_table), dz.LOG_DEBUG )
                        -- The data we get is:
                        -- "bron": "Bron: Weerdata KNMI/NOAA via Weerlive.nl",
                        -- "max_verz": 300,
                        -- "rest_verz": 90
                        local rest = api_table[1].rest_verz
                        dz.log( 'Remaining calls to weerlive.nl api for today = ' .. rest, dz.LOG_DEBUG )
                    else
                        dz.log( 'No api_table found', dz.LOG_ERROR )
                    end

                    --- LIVEWEER ---------------------------------
                    local liveweer_table = result_table['liveweer']
                    if type(liveweer_table) == "table" then
                        dz.log( 'liveweer_table: type = ' .. type(liveweer_table), dz.LOG_DEBUG )
                        -- The data we get is:
                        --"plaats": "Your City",
                        --"timestamp": 1714650484,
                        --"time": "02-05-2024 13:48:04",
                        --"temp": 23.2,
                        --"gtemp": 22.7,
                        --"samenv": "Licht bewolkt",
                        --"lv": 64,
                        --"windr": "O",
                        --"windrgr": 91.8,
                        --"windms": 2.14,
                        --"windbft": 2,
                        --"windknp": 4.2,
                        --"windkmh": 7.7,
                        --"luchtd": 998.851,
                        --"ldmmhg": 749,
                        --"dauwp": 16.1,
                        --"zicht": 27600,
                        --"gr": 796,
                        --"verw": "Vanaf vanmiddag onweersbuien. Vrijdag koeler weer met af en toe regen",
                        --"sup": "06:06",
                        --"sunder": "21:11",
                        --"image": "lichtbewolkt",
                        --"alarm": 0,
                        --"lkop": "Vanmiddag en vanavond plaatselijk overlast door onweersbuien",
                        --"ltekst": " Tot halverwege de middag zijn er geen waarschuwingen van kracht.Later vanmiddag en vanavond komen er vooral in het midden en zuiden onweersbuien voor. Deze buien kunnen plaatselijk gepaard gaan met windstoten, hagel en veel regen. Hierdoor kan er hinder zijn voor verkeer en buitenactiviteiten. Ook kan er plaatselijk wateroverlast zijn.Later in de avond neemt het aantal buien af en worden ze minder zwaar.Wat kan ik verwachten en wat kan ik doen?Meer detailsBij een onweersbui windstoten tot ca. 65 km/u. Hagel tot 2 cm. Daarnaast plaatselijk neerslaghoeveelheden van 20-40 mm in een uur.",
                        --"wrschklr": "groen",
                        --"wrsch_g": "02-05-2024 21:00",
                        --"wrsch_gts": 1714676400,
                        --"wrsch_gc": "geel"

                        -- TempHumBaro.
                        local temp = liveweer_table[1].temp -- Also used in with the wind device.
                        if wl_thb_idx ~= 99999 then 
                            local lv = liveweer_table[1].lv
                            local status = gethumstat( lv )
                            local luchtd = liveweer_table[1].luchtd
                            -- Just a quick side trip here for tomorrow's image
                            dz.log( 'Image text for tomorrow = '.. result_table.wk_verw[2].image, dz.LOG_DEBUG )
                            local image = result_table.wk_verw[2].image
                            local forecast = getforecast( image )
                            dz.log( 'Update TempHumBaro: Temp: ' .. temp .. ', Hum: ' .. lv .. ', Status: ' .. status .. ', Luchtdr: ' .. luchtd .. ', Forecast:' .. forecast, dz.LOG_DEBUG )
                            dz.devices(wl_thb_idx).updateTempHumBaro( temp, lv, status, luchtd, forecast )
                        end

                        -- Apparent temperature
                        local gtemp = liveweer_table[1].gtemp -- Also used in with the wind device.
                        if wl_gtemp_idx ~= 99999 then 
                            dz.log( 'Update gTemp: ' .. gtemp, dz.LOG_DEBUG )
                            dz.devices(wl_gtemp_idx).updateTemperature( gtemp )
                        end

                        -- Wind
                        if wl_wind_idx ~= 99999 then  
                            local windms = _u.round( liveweer_table[1].windms, 1 )
                            local windrgr = liveweer_table[1].windrgr
                            local windDirectionString = _h.getDirectionfromDegree( windrgr )
                            --local windGust = result_table.current.wind_gusts_10m
                            dz.log( 'Update Windgr : ' .. windrgr .. ', Winddirection' .. windDirectionString .. ', Wind m/s: ' .. windms .. ', Gust: 0'  .. ', Temp: ' .. temp .. ', gTemp: ' .. gtemp, dz.LOG_DEBUG )
                            dz.devices(wl_wind_idx).updateWind( windrgr, windDirectionString, windms, 0, temp, gtemp )
                        end

                        -- Visibility
                        if wl_zicht_idx ~= 99999 then
                            local zicht = liveweer_table[1].zicht
                            dz.log( 'Update zicht: ' .. zicht / 1000, dz.LOG_DEBUG )
                            dz.devices(wl_zicht_idx).updateVisibility( zicht / 1000 )
                        end

                        -- Global radiation Watt/M2
                        if wl_gr_idx ~= 99999 then
                            local gr = liveweer_table[1].gr
                            dz.log( 'Update global radiation: ' .. gr, dz.LOG_DEBUG )
                            dz.devices(wl_gr_idx).updateRadiation( gr )                           
                        end
                        
                        -- Weatherforcast extensive text
                        if wl_txt_idx ~= 99999 then
                            local ltekst = liveweer_table[1].ltekst
                            --Space after sentences is often missing. Add a space to puntcation.
                            ltekst = string.gsub( ltekst, "(%.)", "%1 " )
                            ltekst = string.gsub( ltekst, "(%?)", "%1 " )
                            ltekst = string.gsub( ltekst, "etails", "etails " )
                            ltekst = string.gsub( ltekst, "etails :", "etails: " )
                            ltekst = string.gsub( ltekst, "etails ", "etails: " )
                            ltekst = string.gsub( ltekst, "  ", " " )
                            ltekst = _h.trimLRspaces( ltekst )
                            --Replace double spaces by one space.
                            --ltekst = string.gsub( ltekst, "%s%s", "%s" )
                            dz.log( 'Update extensive Text: "' .. ltekst .. '"', dz.LOG_DEBUG )
                            dz.devices(wl_txt_idx).updateText( '<span style="line-height:' .. lineHeight .. '">' .. ltekst  .. '</span>' )
                            
                            if wl_hideNowarning == 1 and ltekst == 'Er zijn momenteel geen waarschuwingen van kracht.' then
                                -- set hidden.
                                if string.sub( dz.devices(wl_txt_idx).name, 1, 1) ~= '$' then
                                    dz.log( 'Set ' .. dz.devices(wl_txt_idx).name .. ' hidden!', dz.LOG_DEBUG )
                                    dz.devices(wl_txt_idx).rename( '$' .. dz.devices(wl_txt_idx).name )
                                end
                            else
                                -- set visiblle
                                if string.sub( dz.devices(wl_txt_idx).name, 1, 1) == '$' then
                                    dz.log( 'Set ' .. dz.devices(wl_txt_idx).name .. ' visible!', dz.LOG_DEBUG )
                                    dz.devices(wl_txt_idx).rename( string.gsub( dz.devices(wl_txt_idx).name, '%$', '') )
                                end
                            end
                        end
                        
                        -- Weather alert
                        if wl_alarm_idx ~= 99999 then
                            local alarm = liveweer_table[1].alarm
                            local lkop = liveweer_table[1].lkop
                            local wrschklr = liveweer_table[1].wrschklr
                            local wrsch_g = liveweer_table[1].wrsch_g
                            local wrsch_gts = liveweer_table[1].wrsch_gts
                            local wrsch_gc = liveweer_table[1].wrsch_gc
                            local alertText = lkop

                            --Set the alert color
                            if wrschklr == 'rood' then
                                alertLevel = dz.ALERTLEVEL_RED
                            elseif wrschklr == 'oranje' then
                                alertLevel = dz.ALERTLEVEL_ORANGE
                            elseif wrschklr == 'geel' then
                                alertLevel = dz.ALERTLEVEL_YELLOW
                            else
                                alertLevel = dz.ALERTLEVEL_GREY
                            end

                            if wrsch_g ~= '-'  then
                                -- There is a Warning
                                if wrsch_gc == 'rood' then
                                    htmlbgColor = cRed
                                elseif wrsch_gc == 'oranje' then
                                    htmlbgColor = cOrange
                                elseif wrsch_gc == 'geel' then
                                    htmlbgColor = cYellow
                                else
                                    htmlbgColor = cWhite
                                end
                                -- Add warning time and message on new line with the warning color.
                                alertText = alertText .. '\n<span style="line-height:' .. lineHeight .. ';  background-color: ' .. htmlbgColor .. '">Vanaf ' .. wrsch_g .. ' geldt code ' .. wrsch_gc .. '.</span>'                                
                            end
                        
                            -- Update the alert device with new alertLevel and alertText.
                            dz.log( 'Update Weeralarm: ' .. alertText, dz.LOG_DEBUG )
                            dz.devices(wl_alarm_idx).updateAlertSensor(alertLevel, alertText )
                            
                            if wl_hideNowarning == 1 and lkop == 'Er zijn geen waarschuwingen' and wrsch_g == '-' and wrsch_gc == '-' then
                                -- set hidden.
                                if string.sub( dz.devices(wl_alarm_idx).name, 1, 1) ~= '$' then
                                    dz.log( 'Set ' .. dz.devices(wl_alarm_idx).name .. ' hidden!', dz.LOG_DEBUG )
                                    dz.devices(wl_alarm_idx).rename( '$' .. dz.devices(wl_alarm_idx).name )
                                end
                            else
                                -- set visiblle
                                if string.sub( dz.devices(wl_alarm_idx).name, 1, 1) == '$' then
                                    dz.log( 'Set ' .. dz.devices(wl_alarm_idx).name .. ' visible!', dz.LOG_DEBUG )
                                    dz.devices(wl_alarm_idx).rename( string.gsub( dz.devices(wl_alarm_idx).name, '%$', '') )
                                end
                            end                            
                        end

                        -- Summary and forecast used for wl_verw_idx
                        samenv = liveweer_table[1].samenv
                        verw =  liveweer_table[1].verw

                    else
                        dz.log( 'No liveweer_table found', dz.LOG_ERROR )
                    end

                    ---- WK_VERW ----------------------------------
                    local wk_verw_table = result_table['wk_verw']
                    if type(wk_verw_table) == "table" then
                        dz.log( 'wk_verw_table: type = ' .. type(wk_verw_table), dz.LOG_DEBUG )
                        -- The data we get is for 5 days, including today:
                        --"dag": "12-05-2024",
                        --"image": "halfbewolkt",
                        --"max_temp": 18,
                        --"min_temp": 17,
                        --"windbft": 3,
                        --"windkmh": 18,
                        --"windknp": 10,
                        --"windms": 5,
                        --"windrgr": 121,
                        --"windr": "ZO",
                        --"neersl_perc_dag": 50,
                        --"zond_perc_dag": 68

                        d1date = string.sub( wk_verw_table[1].dag, 1, 5 )   -- We use only 'dd-mm'
                        d1nslperc = wk_verw_table[1].neersl_perc_dag
                        d1znperc = wk_verw_table[1].zond_perc_dag
                        d1mintemp = wk_verw_table[1].min_temp
                        d1maxtemp = wk_verw_table[1].max_temp
                            
                        d2date = string.sub( wk_verw_table[2].dag, 1, 5 )   -- We use only 'dd-mm'
                        d2nslperc = wk_verw_table[2].neersl_perc_dag 
                        d2znperc = wk_verw_table[2].zond_perc_dag
                        d2mintemp = wk_verw_table[2].min_temp
                        d2maxtemp = wk_verw_table[2].max_temp

                        
                        if wl_mintoD_idx ~= 99999 then
                            dz.log( 'Update Min. Temp toDay: ' .. d1mintemp, dz.LOG_DEBUG )
                            dz.devices(wl_mintoD_idx).updateTemperature( d1mintemp )
                        end
                        if wl_maxtoD_idx ~= 99999 then
                            dz.log( 'Update Max. Temp toDay: ' .. d1maxtemp, dz.LOG_DEBUG )
                            dz.devices(wl_maxtoD_idx).updateTemperature( d1maxtemp )
                        end
                        if wl_mintoM_idx ~= 99999 then
                            dz.log( 'Update Min. Temp toMorrow: ' .. d2mintemp, dz.LOG_DEBUG )
                            dz.devices(wl_mintoM_idx).updateTemperature( d2mintemp )
                        end
                        if wl_maxtoM_idx ~= 99999 then
                            dz.log( 'Update Max. Temp toMorrow: ' .. d2maxtemp, dz.LOG_DEBUG )
                            dz.devices(wl_maxtoM_idx).updateTemperature( d2maxtemp )
                        end

                        if wl_verw_idx ~= 99999 then
                            -- To calculate the expected sun hours.
                            local sunrise = dz.time.sunriseInMinutes
                            local sunset = dz.time.sunsetInMinutes
                            
                            -- For today -------------------------------------
                            local suntimeMinutes = ( d1znperc / 100 ) * ( sunset - sunrise ) 
                            local sunHours = suntimeMinutes / 60
                            local integerPart, fractionalPart = math.modf( sunHours )
                            fractionalPart = _u.round( fractionalPart * 60, 0 )
                            local sunMinutes = string.format( "%02d", fractionalPart )
                            local sunTime = integerPart .. ':' .. sunMinutes .. ' uur'
                            dz.log( 'Calculated sun time today = ' .. sunTime, dz.LOG_DEBUG )

                            local texta = ''
                            if wl_mintoD_idx ~= 99999 and wl_maxtoD_idx ~= 99999 then
                                texta = d1date .. ': Verwachte uren zon ' .. sunTime
                            else
                                -- arrowup = &#9650; arrowdown = &#9660; Degree celsius = &#8451;
                                texta = d1date .. ': <span style="color: ' ..  cGrey .. '">&#9660;</span>Temp. ' .. d1mintemp .. ' &#8451; - <span style="color: ' ..  cGrey .. '">&#9650;</span>Temp. ' .. d1maxtemp .. ' &#8451; - Zon ' .. sunTime
                            end
                            dz.log( texta, dz.LOG_DEBUG )

                            -- For tomorrow ----------------------------------
                            local suntimeMinutes = ( d2znperc / 100 ) * ( sunset - sunrise ) 
                            local sunHours = suntimeMinutes / 60
                            local integerPart, fractionalPart = math.modf( sunHours )
                            fractionalPart = _u.round( fractionalPart * 60, 0 )
                            local sunMinutes = string.format( "%02d", fractionalPart )
                            local sunTime = integerPart .. ':' .. sunMinutes .. ' uur'
                            dz.log( 'Calculated sun time tomorrow = ' .. sunTime, dz.LOG_DEBUG )

                            local textb = ''
                            if wl_mintoM_idx ~= 99999 and wl_maxtoM_idx ~= 99999 then
                                textb = d2date .. ': Verwachte uren zon ' .. sunTime
                            else
                                -- arrowup = &#9650; arrowdown = &#9660; Degree celsius = &#8451;
                                textb = d2date .. ': <span style="color: ' ..  cGrey .. '">&#9660;</span>Temp. ' .. d2mintemp .. ' &#8451; - <span style="color: ' ..  cGrey .. '">&#9650;</span>Temp. ' .. d2maxtemp .. ' &#8451; - Zon ' .. sunTime
                            end
                            dz.log( textb, dz.LOG_DEBUG )

                            -- Summary and forcast text.
                            dz.log( 'Samenvatting: ' .. samenv, dz.LOG_DEBUG ) 
                            dz.log( 'Verwachting: ' .. verw, dz.LOG_DEBUG )
                            local verw_text = ''
                            if wl_uselocationurl ~= 1 then
                                verw_text = '<span style="color: ' ..  htmlColor .. '">' .. samenv .. '\n' .. verw .. '</span>\n' .. texta .. '\n' .. textb
                            else
                                verw_text = '<a href="' .. wl_locationurl .. '" target="_blank"><span style="color: ' ..  htmlColor .. '">'  .. samenv .. '</span></a>\n<span style="color: ' ..  htmlColor .. '">' .. verw .. '</span>\n' .. texta .. '\n' .. textb
                            end                                
                            -- Update with new Text.
                            dz.log( 'Update Verwachting: ' .. verw_text, dz.LOG_DEBUG )
                            dz.devices(wl_verw_idx).updateText( verw_text )

                            if wl_icons_installed == 1 then
                                -- Update icon for tomorrow forecast.
                                local image = wk_verw_table[2].image
                                local iconNumber = getIconNumber( image )
                                dz.devices(wl_verw_idx).setIcon(iconNumber)
                            end
                        end                        
                    else
                        dz.log( 'No wk_verw_table found', dz.LOG_ERROR )
                    end

                    ---- UUR_VERW ----------------------------------
                    local uur_verw_table = result_table['uur_verw']
                    if type(uur_verw_table) == "table" then
                        dz.log( 'uur_verw_table: type = ' .. type(uur_verw_table), dz.LOG_DEBUG )
                        -- The data we get is for 24 hr, starting from next whole hour:
                        --"uur": "13-05-2024 21:00",
                        --"timestamp": 1715626800,
                        --"image": "wolkennacht",
                        --"temp": 20,
                        --"windbft": 2,
                        --"windkmh": 7,
                        --"windknp": 4,
                        --"windms": 2,
                        --"windrgr": 130,
                        --"windr": "ZO",
                        --"neersl": 0,
                        --"gr": 6
                       
                        if wl_rain_idx ~= 99999 then 
                            -- Loop through the table. Search for the first rain occurence.
                            local tc = #uur_verw_table
                            for i = 1, tc do                    
                                -- if found: Rain expexted at 'uur'
                                -- else: No rain expected until (last time found.)
                                local neersl = uur_verw_table[i].neersl
                                local hr = uur_verw_table[i].uur
                                if neersl ~= 0 then
                                    countRain = countRain + neersl
                                    local unixTime = dz.time.dateToTimestamp( hr, 'dd-mm-yyyy hh:MM' )
                                    if firstRain == 'norain' then firstRain = dz.time.timestampToDate( unixTime, 'hh:MM' , 0 ) end
                                    lastRain = dz.time.timestampToDate( unixTime, 'hh:MM' , 3600 ) -- is the next hour. 
                                else
                                    if firstRain ~= 'norain' then
                                        -- neersl == 0 after first episode of rain, so exit the loop.
                                        break
                                    end
                                end
                            end
                            if firstRain == 'norain' then
                                rainText = 'Geen neerslag verwacht < 24 uur'
                                dz.log( rainText, dz.LOG_DEBUG )
                            else
                                rainText = '<span style="color: ' ..  htmlColor .. '">1<sup>e</sup> Neerslag (totaal ' .. countRain .. ' l/m<sup>2</sup>) tussen ' .. firstRain .. ' - ' .. lastRain .. ' uur</span>'
                                dz.log( rainText, dz.LOG_DEBUG )
                            end
                            local rainTexta = d1date .. ': Kans op neerslag ' .. d1nslperc .. '%.'
                            dz.log( rainTexta, dz.LOG_DEBUG )
                            local rainTextb = d2date .. ': Kans op neerslag ' .. d2nslperc .. '%.'
                            dz.log( rainTextb, dz.LOG_DEBUG )

                             -- Update with new Text.
                            dz.log( 'Update Regenverwachting: ' .. rainText .. '\n' .. rainTexta .. '\n' .. rainTextb, dz.LOG_DEBUG )
                            dz.devices(wl_rain_idx).updateText( rainText .. '\n' .. rainTexta .. '\n' .. rainTextb )

                            if wl_icons_installed == 1 then
                                if rainText == 'Geen neerslag verwacht < 24 uur' then
                                    local iconNumber = getIconNumber( 'geenregen')
                                    dz.devices(wl_rain_idx).setIcon(iconNumber)
                                else
                                    -- Get rain icon
                                    local iconNumber = getIconNumber( 'regen' ) 
                                    dz.devices(wl_rain_idx).setIcon(iconNumber)
                                end
                            end
                        end
                    else
                        dz.log( 'No uur_verw_table found', dz.LOG_ERROR )
                    end
                else
                    dz.log( 'No result_table found', dz.LOG_ERROR )
                end
		    else
			    dz.log( 'Item or JSON - NOT OK', dz.LOG_ERROR )
			end
	    end
	end
}
-- That's All --------------------------------------------------
A few functions are placed in the global_data script under 'helpers' (as you can also see explained in the dzvents wiki), because I call them from more than one script as can be seen in the comments.

Code: Select all

	------------------------------------------
	-- Global helper functions
	helpers = {
		------------------------
		-- Used in dt-OpenMeteo, t-Weerlive, t-Airplanes,
		getDirectionfromDegree = function( degrees )
			-- To calculate the degrees.
			local directionString = ''
			-- When string is not in Enlish, the icon does not appear!
			if degrees >= 0 and degrees < 11.25 then directionString = 'N' 
				elseif degrees >= 11.25 and degrees < 33.75 then directionString = 'NNE'
				elseif degrees >= 33.75 and degrees < 56.25 then directionString = 'NE'
				elseif degrees >= 56.25 and degrees < 78.75 then directionString = 'ENE'
				elseif degrees >= 78.75 and degrees < 101.25 then directionString = 'E'
				elseif degrees >= 101.25 and degrees < 123.75 then directionString = 'ESE'
				elseif degrees >= 123.75 and degrees < 146.25 then directionString = 'SE'
				elseif degrees >= 146.25 and degrees < 168.75 then directionString = 'SSE'
				elseif degrees >= 168.75 and degrees < 191.25 then directionString = 'S'
				elseif degrees >= 191.25 and degrees < 213.75 then directionString = 'SSW'
				elseif degrees >= 213.75 and degrees < 236.25 then directionString = 'SW'
				elseif degrees >= 236.25 and degrees < 258.75 then directionString = 'WSW'
				elseif degrees >= 258.75 and degrees < 281.25 then directionString = 'W'
				elseif degrees >= 281.25 and degrees < 303.75 then directionString = 'WNW'
				elseif degrees >= 303.75 and degrees < 326.25 then directionString = 'NW'
				elseif degrees >= 326.25 and degrees < 348.75  then directionString = 'NNW'
				elseif degrees >= 348.75 and degrees <= 360 then directionString = 'N' end
			return directionString
		end,
		------------------------
		-- Used in t-Weerlive,
		trimLRspaces = function( s )
			return ( s:gsub( "^%s*(.-)%s*$", "%1" ) )
		end,
		------------------------
	}
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
User avatar
waltervl
Posts: 5293
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by waltervl »

janpep wrote: Friday 10 January 2025 16:38 Again I did not receive notification of your message.
I will check if I can find a reason.
This seems to be an issue with the notifications when the forum was migrated to a new environment. See also
viewtopic.php?f=6&t=43045
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
janpep
Posts: 221
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Configurable 'KNMI Weerlive' script

Post by janpep »

waltervl wrote: Friday 10 January 2025 19:48 This seems to be an issue with the notifications when the forum was migrated to a new environment. See also
viewtopic.php?f=6&t=43045
Thanks for your service!
Domoticz in Ubuntu virtual machine on Synology DS718+ behind FRITZ!Box.
Using: EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest