logo

Afvalwijzer (garbage collection) notifications in domoticz

Wed December 27th 2017

I missed several garbage trucks during the holidays due to changes in collection dates .. as always. So I implemented a simple mijnafvalwijzer.nl poller scraping the website for relevant data for my address ;-) The data is processed, and stored in a domoticz text devices and if relevant a notification is send.

see domoticz forum

Note that the script requires zip code and house nr. Add those in the script at “Postcode” and “Huisnummer”

-- afvalWijzer module
-- 
-- curl in os required!!
-- create dummy text device from dummy hardware 'Afval Kalender'
-- add as time based lua script
-- Check the timing when to get a notification below
-- based on script by zicht @ http://www.domoticz.com/forum/viewtopic.php?t=17963
-- but pretty much all of it was rewritten ;-)

-- 2017-12-27 working version
-- 2018-01-02 timeouts and repeats

commandArray = {}

local myAfvalDevice='Afval Kalender'
local Postcode='<enter afvalwijzer supported zipcode here>'
local Huisnummer='<enter huisnr here>'

time = os.date("*t")
-- time of notification
if time.hour==7 and time.min==11 then
-- if time.min %1 == 0 then
    -- print('AfvalWijzer module')

    function os.capture(cmd, rep)  -- execute command to get site
        -- rep is nr of repeats if result is empty
        local r = rep or 1
        local s = ""
        while ( s == "" and r > 0) do
            r = r-1
            local f = assert(io.popen(cmd, 'r'))
            s = assert(f:read('*a'))
            f:close()
        end
        if ( rep - r > 1 ) then
            print("os.capture needed more than 1 call: " .. rep-r)
        end
      return s
    end

    -- get data from afvalWijzer
    local command = "curl --max-time 5 -s 'https://www.mijnafvalwijzer.nl/nl/"..Postcode.."/"..Huisnummer.."/" .. "' | grep -E 'class=\"first(Date|WasteType)'"
    -- addded: grep -E '\<class=\"first(Date|WasteType)'
    -- print('command: ' .. commando)
    local tmp = os.capture(command, 5)
    if ( tmp == "" ) then
        print("afvalWijzer: Empty result from curl command")
    end

    if tmp ~= "" then
        -- strip html stuff and format for domoticz
        tmp=tmp:gsub('%c','')
        tmp=tmp:gsub('.-<p class="firstDate">(.-)</p>.-<p class="firstWasteType">(.-)</p>','%1: %2\n')

        print ('afvalWijzer result: ' .. tmp)
        if otherdevices[myAfvalDevice] ~= tmp then
            commandArray['UpdateDevice'] = otherdevices_idx[myAfvalDevice] .. '|0|' .. tmp
            if tmp:match('vandaag') then
                print("afvalWijzer: notification sent")
                commandArray['SendNotification']='Afvalwijzer#'..tmp
            end
        end
    end -- empty result
end

return commandArray