Weerlive.nl  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Weerlive.nl

Post by erijk999 »

Hallo,

I have the following script running, and would like to add a little more. I am new to this area and could use a little push in the right direction.
I would like to add the following: min and max temperature and wind chill maybe even more later, but with some help from you I would be able to do that.

Code: Select all

-- KNMI weerlive.nl for domoticz
-- KNMI Weergegevens via Weerlive.nl
-- http://weerlive.nl/delen.php
-- add Domoticz forum link later
-- 2019-06-19 initial version

local apiKey = "local apiKey"
local location = "51.9991039,5.6747544"

-- create below devices in Domoticz
-- creat dummy hardware device WEERLIVE and add:
local wind  = 'weerlive wind'        -- wind
local baro  = 'weerlive barometer'   -- Temp + Humidity + Baro
local text  = 'weerlive'             -- text
local verw  = 'weerlive verwachting' -- text
local vis   = 'weerlive visibility'  -- visibility
local twind = 'weerlive twind'       -- text
local alarm = 'weerlive alarm'       -- text

return {
    logging = { 
            -- level = domoticz.LOG_DEBUG -- comment when not in dev
        },
    on = {
        timer = { 'every 10 minutes' },
        httpResponses = { 'get_weerlive' },
    },
    execute = function(dz, item) 
        if item.isTimer then
            dz.log('weerlive started by trigger: ' .. item.trigger, dz.LOG_INFO)

            dz.openURL({
                url = 'http://weerlive.nl/api/json-data-10min.php?key=' .. apiKey .. '&locatie=' .. location,
                method='GET',
                callback='get_weerlive'
            })
        elseif item.isHTTPResponse then

            dz.log('HTTPResponse event was triggered by ' .. item.trigger, dz.LOG_INFO)

            if (not item.ok or not item.isJSON) then
                dz.log('buienRadar call failed', dz.log_ERROR)
                dz.log('responseOK: ' .. tostring(item.ok), dz.LOG_INFO)
                dz.log('statusCode: ' .. item.statusCode, dz.LOG_INFO)
                dz.log('statusText: ' .. item.statusText, dz.LOG_INFO)
                dz.log('json: ', dz.LOG_INFO)
                dz.log(item.json, dz.LOG_INFO)
                return
            end

            if ( not item.json["liveweer"] ) then
                dz.log('weerlive data missing liveweer', dz.LOG_ERROR)
                return
            end

            local wr = { "Noord", "NNO", "NO", "ONO", "Oost", "OZO", "ZO", "ZZO", 
                "Zuid", "ZZW", "ZW", "WZW", "West", "WNW", "NW", "NNW" }            
            function w2d(w)
                for i,v in pairs(wr) do
                    if v == w then
                        return(22.5*(i-1))
                    end
                end
            end          
            function w2e(w)
                local we = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                    "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" }
                for i,v in pairs(wr) do
                    if v == w then
                        return we[i]
                    end
                end
            end


            local wl = item.json["liveweer"][1]
            dz.devices(wind).updateWind(w2d(wl["windr"]),w2e(wl["windr"]),
                wl['windms'],0,wl['temp'],wl['gtemp'])

            function lvstat(lv)
                lv = tonumber(lv)
                if lv < 35 then return dz.HUM_DRY
                elseif 35 < lv and lv < 45 then return dz.HUM_NORMAL
                elseif 45 < lv and lv < 55 then return dz.HUM_COMFORTABLE
                elseif 55 < lv and lv < 65 then return dz.HUM_NORMAL
                else return dz.HUM_WET
                end
            end
            -- dz.BARO_CLOUDY, dz.PARTLYCLOUDY, dz.BARO_STABLE, dz.BARO_SUNNY, 
            -- dz.BARO_THUNDERSTORM, dz.BARO_NOINFO, dz.BARO_UNSTABLE, 
            -- dz.BARO_RAIN
            function ic2fc(ic) 
                if ic == "zonnig" or ic =="helderenacht" then return dz.BARO_SUNNY
                elseif ic == "halfbewolkt" or ic == "mist" 
                    or ic == "nachtmist" then return dz.BARO_PARTLYCLOUDY
                elseif ic == "bewolkt" or ic == "zwaarbewolkt" 
                    or ic == "wolkennacht" then return dz.BARO_CLOUDY
                elseif ic == "regen" or ic == "buien" or ic == "hagel" 
                    or ic == "sneeuw" then return dz.BARO_RAIN
                elseif ic == "bliksem" then return dz.BARO_THUNDERSTORM
                else 
                    dz.log('unknown icon: ' .. ic, dz.LOG_ERROR)
                    return BARO_NOINFO
                end
            end
            dz.devices(baro).updateTempHumBaro(wl["temp"],wl["lv"], 
                lvstat(wl["lv"]), wl['luchtd'], ic2fc(wl['image']))

            local t = nil
            t = wl["samenv"]
            if dz.devices(text).text ~= t then 
                dz.devices(text).updateText(t)
            end
            t = wl["verw"]
            if dz.devices(verw).text ~= t then
                dz.devices(verw).updateText(t)
            end

            dz.devices(vis).updateVisibility(wl['zicht'])

            t = wl["windr"] .. ' ' .. wl["winds"] .. ' Bft'
            if dz.devices(twind).text ~= t then
                dz.devices(twind).updateText(t)
            end

            if wl["alarm"] == "0" then
                t = "no alarms"
            else
                t = wl['alarm'] .. ': ' .. wl['alarmtxt']
            end
            if dz.devices(alarm) ~= t then
                dz.devices(alarm).updateText(t)
            end



        end
    end
}
User avatar
kiddigital
Posts: 435
Joined: Thursday 10 August 2017 6:52
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Weerlive.nl

Post by kiddigital »

I guess that must be possible, but why not use the Domoticz native modules like Buienradar or Open Weather Map (Domoticz Beta version has a very extended OWM module)?

Do you miss anything in these maybe?
One RPi with Domoticz, RFX433e, aeon labs z-wave plus stick GEN5, ha-bridge 5.4.0 for Alexa, Philips Hue Bridge, Pimoroni Automation Hat
One RPi with Pi foundation standard touch screen to display Dashticz
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by erijk999 »

owm is not accurate enough for my location and buienradar also has no min and max temperature measurement
PatrickSt91
Posts: 27
Joined: Sunday 31 May 2020 10:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by PatrickSt91 »

Buienradar does have a min and max temperature. It even has max min, min min, and min max and max max.

Nevertheless I'm willing to help you out but I need to see the JSON output from http://weerlive.nl/api/json-data-10min.php?key=


So I can put the info in variables
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by erijk999 »

Hi Patrikst91,


is this what you mean?

{ "liveweer": [{"plaats": "Bennekom", "temp": "3.1", "gtemp": "-0.5", "samenv": "Half bewolkt", "lv": "97", "windr": "ZW", "windms": "4", "winds": "3", "windk": "7.8", "windkmh": "14.4", "luchtd": "1002.9", "ldmmhg": "752", "dauwp": "3", "zicht": "8", "verw": "Enkele buien. Morgen in het zuidoosten kans op natte sneeuw", "sup": "08:42", "sunder": "16:36", "image": "wolkennacht", "d0weer": "bewolkt", "d0tmax": "5", "d0tmin": "0", "d0windk": "3", "d0windknp": "8", "d0windms": "4", "d0windkmh": "15", "d0windr": "Z", "d0neerslag": "17", "d0zon": "7", "d1weer": "halfbewolkt_regen", "d1tmax": "3", "d1tmin": "1", "d1windk": "2", "d1windknp": "4", "d1windms": "2", "d1windkmh": "7", "d1windr": "ZO", "d1neerslag": "50", "d1zon": "20", "d2weer": "bewolkt", "d2tmax": "2", "d2tmin": "-1", "d2windk": "2", "d2windknp": "4", "d2windms": "2", "d2windkmh": "7", "d2windr": "ZW", "d2neerslag": "30", "d2zon": "10", "alarm": "0"}]}
PatrickSt91
Posts: 27
Joined: Sunday 31 May 2020 10:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by PatrickSt91 »

Yep that's what I meant, I'll make something tomorrow.

Gr,
Patrick
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by erijk999 »

Wow PatrickSt91,

Thank you very much
PatrickSt91
Posts: 27
Joined: Sunday 31 May 2020 10:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by PatrickSt91 »

Couldn't wait till tomorrow haha.

So here it is.

Please let me know if it work :)

I added 4;
- Wind chill
- Max temperature
- Min temperature
- Temperature

Code: Select all

-- KNMI weerlive.nl for domoticz
-- KNMI Weergegevens via Weerlive.nl
-- http://weerlive.nl/delen.php
-- add Domoticz forum link later
-- 2019-06-19 initial version
-- 2020-12-30 updated version


local apiKey = "local apiKey"
local location = "51.9991039,5.6747544"

-- create below devices in Domoticz
-- creat dummy hardware device WEERLIVE and add:
local wind  = 'weerlive wind'        -- wind
local baro  = 'weerlive barometer'   -- Temp + Humidity + Baro
local text  = 'weerlive'             -- text
local verw  = 'weerlive verwachting' -- text
local vis   = 'weerlive visibility'  -- visibility
local twind = 'weerlive twind'       -- text
local alarm = 'weerlive alarm'       -- text

-- 2020-12-30 Added temperature
local windc = 'weerlive windchill'	 -- temperature (Windchill) ADDED
local maxtp = 'weerlive maxtemp' 	 -- temperature (Max Temp) ADDED
local mintp = 'weerlive mintemp' 	 -- temperature (Min Temp) ADDED
local tempn = 'weerlive tempnow'	 -- temperature (Temperature) ADDED

return {
    logging = { 
            -- level = domoticz.LOG_DEBUG -- comment when not in dev
        },
    on = {
        timer = { 'every 10 minutes' },
        httpResponses = { 'get_weerlive' },
    },
    execute = function(dz, item) 
        if item.isTimer then
            dz.log('weerlive started by trigger: ' .. item.trigger, dz.LOG_INFO)

            dz.openURL({
                url = 'http://weerlive.nl/api/json-data-10min.php?key=' .. apiKey .. '&locatie=' .. location,
                method='GET',
                callback='get_weerlive'
            })
        elseif item.isHTTPResponse then

            dz.log('HTTPResponse event was triggered by ' .. item.trigger, dz.LOG_INFO)

            if (not item.ok or not item.isJSON) then
                dz.log('buienRadar call failed', dz.log_ERROR)
                dz.log('responseOK: ' .. tostring(item.ok), dz.LOG_INFO)
                dz.log('statusCode: ' .. item.statusCode, dz.LOG_INFO)
                dz.log('statusText: ' .. item.statusText, dz.LOG_INFO)
                dz.log('json: ', dz.LOG_INFO)
                dz.log(item.json, dz.LOG_INFO)
                return
            end

            if ( not item.json["liveweer"] ) then
                dz.log('weerlive data missing liveweer', dz.LOG_ERROR)
                return
            end

            local wr = { "Noord", "NNO", "NO", "ONO", "Oost", "OZO", "ZO", "ZZO", 
                "Zuid", "ZZW", "ZW", "WZW", "West", "WNW", "NW", "NNW" }            
            function w2d(w)
                for i,v in pairs(wr) do
                    if v == w then
                        return(22.5*(i-1))
                    end
                end
            end          
            function w2e(w)
                local we = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                    "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" }
                for i,v in pairs(wr) do
                    if v == w then
                        return we[i]
                    end
                end
            end


            local wl = item.json["liveweer"][1]
            dz.devices(wind).updateWind(w2d(wl["windr"]),w2e(wl["windr"]),
                wl['windms'],0,wl['temp'],wl['gtemp'])

            function lvstat(lv)
                lv = tonumber(lv)
                if lv < 35 then return dz.HUM_DRY
                elseif 35 < lv and lv < 45 then return dz.HUM_NORMAL
                elseif 45 < lv and lv < 55 then return dz.HUM_COMFORTABLE
                elseif 55 < lv and lv < 65 then return dz.HUM_NORMAL
                else return dz.HUM_WET
                end
            end
            -- dz.BARO_CLOUDY, dz.PARTLYCLOUDY, dz.BARO_STABLE, dz.BARO_SUNNY, 
            -- dz.BARO_THUNDERSTORM, dz.BARO_NOINFO, dz.BARO_UNSTABLE, 
            -- dz.BARO_RAIN
            function ic2fc(ic) 
                if ic == "zonnig" or ic =="helderenacht" then return dz.BARO_SUNNY
                elseif ic == "halfbewolkt" or ic == "mist" 
                    or ic == "nachtmist" then return dz.BARO_PARTLYCLOUDY
                elseif ic == "bewolkt" or ic == "zwaarbewolkt" 
                    or ic == "wolkennacht" then return dz.BARO_CLOUDY
                elseif ic == "regen" or ic == "buien" or ic == "hagel" 
                    or ic == "sneeuw" then return dz.BARO_RAIN
                elseif ic == "bliksem" then return dz.BARO_THUNDERSTORM
                else 
                    dz.log('unknown icon: ' .. ic, dz.LOG_ERROR)
                    return BARO_NOINFO
                end
            end
            dz.devices(baro).updateTempHumBaro(wl["temp"],wl["lv"], 
                lvstat(wl["lv"]), wl['luchtd'], ic2fc(wl['image']))

            local t = nil
            t = wl["samenv"]
            if dz.devices(text).text ~= t then 
                dz.devices(text).updateText(t)
            end
            t = wl["verw"]
            if dz.devices(verw).text ~= t then
                dz.devices(verw).updateText(t)
            end

            dz.devices(vis).updateVisibility(wl['zicht'])

            t = wl["windr"] .. ' ' .. wl["winds"] .. ' Bft'
            if dz.devices(twind).text ~= t then
                dz.devices(twind).updateText(t)
            end

            if wl["alarm"] == "0" then
                t = "no alarms"
            else
                t = wl['alarm'] .. ': ' .. wl['alarmtxt']
            end
            if dz.devices(alarm) ~= t then
                dz.devices(alarm).updateText(t)
            end

		-- 2020-12-30 Added temperature
		dz.devices(windc).updateTemperature(wl['gtemp'])
		dz.devices(maxtp).updateTemperature(wl['d0tmax'])
		dz.devices(mintp).updateTemperature(wl['d0tmin'])
		dz.devices(tempn).updateTemperature(wl['temp'])
			
        end
    end
}
Gr,
Patrick
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl  [Solved]

Post by erijk999 »

Hi Patrikst91,

works well
Super thanks
PatrickSt91
Posts: 27
Joined: Sunday 31 May 2020 10:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weerlive.nl

Post by PatrickSt91 »

Okay great to hear, enjoy!
mikkelnl
Posts: 15
Joined: Wednesday 21 June 2017 15:45
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Weerlive.nl

Post by mikkelnl »

Hi guys... I'd like to send an e-mail whenever the text changes for the 'general text' device that contains the 'weerbericht' content. Is this possible? I think it will need some scripting, and since I am fine editing/tweaking, I am not able to script such a thing myself ;-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Weerlive.nl

Post by waaren »

mikkelnl wrote: Friday 15 January 2021 11:57 Hi guys... I'd like to send an e-mail whenever the text changes for the 'general text' device that contains the 'weerbericht' content. Is this possible? I think it will need some scripting, and since I am fine editing/tweaking, I am not able to script such a thing myself ;-)
Can you try with this?

Code: Select all

-- KNMI weerlive.nl for domoticz
-- KNMI Weergegevens via Weerlive.nl
-- http://weerlive.nl/delen.php
-- add Domoticz forum link later
-- 2019-06-19 initial version
-- 2020-12-30 updated version
-- 2021-01-15 Added Email


local apiKey = "local apiKey"
local location = "51.9991039,5.6747544"

-- create below devices in Domoticz
-- creat dummy hardware device WEERLIVE and add:
local wind  = 'weerlive wind'        -- wind
local baro  = 'weerlive barometer'   -- Temp + Humidity + Baro
local text  = 'weerlive'             -- text
local verw  = 'weerlive verwachting' -- text
local vis   = 'weerlive visibility'  -- visibility
local twind = 'weerlive twind'       -- text
local alarm = 'weerlive alarm'       -- text

-- 2020-12-30 Added temperature
local windc = 'weerlive windchill'	 -- temperature (Windchill) ADDED
local maxtp = 'weerlive maxtemp' 	 -- temperature (Max Temp) ADDED
local mintp = 'weerlive mintemp' 	 -- temperature (Min Temp) ADDED
local tempn = 'weerlive tempnow'	 -- temperature (Temperature) ADDED

local email = '[email protected]' -- set to false if you don't want an Email
 
return {
    logging = { 
            -- level = domoticz.LOG_DEBUG -- comment when not in dev
        },
    on = {
        timer = { 'every 10 minutes' },
        httpResponses = { 'get_weerlive' },
    },
    execute = function(dz, item) 
        if item.isTimer then
            dz.log('weerlive started by trigger: ' .. item.trigger, dz.LOG_INFO)

            dz.openURL({
                url = 'http://weerlive.nl/api/json-data-10min.php?key=' .. apiKey .. '&locatie=' .. location,
                method='GET',
                callback='get_weerlive'
            })
        elseif item.isHTTPResponse then

            dz.log('HTTPResponse event was triggered by ' .. item.trigger, dz.LOG_INFO)

            if (not item.ok or not item.isJSON) then
                dz.log('Weerlive call failed', dz.log_ERROR)
                dz.log('responseOK: ' .. tostring(item.ok), dz.LOG_INFO)
                dz.log('statusCode: ' .. item.statusCode, dz.LOG_INFO)
                dz.log('statusText: ' .. item.statusText, dz.LOG_INFO)
                dz.log('json: ', dz.LOG_INFO)
                dz.log(item.json, dz.LOG_INFO)
                return
            end

            if ( not item.json["liveweer"] ) then
                dz.log('weerlive data missing liveweer', dz.LOG_ERROR)
                return
            end

            local wr = { "Noord", "NNO", "NO", "ONO", "Oost", "OZO", "ZO", "ZZO", 
                "Zuid", "ZZW", "ZW", "WZW", "West", "WNW", "NW", "NNW" }            
            function w2d(w)
                for i,v in pairs(wr) do
                    if v == w then
                        return(22.5*(i-1))
                    end
                end
            end          
            function w2e(w)
                local we = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                    "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" }
                for i,v in pairs(wr) do
                    if v == w then
                        return we[i]
                    end
                end
            end


            local wl = item.json["liveweer"][1]
            dz.devices(wind).updateWind(w2d(wl["windr"]),w2e(wl["windr"]),
                wl['windms'],0,wl['temp'],wl['gtemp'])

            function lvstat(lv)
                lv = tonumber(lv)
                if lv < 35 then return dz.HUM_DRY
                elseif 35 < lv and lv < 45 then return dz.HUM_NORMAL
                elseif 45 < lv and lv < 55 then return dz.HUM_COMFORTABLE
                elseif 55 < lv and lv < 65 then return dz.HUM_NORMAL
                else return dz.HUM_WET
                end
            end
            -- dz.BARO_CLOUDY, dz.PARTLYCLOUDY, dz.BARO_STABLE, dz.BARO_SUNNY, 
            -- dz.BARO_THUNDERSTORM, dz.BARO_NOINFO, dz.BARO_UNSTABLE, 
            -- dz.BARO_RAIN
            function ic2fc(ic) 
                if ic == "zonnig" or ic =="helderenacht" then return dz.BARO_SUNNY
                elseif ic == "halfbewolkt" or ic == "mist" 
                    or ic == "nachtmist" then return dz.BARO_PARTLYCLOUDY
                elseif ic == "bewolkt" or ic == "zwaarbewolkt" 
                    or ic == "wolkennacht" then return dz.BARO_CLOUDY
                elseif ic == "regen" or ic == "buien" or ic == "hagel" 
                    or ic == "sneeuw" then return dz.BARO_RAIN
                elseif ic == "bliksem" then return dz.BARO_THUNDERSTORM
                else 
                    dz.log('unknown icon: ' .. ic, dz.LOG_ERROR)
                    return BARO_NOINFO
                end
            end
            dz.devices(baro).updateTempHumBaro(wl["temp"],wl["lv"], 
                lvstat(wl["lv"]), wl['luchtd'], ic2fc(wl['image']))

            local t = nil
            t = wl["samenv"]
            if dz.devices(text).text ~= t then 
                dz.devices(text).updateText(t)
                if email then dz.email('Weerbericht', t, email) end
            end
            t = wl["verw"]
            if dz.devices(verw).text ~= t then
                dz.devices(verw).updateText(t)
            end

            dz.devices(vis).updateVisibility(wl['zicht'])

            t = wl["windr"] .. ' ' .. wl["winds"] .. ' Bft'
            if dz.devices(twind).text ~= t then
                dz.devices(twind).updateText(t)
            end

            if wl["alarm"] == "0" then
                t = "no alarms"
            else
                t = wl['alarm'] .. ': ' .. wl['alarmtxt']
            end
            if dz.devices(alarm) ~= t then
                dz.devices(alarm).updateText(t)
            end

		-- 2020-12-30 Added temperature
		dz.devices(windc).updateTemperature(wl['gtemp'])
		dz.devices(maxtp).updateTemperature(wl['d0tmax'])
		dz.devices(mintp).updateTemperature(wl['d0tmin'])
		dz.devices(tempn).updateTemperature(wl['temp'])
			
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Weerlive.nl

Post by erem »

@waaren
copy/paste got you again :D :D

>> dz.log('buienRadar call failed', dz.log_ERROR)
Regards,

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

Re: Weerlive.nl

Post by waaren »

erem wrote: Friday 15 January 2021 13:23 copy/paste got you again :D :D

>> dz.log('buienRadar call failed', dz.log_ERROR)
Thx, ...but I haven't touched that part so it was already in the script. Nevertheless I will correct it in my post.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
mikkelnl
Posts: 15
Joined: Wednesday 21 June 2017 15:45
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Weerlive.nl

Post by mikkelnl »

waaren wrote: Friday 15 January 2021 12:42
mikkelnl wrote: Friday 15 January 2021 11:57 Hi guys... I'd like to send an e-mail whenever the text changes for the 'general text' device that contains the 'weerbericht' content. Is this possible? I think it will need some scripting, and since I am fine editing/tweaking, I am not able to script such a thing myself ;-)
Can you try with this?

Code: Select all

-- KNMI weerlive.nl for domoticz
-- KNMI Weergegevens via Weerlive.nl
-- http://weerlive.nl/delen.php
-- add Domoticz forum link later
-- 2019-06-19 initial version
-- 2020-12-30 updated version
-- 2021-01-15 Added Email


local apiKey = "local apiKey"
local location = "51.9991039,5.6747544"

-- create below devices in Domoticz
-- creat dummy hardware device WEERLIVE and add:
local wind  = 'weerlive wind'        -- wind
local baro  = 'weerlive barometer'   -- Temp + Humidity + Baro
local text  = 'weerlive'             -- text
local verw  = 'weerlive verwachting' -- text
local vis   = 'weerlive visibility'  -- visibility
local twind = 'weerlive twind'       -- text
local alarm = 'weerlive alarm'       -- text

-- 2020-12-30 Added temperature
local windc = 'weerlive windchill'	 -- temperature (Windchill) ADDED
local maxtp = 'weerlive maxtemp' 	 -- temperature (Max Temp) ADDED
local mintp = 'weerlive mintemp' 	 -- temperature (Min Temp) ADDED
local tempn = 'weerlive tempnow'	 -- temperature (Temperature) ADDED

local email = '[email protected]' -- set to false if you don't want an Email
 
return {
    logging = { 
            -- level = domoticz.LOG_DEBUG -- comment when not in dev
        },
    on = {
        timer = { 'every 10 minutes' },
        httpResponses = { 'get_weerlive' },
    },
    execute = function(dz, item) 
        if item.isTimer then
            dz.log('weerlive started by trigger: ' .. item.trigger, dz.LOG_INFO)

            dz.openURL({
                url = 'http://weerlive.nl/api/json-data-10min.php?key=' .. apiKey .. '&locatie=' .. location,
                method='GET',
                callback='get_weerlive'
            })
        elseif item.isHTTPResponse then

            dz.log('HTTPResponse event was triggered by ' .. item.trigger, dz.LOG_INFO)

            if (not item.ok or not item.isJSON) then
                dz.log('Weerlive call failed', dz.log_ERROR)
                dz.log('responseOK: ' .. tostring(item.ok), dz.LOG_INFO)
                dz.log('statusCode: ' .. item.statusCode, dz.LOG_INFO)
                dz.log('statusText: ' .. item.statusText, dz.LOG_INFO)
                dz.log('json: ', dz.LOG_INFO)
                dz.log(item.json, dz.LOG_INFO)
                return
            end

            if ( not item.json["liveweer"] ) then
                dz.log('weerlive data missing liveweer', dz.LOG_ERROR)
                return
            end

            local wr = { "Noord", "NNO", "NO", "ONO", "Oost", "OZO", "ZO", "ZZO", 
                "Zuid", "ZZW", "ZW", "WZW", "West", "WNW", "NW", "NNW" }            
            function w2d(w)
                for i,v in pairs(wr) do
                    if v == w then
                        return(22.5*(i-1))
                    end
                end
            end          
            function w2e(w)
                local we = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                    "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" }
                for i,v in pairs(wr) do
                    if v == w then
                        return we[i]
                    end
                end
            end


            local wl = item.json["liveweer"][1]
            dz.devices(wind).updateWind(w2d(wl["windr"]),w2e(wl["windr"]),
                wl['windms'],0,wl['temp'],wl['gtemp'])

            function lvstat(lv)
                lv = tonumber(lv)
                if lv < 35 then return dz.HUM_DRY
                elseif 35 < lv and lv < 45 then return dz.HUM_NORMAL
                elseif 45 < lv and lv < 55 then return dz.HUM_COMFORTABLE
                elseif 55 < lv and lv < 65 then return dz.HUM_NORMAL
                else return dz.HUM_WET
                end
            end
            -- dz.BARO_CLOUDY, dz.PARTLYCLOUDY, dz.BARO_STABLE, dz.BARO_SUNNY, 
            -- dz.BARO_THUNDERSTORM, dz.BARO_NOINFO, dz.BARO_UNSTABLE, 
            -- dz.BARO_RAIN
            function ic2fc(ic) 
                if ic == "zonnig" or ic =="helderenacht" then return dz.BARO_SUNNY
                elseif ic == "halfbewolkt" or ic == "mist" 
                    or ic == "nachtmist" then return dz.BARO_PARTLYCLOUDY
                elseif ic == "bewolkt" or ic == "zwaarbewolkt" 
                    or ic == "wolkennacht" then return dz.BARO_CLOUDY
                elseif ic == "regen" or ic == "buien" or ic == "hagel" 
                    or ic == "sneeuw" then return dz.BARO_RAIN
                elseif ic == "bliksem" then return dz.BARO_THUNDERSTORM
                else 
                    dz.log('unknown icon: ' .. ic, dz.LOG_ERROR)
                    return BARO_NOINFO
                end
            end
            dz.devices(baro).updateTempHumBaro(wl["temp"],wl["lv"], 
                lvstat(wl["lv"]), wl['luchtd'], ic2fc(wl['image']))

            local t = nil
            t = wl["samenv"]
            if dz.devices(text).text ~= t then 
                dz.devices(text).updateText(t)
                if email then dz.email('Weerbericht', t, email) end
            end
            t = wl["verw"]
            if dz.devices(verw).text ~= t then
                dz.devices(verw).updateText(t)
            end

            dz.devices(vis).updateVisibility(wl['zicht'])

            t = wl["windr"] .. ' ' .. wl["winds"] .. ' Bft'
            if dz.devices(twind).text ~= t then
                dz.devices(twind).updateText(t)
            end

            if wl["alarm"] == "0" then
                t = "no alarms"
            else
                t = wl['alarm'] .. ': ' .. wl['alarmtxt']
            end
            if dz.devices(alarm) ~= t then
                dz.devices(alarm).updateText(t)
            end

		-- 2020-12-30 Added temperature
		dz.devices(windc).updateTemperature(wl['gtemp'])
		dz.devices(maxtp).updateTemperature(wl['d0tmax'])
		dz.devices(mintp).updateTemperature(wl['d0tmin'])
		dz.devices(tempn).updateTemperature(wl['temp'])
			
        end
    end
}
Thanks, did not get an email :( is there a log to check?

edit: Never mind, my bad, works fine, thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest