I created a dzVents script that make use of this site and store the information in a textdevice and an alertdevice. The script makes use of dzVents persistent data to store a copy the json result So if the site is not available for a couple of hours it will use the saved data.
Look at the 10 lines Using dzVents with Domoticz for a short intro to dzVents and howto get this script active in domoticz.
Further description and howto config are in the script.
have Fun !
Code: Select all
--[[ getGarbageDates.lua for [ dzVents >= 2.4 ]
this script is only useful in those areas of the Netherlands where the HVC group collects household garbage
Enter your zipcode and housenumber 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 00:05","at 08:00" }, -- daily run twice
httpResponses = { "getGarbage_Response" } -- Trigger the handle Json part
},
-- logging = { level = domoticz.LOG_DEBUG, -- Remove the "-- at the beginning of this and next line for debugging the script
-- marker = "collectGarbage" },
data = { garbage = {initial = {} }, -- Keep a copy of last json just in case
},
execute = function(dz, triggerObject)
--++++--------------------- Mandatory: Set your values and device names below this Line --------------------------------------
local myZipcode = "3318JZ"
local myHousenumber = 85
local myTextDevice = "GarbageText" -- 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
--++++---------------------------- Set your values and device names above this Line --------------------------------------------
-- Compose URL and send
local function collectGarbageDates(secondsFromNow)
local getGarbage_url = "http://inzamelkalender.hvcgroep.nl/push/calendar?postcode=" ..
myZipcode .."&huisnummer=" .. myHousenumber
dz.openURL ({ url = getGarbage_url ,
method = "GET",
callback = "getGarbage_Response" }).afterSec(secondsFromNow)
end
-- Add entry to log and notify to all subsystems
local function errorMessage(message)
dz.log(message,dz.LOG_ERROR)
dz.notify(message)
end
local function string2Epoch(dateString) -- seconds from epoch based on stringdate (used by string2Date)
-- Assuming a date pattern like: yyyy-mm-dd hh:mm:ss
local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)"
local runyear, runmonth, runday, runhour, runminute, runseconds = dateString:match(pattern)
local convertedTimestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})
return convertedTimestamp
end
local function string2Date(str,fmt) -- convert string from json into datevalue
if fmt then return os.date(fmt,string2Epoch(str)) end
return os.date(" %A %d %B, %Y",string2Epoch(str)):gsub(" 0"," ")
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 setGarbageAlertDevice(alertDevice,alertText,alertDate)
local delta = tonumber(string2Date(alertDate,"%d")) - tonumber(os.date("%d")) -- delta in days between today and first garbage collection date
dz.devices(alertDevice).updateAlertSensor(alertLevel(delta),alertText)
return (delta == 0)
end
local function longGarbageName(str) -- Use descriptive strings
str = str:gsub("PMD","Plastic verpakkingen, blik en drinkpakken ")
str = str:gsub("GFT","Groente-, fruit- en tuin afval ")
str = str:gsub("PAPIER","Papier en kartonnen verpakkingen ")
str = str:gsub("REST","Restafval ")
return str
end
local function handleResponse()
if #triggerObject.json > 0 then
dz.data.garbage = triggerObject.json
rt = triggerObject.json
else
errorMessage("Problem with response from hvcgroep (no data) 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. are zipcode and housenumber ok and in HVC group area ?")
return false
end
end
local garbageLines = ""
local typeEarliestDate
local overallEarliestDate = "2999-12-31" -- Hopefully we will have a different garbage collection system by then
local garbageToday = false
for i = 1,#rt do -- walk the resulttable
typeEarliestDate = "2999-12-31"
for j = 1,#rt[i].dateTime do -- walk the dates subtable
if rt[i].dateTime[j].date < typeEarliestDate then -- Keep date closest to today per type
typeEarliestDate = rt[i].dateTime[j].date
if typeEarliestDate < overallEarliestDate then -- date closest to today overall ?
overallEarliestDate = typeEarliestDate -- keep date
overallEarliestType = rt[i].naam -- keep date
end
garbageLines = garbageLines .. string2Date(typeEarliestDate,"%a %e %b: " ) .. longGarbageName(rt[i].naam) .. " " .. "\n"
dz.log(garbageLines,dz.LOG_DEBUG)
typeEarliestDate = rt[i].dateTime[j].date -- Keep date closest to today
end
end
end
if myAlertDevice then -- Update AlertDevice with nearby date / type
garbageToday = setGarbageAlertDevice( myAlertDevice,
longGarbageName(overallEarliestType) .. "\n" ..
string2Date(overallEarliestDate),
overallEarliestDate)
end
if myTextDevice then -- Update defined virtual text device with dates / types
dz.devices(myTextDevice).updateText(garbageLines)
end
if dz.time.matchesRule("at 08:00-17:00") and garbageToday then
dz.notify(longGarbageName(overallEarliestType) .. "will be collected today")
end
end
-- Main
if triggerObject.isHTTPResponse then
if triggerObject.ok then
handleResponse()
else
errorMessage("Problem with response from hvcgroep (not ok)")
collectGarbageDates(600) -- response not OK, try again after 10 minutes
end
else
collectGarbageDates(1)
end
end
}