Code: Select all
-- This script updates the "Afvalkalender" and gives notifications when action is needed
PostalCode='<your postal code>'
HouseNumber='<your housenumer>'
Addition='<your addition to housenumber, can be empty>'
AfvalDevice="<name of the textdevice which should containt the garbage calendar"
return {
on = {
timer = {
'at 20:00',
'at 19:00',
'at 18:00', -- Update frequency of the virtual device, set to whatever you like, make sure there is at least one at 20.00 (to get the notification)
},
httpResponses = {
'Rova' -- must match with the callback passed to the openURL command
},
devices = {
'test' -- can be removed, a device name if you want to trigger the script
}
},
logging = {
level = domoticz.LOG_DEBUG, -- adjust to your needs
marker = "Rova"
},
execute = function(domoticz, item)
if (item.isDevice or item.isTimer) then
domoticz.openURL({
url = 'https://www.rova.nl/api/waste-calendar/upcoming?postalcode='..PostalCode..'&houseNumber='..HouseNumber..'&addition='..Addition..'&take=3',
method = 'GET',
callback = 'Rova', -- see httpResponses above.
})
end
if (item.isHTTPResponse) or (item.isShellCommandResponse) then
if (item.ok) then
domoticz.log("Data received: "..item.data,domoticz.LOG_DEBUG)
-- convert dates
local RovaDate=string.sub(item.json[1].date,1,-10)
local RovaDate2=string.sub(item.json[2].date,1,-10)
local RovaDate3=string.sub(item.json[3].date,1,-10)
-- log result
domoticz.log("Next Garbage Collection: ["..item.json[1].garbageType..' at '..RovaDate..']',domoticz.LOG_FORCE)
-- update device
domoticz.devices(AfvalDevice).updateText(item.json[1].garbageType.." "..RovaDate..", "..item.json[2].garbageType.." "..RovaDate2..", "..item.json[3].garbageType.." "..RovaDate3)
-- check if we have to send notification
domoticz.log("minutes="..domoticz.time.minutes..", hours="..domoticz.time.hour,domoticz.LOG_DEBUG)
if (domoticz.time.minutes<5 and domoticz.time.hour==20) then
domoticz.log("20.00: notification timestamp event, checking if we have to notify",domoticz.LOG_DEBUG)
local Time = require('Time')
local t = Time(RovaDate..' 00:00:00')
domoticz.log("Secondsago = "..t.secondsAgo..", minutesAgo="..t.minutesAgo..", hoursago="..t.hoursAgo..", daysago="..t.daysAgo,domoticz.LOG_DEBUG)
-- check if we have to notify
if (t.daysAgo==0 and t.secondsAgo<0) then -- timestamp is within 1 day
-- Yes we do: notify
domoticz.log("Rova will be there within 1 day, sending notification [".."Afvaltype "..item.json[1].garbageType.." wordt opgehaald op "..RovaDate .."]", domoticz.LOG_FORCE)
domoticz.notify("Rova","Afvaltype "..item.json[1].garbageType.." wordt opgehaald op "..RovaDate,domoticz.PRIORITY_NORMAL)
else
-- no we don't
domoticz.log("no need to notify",domoticz.LOG_DEBUG)
end
end
else
domoticz.log("Script error: "..item.statusCode,domoticz.LOG_ERROR)
domoticz.log(item,domoticz.LOG_ERROR)
end
end
end
}