It is working, thanks a lot waaren!
For all others who want to use RMN.nl as the data source (for certain Dutch municipalities). This is the script that works. Simply fill-in the bag ID field with the number you can find on:
https://inzamelschema.rmn.nl/rest/adressen/0000aa-99 (0000aa = zip code - 00 = house number)
Code: Select all
--[[ getGarbageDates.lua for [ dzVents >= 2.4.28 ]
--
Enter your bagid in the appropriate place between the lines starting with --++++
Next is to set your virtual text and or virtual alert device.
the text device will contain the most nearby collectdates for the four types of household garbage
the alert device will contain the date and type for the garbagecollecion that will arrive first
]]--
return
{
on =
{
timer =
{
'at 20:00',
'at 07:00'
},
devices =
{
-- 'getGarbage', -- Only for test purposes can be ignored
},
httpResponses =
{
'getGarbage_Response' -- Trigger to handle Json part
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_DEBUG when something does not work as expected
marker = 'collectGarbage',
},
data =
{
garbage =
{
initial = {}, -- Keep a copy of last json just in case
},
},
execute = function(dz, item)
--++++--------------------- Mandatory: Set your values and device names below this Line --------------------------------------
local myBagId = "XXXXXXXX" -- Find bagid at https://inzamelschema.rmn.nl/rest/adressen/0000aa-99 (zip code - house number)
local myYear = os.date("%Y")
local myTextDevice = "Garbage" -- Name with quotes or idx without when created as virtual text device
local myAlertDevice = "GarbageAlert" -- Name with quotes or idx without when created as virtual alert device
local myNotificationTable =
{
-- table with one or more notification systems.
-- uncomment the notification systems that you want to be used
-- Can be one or more of
-- dz.NSS_FIREBASE_CLOUD_MESSAGING,
-- dz.NSS_PUSHOVER,
-- dz.NSS_HTTP,
-- dz.NSS_KODI,
-- dz.NSS_LOGITECH_MEDIASERVER,
-- dz.NSS_NMA,
-- dz.NSS_PROWL,
-- dz.NSS_PUSHALOT,
-- dz.NSS_PUSHBULLET,
-- dz.NSS_PUSHOVER,
-- dz.NSS_PUSHSAFER,
dz.NSS_TELEGRAM,
}
local longGarbageName =
{
[100] = "Plastic",
[3] = "GFT afval (bruin)",
[87] = "Oud papier en kartonnen (blauw)",
[1] = "Restafval (grijs)"
}
--++++---------------------------- No changes required below this line --------------------------------------------
local function collectGarbageDates(secondsFromNow)
local getGarbage_url = "https://inzamelschema.rmn.nl/rest/adressen/" ..myBagId .. "/kalender/" .. myYear
dz.openURL ({ url = getGarbage_url ,
callback = "getGarbage_Response" }).afterSec(secondsFromNow)
end
-- Add entry to log and notify to set subsystems
local function errorMessage(message)
dz.log(message,dz.LOG_ERROR)
dz.notify('Garbage',message, dz.PRIORITY_HIGH, dz.SOUND_DEFAULT, "" , myNotificationTable)
end
local function convertDateFormat(dateString, fromPattern, toFormat)
local fromPattern = fromPattern or '(%d+)-(%d+)-(%d+)'
local toFormat = toFormat or '%a %d %b'
local runyear, runmonth, runday= dateString:match(fromPattern)
return os.date(toFormat, os.time({year = runyear, month = runmonth, day = runday}) )
end
local function text(lines)
if dz.utils.deviceExists(myTextDevice) then
dz.devices(myTextDevice).updateText( table.concat(lines, '\n') )
end
end
local function alertLevel(delta)
if delta < 2 then return dz.ALERTLEVEL_RED end
if delta < 3 then return dz.ALERTLEVEL_YELLOW end
if delta < 4 then return dz.ALERTLEVEL_ORANGE end
return dz.ALERTLEVEL_GREEN
end
local function alert(lines)
if dz.utils.deviceExists(myAlertDevice) then
dz.devices(myAlertDevice).updateAlertSensor(alertLevel(lines.delta), lines[1])
end
if dz.time.matchesRule('at 05:00-09:00') and lines.delta == 0 then
dz.notify('Huisafval',lines[1]:match('%: (.*)') .. ' will be collected today', dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , myNotificationTable)
elseif dz.time.matchesRule('at 17:00-22:00') and lines.delta == 1 then
dz.notify('Huisafval',lines[1]:match('%: (.*)') .. ' will be collected tomorrow', dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , myNotificationTable)
end
end
local function selectRecords(t)
dateRecords = {}
for _, garbage in ipairs(t) do
if garbage.ophaaldatum >= dz.time.rawDate then
if dateRecords[garbage.ophaaldatum] then
table.insert(dateRecords[garbage.ophaaldatum], longGarbageName[garbage.afvalstroom_id] )
else
dateRecords[garbage.ophaaldatum] = { longGarbageName[garbage.afvalstroom_id] }
end
dz.log(garbage.ophaaldatum .. ' = ' .. longGarbageName[garbage.afvalstroom_id], dz.LOG_DEBUG)
end
end
return dateRecords
end
local function makeLines(t)
local startDate = dz.time
local futureDay = 0
local lineCounter = 1
local textLines = {}
while lineCounter < 5 and futureDay < 60 do
local traverseDate = startDate.addDays(futureDay).rawDate
if t[traverseDate] then
local textLine = convertDateFormat(traverseDate) .. ': ' .. table.concat(t[traverseDate], ' plus ')
if lineCounter == 1 then textLines.delta = futureDay end
textLines[lineCounter] = textLine
lineCounter = lineCounter + 1
end
futureDay = futureDay + 1
end
return textLines
end
local function handleResponse()
if not(item.ok and item.json) then
errorMessage('Problem with response (no data). Try using data from earlier run')
rt = dz.data.garbage -- json empty. Get last valid from dz.data
if #rt < 1 then -- No valid data in dz.data either
errorMessage('No previous data. is bagnumber ok?')
return false
end
else
rt = selectRecords(item.json)
textLines = makeLines(rt)
alert(textLines)
text(textLines)
end
end
-- Main
if item.isHTTPResponse then
if item.ok then
handleResponse()
else
errorMessage('Problem with response (not ok)')
collectGarbageDates(600) -- response not OK, try again after 10 minutes
end
else
collectGarbageDates(1)
end
end
}