Custom page Alarmeringen.nl by script
Posted: Monday 20 May 2024 14:56
Now that I am rewriting my scripts in a new format, I would like to share some of these projects here. Maybe it will help someone.
Emergency services often pass by with sirens. Sometimes I wonder what is or was going on. One time it turned out to be a chimney fire just a few houses away.
All alarm reports in the Netherlands are published and can be accessed per region or city via RSS feed.
You can find your place at: https://alarmeringen.nl
Here you see the spelling of the name of your own city, to then use it in the URL.
Example of the data obtained for Alphen aan den Rijn: https://alarmeringen.nl/feeds/city/alph ... n-rijn.rss
I parsed this XML data in a script to post the latest notifications. I did not find it useful to put this information in a text device.
The space is too limited and it doesn't look nice. That's why I decided to place it in a custom HTML page, which is entirely created via the script.
This page can be called up via the Custom menu. See the picture below.
The result: The first line in the description shows codes and overlaps with the second line. I kept it anyway, because it sometimes provides additional information.
The icons that are also used on the website: Correct and double check Your settings, cityname, the full path to your domoticz/www/template directory and URL in the script.
t-Alarmeringen:
Emergency services often pass by with sirens. Sometimes I wonder what is or was going on. One time it turned out to be a chimney fire just a few houses away.
All alarm reports in the Netherlands are published and can be accessed per region or city via RSS feed.
You can find your place at: https://alarmeringen.nl
Here you see the spelling of the name of your own city, to then use it in the URL.
Example of the data obtained for Alphen aan den Rijn: https://alarmeringen.nl/feeds/city/alph ... n-rijn.rss
I parsed this XML data in a script to post the latest notifications. I did not find it useful to put this information in a text device.
The space is too limited and it doesn't look nice. That's why I decided to place it in a custom HTML page, which is entirely created via the script.
This page can be called up via the Custom menu. See the picture below.
- The script places the latest notifications in a dynamically created table with time, icon and description of the notification.
- I have placed three pictures with these icons of ambulance, fire brigade and police in the directory: /home/user/domoticz/www/templates/images.
- The script refers to that. The images are used as a link to further information on the website.
- The html file is placed in: /home/<username>/domoticz/www/templates by the script.
- Your own street name can be entered in the script and a notification email will be sent when there is an emergency in your own street.
- NB. A variable 'lastAlarmNotification' is placed in global_data, to ensure that a notification that has already been sent for your own street will not be given multiple times. Create that as:
Code: Select all
-- Global persistent data data = { -- Used in t-Alarmeringen for Alarmeringen.nl. lastAlarmNotification = {}, },
- NB. Forgotten and added later: The script also refers to a global function in the global_data.
Code: Select all
-- Global helper functions helpers = { getUTCtimediffseconds = function( qUnixtime ) local timezone = os.date('%z', qUnixtime) -- "+0200" local signum, hours, minutes = timezone:match '([+-])(%d%d)(%d%d)' local lTimediff = (tonumber(signum..hours)*3600 + tonumber(signum..minutes)*60) return lTimediff end, }
The result: The first line in the description shows codes and overlaps with the second line. I kept it anyway, because it sometimes provides additional information.
The icons that are also used on the website: Correct and double check Your settings, cityname, the full path to your domoticz/www/template directory and URL in the script.
t-Alarmeringen:
Code: Select all
-- 03-12-2022 script by Jan Peppink, https://ict.peppink.nl
-- Creates a custom html page with x lines of the last alarm notifications for the city.
-- icon images are expected to be placed. See Your settings.
--- Your settings ----------------------------------------------------------------
-- First set the used device index numbers and variables you might want to change.
local maxLines = 10 -- Max number of lines to show in the table.
local locationToSearch = 'yourStreetName' -- Get a notification when streetname is found.
local al_emailTo = "[email protected]" -- Set E-mail adres to sent notification to.
local cityName = 'Name Of Your City' -- Used in header, and used to strip from text.
-- Use the same spelling that is found in description!
--Set the Fullpath to the html file. Example '/home/username/domoticz/www/templates/Alarmeringen.html'
local htmlFile = '/home/<username>/domoticz/www/templates/Alarmeringen.html'
--Set imageDir, where the icon_ambulance.web, icon_brandweer.webp, icon_politie.webp images are expected
local imageDir = '/templates/images' -- starting from root
--Set the URL to get the rss for your city. Find city name at: https://alarmeringen.nl/plaatsen.html
-- Example region = 'http://alarmeringen.nl/feeds/region/hollands-midden.rss'
-- Example city = 'https://alarmeringen.nl/feeds/city/alphen-aan-den-rijn.rss'
local alarmeringURL = 'https://alarmeringen.nl/feeds/city/<URLnamefoundforyourcity>.rss'
return {
on = {
timer = {
'every 5 minutes',
},
httpResponses = {
'alarmering' -- must match with the callback passed to the openURL command
}
},
logging = {
-- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
level = domoticz.LOG_INFO,
--level = domoticz.LOG_DEBUG,
marker = 'Alarmering-',
},
execute = function(dz, triggeredItem)
-- Set Local environment=================
local _u = dz.utils
local _h = dz.helpers
local _d = dz.globalData
local locationFound = false
-- Local Functions go here =============
-- Now start to do something ============
if (triggeredItem.isTimer) then
dz.openURL({
url = alarmeringURL,
method = 'GET',
callback = 'alarmering', -- see httpResponses above.
})
end
if (triggeredItem.isHTTPResponse) then
-- Process the obtained data.
if (triggeredItem.ok and triggeredItem.isXML) then
-- Results are in:
-- triggeredItem.xml.rss.channel.description -Alarmeringen.nl: Alle alarmeringen voor: Hollands Midden
-- triggeredItem.xml.rss.channel.item
-- triggeredItem.xml.rss.channel.item.title -a1 coornhertdreef leiddp : 16161
-- triggeredItem.xml.rss.channel.item.description -Ambulance met spoed naar Coornhertdreef in Leiderdorp
-- triggeredItem.xml.rss.channel.item.pubDate -Fri, 09 Dec 2022 08:20:16 +0000
-- utcTimeDiffSeconds to calculate local time from resulting pupDate
local utcTimeDiffSeconds = _h.getUTCtimediffseconds( dz.time.dDate )
dz.log( 'utcTimeDiffSeconds = ' .. utcTimeDiffSeconds, dz.LOG_DEBUG )
local result_table = triggeredItem.xml.rss.channel.item
if type( result_table ) == "table" then
dz.log( 'Existing result_table: type = ' .. type( result_table ), dz.LOG_DEBUG )
-- Witing the custom HTML file ----------------
--Step 1. Remove old file and Open file for appending
dz.log( 'html file = ' .. htmlFile, dz.LOG_DEBUG )
if _u.fileExists( htmlFile) then os.remove( htmlFile ) end
local file = io.open( htmlFile, "a" )
--Step 2. Write fileHeader ----------------
local fileHeader = '<!DOCTYPE html>\n' ..
'<html>\n' ..
' <head>\n' ..
' <title>Laatste ' .. maxLines .. ' Alarmeringen.nl meldingen</title>\n' ..
' </head>\n' ..
' <body bgcolor="black">\n' ..
' <h1 style="text-align:center; color: white;">Alarmeringen.nl voor ' .. cityName .. ' </h1>\n' ..
'<p></p>' ..
' <table border="1" style="border-collapse:collapse; margin-left:auto; margin-right:auto; color: yellow;" bgcolor="black"\n' ..
' <tr>\n' ..
' <th align="left" style="padding: 3px 10px 3px 10px; color: cyan;">Tijd</th>\n' ..
' <th colspan="2" align="left" style="padding: 3px 10px 3px 10px; color: cyan;">Laatste ' .. maxLines .. ' meldingen</th>\n' ..
' </tr>\n'
file:write( fileHeader )
--Step 3. Get and write tableContent in loop ----------------
local tc = #result_table
local counter = 0
local tableContent = ''
for i = 1, tc do
--Interesting fields - example:
--title: a2 pieter floriszstraat alphrn directe inzet 16162
--link: http://alarmeringen.nl/zuid-holland/hollands-midden/alphen-aan-den-rijn/?utm_source=rss&utm_medium=hollands-midden&utm_campaign=sharing
--description: Ambulance met gepaste spoed naar Pieter Floriszstraat in Alphen Aan Den Rijn
--pubDate: Fri, 09 Dec 2022 08:13:18 +0000
--
--result_table[i].description
--result_table[i].title
--result_table[i].pubDate
if result_table[i].description ~= nil then
-- Skip if Ambulance = voorwaarde scheppend to
if string.find( result_table[i].description, 'voorwaarde scheppend' ) == nil then
--This string is not found so continue
counter = counter + 1
if counter <= maxLines then
dz.log( '---------------- ', dz.LOG_DEBUG )
dz.log( 'Title: ' .. result_table[i].title, dz.LOG_DEBUG )
dz.log( 'Link: ' .. result_table[i].link, dz.LOG_DEBUG )
dz.log( 'Description: ' .. result_table[i].description, dz.LOG_DEBUG )
dz.log( 'pubDate: ' .. result_table[i].pubDate, dz.LOG_DEBUG )
-- date received. Format = Thu, 08 Dec 2022 01:37:39 +0000
local rowDate = dz.time.dateToDate( result_table[i].pubDate, 'ddd, dd mmm yyyy hh:MM:ss +0000', 'dd-mm-yyyy hh:MM', utcTimeDiffSeconds )
local rowLink = result_table[i].link
local rowTitle = result_table[i].title
-- trim off the cityName from description (case insensitive)
local rowDescription = string.gsub( result_table[i].description, ' in ' .. cityName, ".")
-- set rowImage based on description
local rowImage = imageDir .. '/icon_politie.webp' --Is default, because 'Politie' is often not used in description.
if string.find( result_table[i].description, "Ambulance") then rowImage = imageDir .. '/icon_ambulance.webp' end
if string.find( result_table[i].description, "Brandweer") then rowImage = imageDir .. '/icon_brandweer.webp' end
if string.find( result_table[i].description, "Politie") then rowImage = imageDir .. '/icon_politie.webp' end
dz.log( counter .. ': ' .. rowDate .. ' - ' .. rowDescription, dz.LOG_DEBUG )
-- Send notification when locationToSearch in in description
if string.find( result_table[i].description, locationToSearch ) then locationFound = true end
tableContent = tableContent .. ' <tr>\n' ..
' <td align="left" valign="middle" style="padding: 3px 10px 5px 10px">' .. rowDate .. '</td>\n' ..
' <td align="left" valign="middle" style="padding: 3px 10px 5px 10px"><a target="_blank" rel="noopener" href="' .. rowLink.. '"><img src="' .. rowImage.. '" style="width:34px; height:25px"></a></td> \n' ..
' <td align="left" valign="middle" style="padding: 3px 10px 5px 10px">'.. rowTitle .. '</br>' .. rowDescription .. '</td> \n' ..
' </tr> \n'
end
end
end
end
--Step 4. Append table footer with update time and close the table -----
local currentTime = dz.time.rawDateTime
local updateTime = dz.time.dateToDate( currentTime, 'yyyy-mm-dd hh:MM:ss', 'dd-mm-yyyy hh:MM', 0 )
dz.log( 'updateTime = ' .. updateTime, dz.LOG_DEBUG )
tableContent = tableContent .. ' <tr>\n' ..
' <td colspan="3" align="right" style="padding: 3px 10px 3px 10px; color: cyan;">Update: ' .. updateTime .. ' uur</td>\n' ..
' </tr>\n' ..
' </table>\n'
--Step 5. Write tableContent ----------------
file:write(tableContent)
--Step 6. Write fileFooter ------------------
local fileFooter = ' </body>\n' ..
'</html>'
file:write(fileFooter)
--Step 7. Close file ------------------------
file:close()
----------------------------------------
-- Only one notification per session when location is found
--_d.lastAlarmNotification format = '2022-12-10'
-- dz.log( 'lastAlarmNotification = ' .. _d.lastAlarmNotification, dz.LOG_DEBUG )
if locationFound == true then
if _d.lastAlarmNotification == nil or _d.lastAlarmNotification < dz.time.rawDate then
_d.lastAlarmNotification = dz.time.rawDate
dz.log( locationToSearch .. ' found = ' .. tostring( locationFound ) .. ' on ' .. _d.lastAlarmNotification, dz.LOG_DEBUG )
dz.email( 'Alarmering.nl voor ' .. locationToSearch .. '!', '', ak_emailTo, 0 )
end
end
else
dz.log( 'No result_table found', dz.LOG_ERROR )
end
else
dz.log( 'Item or XML - NOT OK', dz.LOG_ERROR )
end
end
end
}
-- That's All --------------------------------------------------