dzVents get garbage collection dates (various)  [SOLVED]

Moderator: leecollings

riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents get garbage collection dates (various)

Post by riko »

I was very content with the script, till 2021 :) Something goes wrong, but I do not understand what goes wrong in line 159.

The log shows the following error:

Code: Select all

2021-01-06 17:19:00.307 Status: dzVents: Info: collectGarbage: ------ Start external script: get_garbage_dates_v2.lua:, trigger: "at 17:19"
2021-01-06 17:19:00.308 Status: dzVents: Debug: collectGarbage: Processing device-adapter for Disable get_garbage_dates: Switch device adapter
2021-01-06 17:19:00.308 Status: dzVents: Debug: collectGarbage: OpenURL: url = https://inzamelschema.rmn.nl/rest/adressen/0312200000006752/kalender/2021
2021-01-06 17:19:00.308 Status: dzVents: Debug: collectGarbage: OpenURL: method = GET
2021-01-06 17:19:00.308 Status: dzVents: Debug: collectGarbage: OpenURL: post data = nil
2021-01-06 17:19:00.308 Status: dzVents: Debug: collectGarbage: OpenURL: headers = nil
2021-01-06 17:19:00.308 Status: dzVents: Debug: collectGarbage: OpenURL: callback = getGarbage_Response
2021-01-06 17:19:00.309 Status: dzVents: Info: collectGarbage: ------ Finished get_garbage_dates_v2.lua
2021-01-06 17:19:00.313 Status: dzVents: Debug: - OpenURL = {["URL"]="https://inzamelschema.rmn.nl/rest/adressen/0312200000006752/kalender/2021", ["method"]="GET", ["_trigger"]="getGarbage_Response", ["_after"]=1}
2021-01-06 17:19:01.882 Status: dzVents: Debug: - HTTPResponse: getGarbage_Response
2021-01-06 17:19:01.959 Status: dzVents: Info: Handling httpResponse-events for: "getGarbage_Response"
2021-01-06 17:19:01.959 Status: dzVents: Info: collectGarbage: ------ Start external script: get_garbage_dates_v2.lua: HTTPResponse: "getGarbage_Response"
2021-01-06 17:19:01.973 Status: dzVents: Debug: collectGarbage: Processing device-adapter for Disable get_garbage_dates: Switch device adapter
2021-01-06 17:19:01.973 Status: dzVents: Debug: collectGarbage: 2021-01-08 = Restafval (grijs)
2021-01-06 17:19:01.973 Status: dzVents: Debug: collectGarbage: 2021-01-09 = Oud papier (blauw)
2021-01-06 17:19:01.973 Status: dzVents: Info: collectGarbage: ------ Finished get_garbage_dates_v2.lua
2021-01-06 17:19:01.973 Error: dzVents: Error: (3.0.2) collectGarbage: An error occurred when calling event handler get_garbage_dates_v2
2021-01-06 17:19:01.973 Error: dzVents: Error: (3.0.2) collectGarbage: ...omoticz/scripts/dzVents/scripts/get_garbage_dates_v2.lua:159: attempt to concatenate a nil value (field '?')
I'm using this script, based on waarens work, with some minor modifications (line 159 contains:

dz.log(garbage.ophaaldatum .. ' = ' .. longGarbageName[garbage.afvalstroom_id], dz.LOG_DEBUG)

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

]]--

local eveningRun = 'at 20:00'
local morningRun = 'at 17:19'

package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/Telegram.lua'
local telegram = require('Telegram')

return
{
	    active = true,
        on =
        {
            timer =
            {
                morningRun,
                eveningRun,
            },

            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)

		-- uitschakelen van script
		local manualOverrideSwitch = 150
		
        if (manualOverrideSwitch and dz.devices(manualOverrideSwitch).state == 'On') then
            dz.log('Get garbage dates script is handmatig uitgeschakeld (disable script switches)', dz.LOG_DEBUG)
            return
        end	



        --++++--------------------- Mandatory: Set your values and device names below this Line --------------------------------------
        local myBagId = "0312200000006752"
        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 (bruin)",
            [87] = "Oud papier (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)
			telegram.sendText(telegram.getId('zeisterweg'), message)
            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
				telegram.sendText(telegram.getId('zeisterweg'), lines[1]:match('%: (.*)')  .. ' wordt vandaag opgehaald')
                --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
				telegram.sendText(telegram.getId('zeisterweg'), lines[1]:match('%: (.*)')  .. ' wordt morgen opgehaald')
                --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 = {}

            if dz.time.matchesRule(eveningRun) then futureDay = 1 end

            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
}
It looks like the garbage date GET works fine, the link gives the following results:

Code: Select all

[{"afvalstroom_id":3,"ophaaldatum":"2021-01-02"},{"afvalstroom_id":1,"ophaaldatum":"2021-01-08"},{"afvalstroom_id":87,"ophaaldatum":"2021-01-09"},{"afvalstroom_id":37,"ophaaldatum":"2021-01-11"},{"afvalstroom_id":100,"ophaaldatum":"2021-01-14"},{"afvalstroom_id":3,"ophaaldatum":"2021-01-15"},{"afvalstroom_id":1,"ophaaldatum":"2021-01-22"},{"afvalstroom_id":100,"ophaaldatum":"2021-01-28"},{"afvalstroom_id":3,"ophaaldatum":"2021-01-29"},{"afvalstroom_id":1,"ophaaldatum":"2021-02-05"},{"afvalstroom_id":87,"ophaaldatum":"2021-02-06"},{"afvalstroom_id":100,"ophaaldatum":"2021-02-11"},{"afvalstroom_id":3,"ophaaldatum":"2021-02-12"},{"afvalstroom_id":1,"ophaaldatum":"2021-02-19"},{"afvalstroom_id":100,"ophaaldatum":"2021-02-25"},{"afvalstroom_id":3,"ophaaldatum":"2021-02-26"},{"afvalstroom_id":1,"ophaaldatum":"2021-03-05"},{"afvalstroom_id":87,"ophaaldatum":"2021-03-06"},{"afvalstroom_id":100,"ophaaldatum":"2021-03-11"},{"afvalstroom_id":3,"ophaaldatum":"2021-03-12"},{"afvalstroom_id":1,"ophaaldatum":"2021-03-19"},{"afvalstroom_id":100,"ophaaldatum":"2021-03-25"},{"afvalstroom_id":3,"ophaaldatum":"2021-03-26"},{"afvalstroom_id":1,"ophaaldatum":"2021-04-02"},{"afvalstroom_id":87,"ophaaldatum":"2021-04-03"},{"afvalstroom_id":100,"ophaaldatum":"2021-04-08"},{"afvalstroom_id":3,"ophaaldatum":"2021-04-09"},{"afvalstroom_id":1,"ophaaldatum":"2021-04-16"},{"afvalstroom_id":100,"ophaaldatum":"2021-04-22"},{"afvalstroom_id":3,"ophaaldatum":"2021-04-23"},{"afvalstroom_id":1,"ophaaldatum":"2021-04-30"},{"afvalstroom_id":87,"ophaaldatum":"2021-05-01"},{"afvalstroom_id":100,"ophaaldatum":"2021-05-06"},{"afvalstroom_id":3,"ophaaldatum":"2021-05-07"},{"afvalstroom_id":1,"ophaaldatum":"2021-05-14"},{"afvalstroom_id":100,"ophaaldatum":"2021-05-20"},{"afvalstroom_id":3,"ophaaldatum":"2021-05-21"},{"afvalstroom_id":1,"ophaaldatum":"2021-05-28"},{"afvalstroom_id":100,"ophaaldatum":"2021-06-03"},{"afvalstroom_id":3,"ophaaldatum":"2021-06-04"},{"afvalstroom_id":87,"ophaaldatum":"2021-06-05"},{"afvalstroom_id":1,"ophaaldatum":"2021-06-11"},{"afvalstroom_id":100,"ophaaldatum":"2021-06-17"},{"afvalstroom_id":3,"ophaaldatum":"2021-06-18"},{"afvalstroom_id":1,"ophaaldatum":"2021-06-25"},{"afvalstroom_id":100,"ophaaldatum":"2021-07-01"},{"afvalstroom_id":3,"ophaaldatum":"2021-07-02"},{"afvalstroom_id":87,"ophaaldatum":"2021-07-03"},{"afvalstroom_id":1,"ophaaldatum":"2021-07-09"},{"afvalstroom_id":100,"ophaaldatum":"2021-07-15"},{"afvalstroom_id":3,"ophaaldatum":"2021-07-16"},{"afvalstroom_id":1,"ophaaldatum":"2021-07-23"},{"afvalstroom_id":100,"ophaaldatum":"2021-07-29"},{"afvalstroom_id":3,"ophaaldatum":"2021-07-30"},{"afvalstroom_id":1,"ophaaldatum":"2021-08-06"},{"afvalstroom_id":87,"ophaaldatum":"2021-08-07"},{"afvalstroom_id":100,"ophaaldatum":"2021-08-12"},{"afvalstroom_id":3,"ophaaldatum":"2021-08-13"},{"afvalstroom_id":1,"ophaaldatum":"2021-08-20"},{"afvalstroom_id":100,"ophaaldatum":"2021-08-26"},{"afvalstroom_id":3,"ophaaldatum":"2021-08-27"},{"afvalstroom_id":1,"ophaaldatum":"2021-09-03"},{"afvalstroom_id":87,"ophaaldatum":"2021-09-04"},{"afvalstroom_id":100,"ophaaldatum":"2021-09-09"},{"afvalstroom_id":3,"ophaaldatum":"2021-09-10"},{"afvalstroom_id":1,"ophaaldatum":"2021-09-17"},{"afvalstroom_id":100,"ophaaldatum":"2021-09-23"},{"afvalstroom_id":3,"ophaaldatum":"2021-09-24"},{"afvalstroom_id":1,"ophaaldatum":"2021-10-01"},{"afvalstroom_id":87,"ophaaldatum":"2021-10-02"},{"afvalstroom_id":100,"ophaaldatum":"2021-10-07"},{"afvalstroom_id":3,"ophaaldatum":"2021-10-08"},{"afvalstroom_id":1,"ophaaldatum":"2021-10-15"},{"afvalstroom_id":100,"ophaaldatum":"2021-10-21"},{"afvalstroom_id":3,"ophaaldatum":"2021-10-22"},{"afvalstroom_id":1,"ophaaldatum":"2021-10-29"},{"afvalstroom_id":100,"ophaaldatum":"2021-11-04"},{"afvalstroom_id":3,"ophaaldatum":"2021-11-05"},{"afvalstroom_id":87,"ophaaldatum":"2021-11-06"},{"afvalstroom_id":1,"ophaaldatum":"2021-11-12"},{"afvalstroom_id":100,"ophaaldatum":"2021-11-18"},{"afvalstroom_id":3,"ophaaldatum":"2021-11-19"},{"afvalstroom_id":1,"ophaaldatum":"2021-11-26"},{"afvalstroom_id":100,"ophaaldatum":"2021-12-02"},{"afvalstroom_id":3,"ophaaldatum":"2021-12-03"},{"afvalstroom_id":87,"ophaaldatum":"2021-12-04"},{"afvalstroom_id":1,"ophaaldatum":"2021-12-10"},{"afvalstroom_id":100,"ophaaldatum":"2021-12-16"},{"afvalstroom_id":3,"ophaaldatum":"2021-12-17"},{"afvalstroom_id":1,"ophaaldatum":"2021-12-24"},{"afvalstroom_id":100,"ophaaldatum":"2021-12-30"},{"afvalstroom_id":3,"ophaaldatum":"2021-12-31"}]
Anyone experiencing the same problem since 2021? And anyone with a solution?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents get garbage collection dates (various)

Post by waaren »

riko wrote: Wednesday 06 January 2021 17:28 I was very content with the script, till 2021 :) Something goes wrong, but I do not understand what goes wrong in line 159.
And anyone with a solution?
Can you change line 159 from

Code: Select all

dz.log(garbage.ophaaldatum .. ' = ' .. longGarbageName[garbage.afvalstroom_id], dz.LOG_DEBUG)
to

Code: Select all

dz.log(garbage.ophaaldatum .. ' = ' .. tostring(longGarbageName[garbage.afvalstroom_id]), dz.LOG_DEBUG)
and try again?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents get garbage collection dates (various)

Post by riko »

Wow, perfect!
I was even able to determine the Christmas tree category for Monday (afvalstroom id 37) :D
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: dzVents get garbage collection dates (various)

Post by imdos »

I've found the new RMN app and did some certificate unpinning with frida and http toolkit. The new URL is for an adress in IJsselstein for example:
3402ta 17

https://europe-west3-burgerportaal-prod ... 6/calendar

Please let me know if you require some others which reside within the same region to check if the scripts can be rewritten.

P.S. I'm fluent in Dutch; but don't know which language is preferred.
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests