Domoticz dzVents getGarbageDates script

Garbage Collector domoticz script

This script is for Dutch users, that must place their containers on the street. But maybe it comes in hand as a nice example for others.

I often forget to check the calendar so I was happy that my local garbage collector (HVC) has made an app. An app that sends you a notification the day before. But the app doesn’t send always an app. So I searched for an alternative and found: Afval Nederland.  https://play.google.com/store/apps/details?id=nl.afval. dzVents getGarbageDates script Is now my alternative for the apps.

dzVents getGarbageDates script

It works very nice and has info for lots of collectors. But because of my love for Domoticz I want everything stored locally in my Domoticz overview. I asked the creator of the app for info and Waaren (from the Domoticz forum) made this fantastic DzVentz script for me.

Domoticz would show two devices: a text device that shows the next pickup dates, and an alert device that would inform you:

Domoticz Garbage collector

How to Setup?

It’s really simple:

Start your browser and go to https://apps.hvcgroep.nl/rest/adressen/zipcodehousenumber
replace the red things with your info. for the url you can use the url of your collector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Cyclus NV: https://afvalkalender.cyclusnv.nl
HVC: https://apps.hvcgroep.nl
Dar: https://afvalkalender.dar.nl
Afvalvrij / Circulus-Berkel: https://afvalkalender.circulus-berkel.nl
Meerlanden: https://afvalkalender.meerlanden.nl
Cure: https://afvalkalender.cure-afvalbeheer.nl
Avalex: https://www.avalex.nl
RMN: https://inzamelschema.rmn.nl
Venray: https://afvalkalender.venray.nl
Den Haag: https://huisvuilkalender.denhaag.nl
Berkelland: https://afvalkalender.gemeenteberkelland.nl
Alphen aan den Rijn: https://afvalkalender.alphenaandenrijn.nlrest/adressen/0505200000061116/afvalstromen
Waalre: http://afvalkalender.waalre.nl
ZRD: https://afvalkalender.zrd.nl
Spaarnelanden: https://afvalwijzer.spaarnelanden.nl
Montfoort: https://afvalkalender.montfoort.nl
GAD: https://inzamelkalender.gad.nl
Cranendonck: https://afvalkalender.cranendonck.nl

The output you get looks like this:

1
[{"bagId":"0505200000092263","postcode":"3328LN","huisnummer":35,"huisletter":"","huisnummerToevoeging":"","openbareRuimteNaam":"Moestuin","woonplaatsNaam":"Dordrecht","latitude":51.7701971,"longitude":4.6597255,"woonplaatsId":2351,"gemeenteId":505}]

Write down your bagId number! that is the unique key of your address.
Then start domoticz and go to: settings > events
create a new script: give it a name like getGarbageDates, choose dzVents and choose for: device.

Paste the script below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--[[ 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

Ga naar de volgende url in de browser, waarbij je de juiste url voor jouw afvalverwerker kiest, en postcode en huisnummer wijzigd.
https://apps.hvcgroep.nl/rest/adressen/3328LN-35
de output die je krijgt te zien bevat een bagID wat je later nodig hebt.

Dit bagId wordt gebruikt in de volgende URLs:
Ophaaldagen: https://apps.hvcgroep.nl/rest/adressen/BagID/kalender/2018 <<replace BagID
De ophaaldagen gebruiken ID's om aan te geven welk afvaltype het betreft.
Informatie over deze afvaltypes kan opgehaald worden via:
https://apps.hvcgroep.nl/rest/adressen/BagID/afvalstromen <<replace BagID

]]
--

return {
on = { timer = { "at 00:05","at 08:00" }, -- daily run twice
-- { timer = {'every 1 minutes'},
httpResponses = { "getGarbage_Response" } -- Trigger the handle Json part
},

logging = { level = domoticz.LOG_INFO, -- 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 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 myBagId = "0505200000092263"
local myYear = os.date("%Y")

-- it can happen that other examples then HVC uses other numbers, change them at line
garbageTypes = {6,5,3,2}

--++++---------------------------- Set your values and device names above this Line --------------------------------------------

local function collectGarbageDates(secondsFromNow)
local getGarbage_url = "https://apps.hvcgroep.nl/rest/adressen/" ..myBagId .. "/kalender/" .. myYear
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
local pattern = "(%d+)-(%d+)-(%d+)"
local runyear, runmonth, runday= dateString:match(pattern)
local convertedTimestamp = os.time({year = runyear, month = runmonth, day = runday})
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))
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 = tostring(str)
str = str:gsub("6"," Plastic verpakkingen, blik en drinkpakken ")
str = str:gsub("5"," Groente-, fruit- en tuin afval ")
str = str:gsub("3"," Papier en kartonnen verpakkingen ")
str = str:gsub("2"," 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
local today = os.date("%Y-%m-%d")

for i = 1,#garbageTypes do --walk the the type Table
typeEarliestDate = "2999-12-31"
for j = 1,#rt do -- walk the result Table
-- walk the response table
if rt[j].ophaaldatum >= today and rt[j].ophaaldatum < typeEarliestDate and
rt[j].afvalstroom_id == garbageTypes[i] then -- Keep date closest to today per type
typeEarliestDate = rt[j].ophaaldatum
if typeEarliestDate < overallEarliestDate then -- date closest to today overall ?
overallEarliestDate = typeEarliestDate -- keep date
overallEarliestType = garbageTypes[i] -- keep type
end
garbageLines = garbageLines .. string2Date(typeEarliestDate,"%a %e %b" ) .. longGarbageName(rt[j].afvalstroom_id) .. " " .. "\n"
typeEarliestDate = rt[j].ophaaldatum -- 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
}

Addjust your settings in this script: BagId, and collector url. (I only have tested HVC)

You must create 2 virtual devices:

TextDevice with the name: Garbage
AlertDevice with the name GarbageAlert
You can name them as you like but also have to change the names in the script then.

The script runs daily run twice. But for setting up and testing it’s handy to adjust it. above the script you find:
= { timer = { “at 00:05″,”at 08:00” }, — daily run twice
— { timer = {‘every 1 minutes’},

Delete the 2 minuses —  and place them on the row above. it makes comments from the things after the –. so that your script runs every minute.

Save your script, and watch the logs!

When all is done don’t forget to switch the — again, so it runs twice a day, what would be enough.

 

Thanks Afval Nederland and Waaren!!!

Please if you have another working URL or feedback to improve let us know!