KNMI weeralert with dzvents

Moderator: leecollings

Post Reply
User avatar
Hansh
Posts: 22
Joined: Sunday 17 February 2019 14:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Rotterdam
Contact:

KNMI weeralert with dzvents

Post by Hansh »

Inspired by the block script from FireWizard I made a script in dzvents to bring the weather alarm from the KNMI to domoticz

You can request the local weather from KNMI via an API, see https://weerlive.nl/delen.php
All data is very usable and on this forum you can find scripts that process the data.
However, the weather alarm is the general alarm for the Netherlands and is not really useful for the local level.

The RSS feed of the KNMI only provides the weather alarm, but then per province, see https://cdn.knmi.nl/knmi/xml/rss/rss_KN ... wingen.xml

To process this rss in domoticz I made a script which filters out the weather alarm and the alarm text of the desired province

Below is the DzVents script

Code: Select all

local url = 'https://cdn.knmi.nl/knmi/xml/rss/rss_KNMIwaarschuwingen.xml' -- rss url
local scriptVar = 'WeerAlert' -- callback name

local WeerAlertDevice  = 'Weerwaarschuwing' -- name of alert device in domotcicz

local codes = {"Groen","Geel","Oranje","Rood"} -- available codes
local PV = 11 -- Province code
--[[ codes for the different provinces
2=Waddeneilanden        3=Groningen         4=Friesland         5=Drente
6=Noord-Holland         7=Flevoland         8=Overijsel         9=Gelderland
10=Utrecht              11=Zuid-Holland     12=Zeeland          13=Noord-Brabant
14=Limburg              15=Waddenzee        16=ijselmeer-gebied
--]]

return 
{
    on = 
    {
        timer = 
        {
            'at *:56',
        },
        
        httpResponses =
        {
            scriptVar,
        },
    },


         -- ***** No changes required below this line *****

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = scriptVar,
    },
    
    execute = function(dz, item)
    
    local Weeralarm = dz.devices(WeerAlertDevice) --reference to the weather alarm device
        
        --Retrieve desired data from the returned xml file
        local function getData(data)
            rt = dz.utils.fromXML(data).rss.channel.item[PV]
            RelevanteData={["code"]=0,["text"]="Geen data"}
            --dz.log(rt["description"],dz.LOG_DEBUG) 
            --rt["description"]="Code Geel.<br>Enkele onweersbuien met kans op hagel.<p><a href='/kennis-en-datacentrum/waarschuwingen/onweersbuien'>Wat kan ik verwachten en wat kan ik doen bij onweersbuien?</a> (van 01/08/2023 10:00 tot 01/08/2023 16:00 uur)<br><br>"
            for key,value in ipairs(codes) do
                if string.match(rt["description"],value) then
                    RelevanteData["code"]=key
                    RelevanteData["text"]=rt["description"]:gsub("<a.-a>","")
                end
            end
            return RelevanteData
        end
        
        --update the specified device in domoticz
        local function UpdateDevice(data)
            local lastStatus = "0"
            lastStatus= tostring(Weeralarm.color)
            Weeralarm.updateAlertSensor(data["code"],data["text"])
            --un comment the next 3 lines to send a notification when the status has changed(this one is for telegram)
            --if tostring(data["code"]) ~= lastStatus then 
            --    dz.notify('Domoticz', 'Weeralarm: \n'.. data["text"]:gsub("<.->",""), dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , dz.NSS_TELEGRAM)
            --end  
        end
        
        if item.isHTTPResponse then
            if item.ok then
                UpdateDevice(getData(item.data))
            else
                dz.log('There was a problem handling the request', dz.LOG_ERROR)
                dz.log(item, dz.LOG_ERROR)
            end
            return
        end

        dz.openURL(
        {
            url = url, 
            method = 'GET',
            callback = scriptVar,
        })
    end
}
In order for the script to work, a device of the type: General Alert must be created in domotcz

Operation of the script:

In the top lines the div variables can be edited
In order for the script to work properly, only two need to be changed

local WeerAlertDevice = must contain the name of the domoticz device Here Weather alert


local PV = must contain the code for the province
2=Waddeneilanden 3=Groningen 4=Friesland 5=Drente
6=Noord-Holland 7=Flevoland 8=Overijsel 9=Gelderland
10=Utrecht 11=Zuid-Holland 12=Zeeland 13=Noord-Brabant
14=Limburg 15=Waddenzee 16=ijselmeer-gebied

Thanks again to FireWizard for the inspiration

regards
Hansh
All my texts are translated from Dutch to English by Google translate>
Sorry for weird sentence structure
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest