Sharing : Afval kalendar in text sensor

Moderator: leecollings

DickN
Posts: 8
Joined: Monday 06 March 2017 19:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: NL
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by DickN »

pwhooftman wrote: Thursday 15 June 2017 17:34 Nice :)

I grab part of that website to show on my frontpage:

Image
Hi,

Can you tell me how to build this page?
DickN
Posts: 8
Joined: Monday 06 March 2017 19:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: NL
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by DickN »

dutchdevil83 wrote: Wednesday 05 July 2017 20:45 For sending a notification one day before the actual pickup day (for example paper day is wednesday but waste day is friday here) i use this code for the notication:

Code: Select all

  --Notification
   if tonumber(os.date("%d")) + 1 ==  tonumber(string.match(tmp, "0*(%d+)")) then
   commandArray['SendNotification']='Afvalwijzer#'..tmp
   end 
How do you setup this notification?
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by JuanUil »

nice
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
mikeoo
Posts: 110
Joined: Sunday 22 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by mikeoo »

DickN wrote: Sunday 15 July 2018 16:34
dutchdevil83 wrote: Wednesday 05 July 2017 20:45 For sending a notification one day before the actual pickup day (for example paper day is wednesday but waste day is friday here) i use this code for the notication:

Code: Select all

  --Notification
   if tonumber(os.date("%d")) + 1 ==  tonumber(string.match(tmp, "0*(%d+)")) then
   commandArray['SendNotification']='Afvalwijzer#'..tmp
   end 
How do you setup this notification?
In Domoticz Setup --> Settings --> Notification.
Every notification system you use there will be used to send the info from Afvalwijzer.
DickN
Posts: 8
Joined: Monday 06 March 2017 19:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: NL
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by DickN »

Thanks I have a look.
rron
Posts: 223
Joined: Sunday 30 March 2014 15:24
Target OS: Linux
Domoticz version: Beta
Location: Waterland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by rron »

Hello,
Can someone look at my script please? I get some errors whem I'm trying to get my script working.
This is my locations where I want to retrieve the information: https://datalab.purmerend.nl/afvalkalen ... ind=&date=
This is how I made the script

Code: Select all

-- afvalWijzer module
--
-- curl in os required!!
-- create dummy text device from dummy hardware with the name defined for: myAfvalDevice
-- Check the timing when to get a notification for each Afvaltype in the afvaltype_cfg table
-- based on script by zicht @ http://www.domoticz.com/forum/viewtopic.php?t=17963
-- based on script by nf999 @ http://www.domoticz.com/forum/viewtopic.php?f=61&t=17963&p=174908#p169637

local myAfvalDevice='Container'
local area='Weidevenne'
local NotificationEmailAdress = "[email protected]"
-- Define the Notification Title and body text. there are 3 variables you can include:
-- @DAG@ = Will be replaced by (vandaag/morgen/over x dagen)
-- @AFVALTYPE@ = Will be replaced by the AfvalType found on the internet
-- @AFVALTEXT@ = Will be replaced by the content of the text field for the specific AfvalType
-- @AFVALDATE@ = Will be replaced by the pickup date found on the internet
local notificationtitle = '@DAG@ de @AFVALTEXT@ aan de weg zetten!'
local notificationtext  = '@DAG@ wordt de "@AFVALTEXT@" opgehaald!'

-- Switch on Debugging in case of issues => set to true/false=======
local debug = false  -- get debug info in domoticz console/log

-- define a line for each afvaltype_cfg retuned by the webrequest:
-- hour & min ==> the time the check needs to be performed and notification send when daysbefore is true
-- daysbefore ==> 0 means that the notification is send the day of the planned garbage collection
-- daysbefore ==> X means that the notification is send X day(s) before the day of the planned garbage collection
-- text       ==> define the text for the notification.
local afvaltype_cfg = {
   ["Rest"]                          ={hour=19,min=22,daysbefore=1,text="Grijze Container met Restafval"},
   ["GFT"]        ={hour=19,min=22,daysbefore=1,text="Groene Container met Tuinfval"},
   ["Papier"]                   ={hour=12,min=00,daysbefore=0,text="Blauwe Container met Oud papier"}}
--==== end of config ======================================================================================================
-- General conversion tables
local MON_e_n={January="januari", February="februari", March="maart", April="april", May="mei", June="juni", July="juli", August="augustus", September="september", October="oktober", November="november", December="december"}
local WDAY_e_n={Sunday="zondag", Monday="maandag", Tuesday="dinsdag", Wednesday="woensdag", Thursday="donderdag", Friday="vrijdag", Saturday="zaterdag"}
local MON={januari=1,februari=2,maart=3,april=4,mei=5,juni=6,juli=7,augustus=8,september=9,oktober=10,november=11,december=12}

-- round
function Round(num, idp)
   return tonumber(string.format("%." ..(idp or 0).. "f", num))
end
-- debug print
function dprint(text)
   if debug then print("@AFW:"..text) end
end
-- run curl and capture output
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 days between today and provided date
function getdaysdiff(i_afvaltype_date)
   local curTime = os.time{day=timenow.day,month=timenow.month,year=timenow.year}
   -- Calculate the daysdifference between found date and Now and send notification is required
   local afvalyear =timenow.year
   local afvalday  =timenow.day
   local afvalmonth=timenow.month
   local s_afvalmonth="vandaag"
   -- check if date in variable i_afvaltype_date contains "vandaag" in stead of a valid date -> use today's date
   if i_afvaltype_date == "vandaag" then
      -- use the set todays info
   else
      afvalday,s_afvalmonth=i_afvaltype_date:match("%a+ (%d+) (%a+)")
      if (afvalday == nil or s_afvalmonth == nil) then
         print ('! afvalWijzer: No valid date found in i_afvaltype_date: ' .. i_afvaltype_date)
         return
      end
      afvalmonth = MON[s_afvalmonth]
   end
   dprint("...gerd-> afvalyear:"..tostring(afvalyear).."  s_afvalmonth:"..tostring(s_afvalmonth).."  afvalmonth:"..tostring(afvalmonth).."  afvalday:"..tostring(afvalday))
   --
   local afvalTime = os.time{day=afvalday,month=afvalmonth,year=afvalyear}
   -- return number of days diff
   return Round(os.difftime(afvalTime, curTime)/86400,0)   -- 1 day = 86400 seconds
end

function notification(s_afvaltype,s_afvaltype_date,i_daysdifference)
   --
   dprint("...Noti-> i_daysdifference:"..tostring(i_daysdifference).."  afvaltype_cfg[s_afvaltype].daysbefore:"..tostring(afvaltype_cfg[s_afvaltype].daysbefore).."  hour:"..tostring(afvaltype_cfg[s_afvaltype].hour).."  min:"..tostring(afvaltype_cfg[s_afvaltype].min))
   if afvaltype_cfg[s_afvaltype] ~= nil
   and timenow.hour==afvaltype_cfg[s_afvaltype].hour
   and timenow.min==afvaltype_cfg[s_afvaltype].min
   and i_daysdifference == afvaltype_cfg[s_afvaltype].daysbefore then
      print ('afvalWijzer Notification send for ' .. s_afvaltype)
      dag = ""
      if afvaltype_cfg[s_afvaltype].daysbefore == 0 then
         dag = "vandaag"
      elseif afvaltype_cfg[s_afvaltype].daysbefore == 1 then
         dag = "morgen"
      else
         dag = "over " .. tostring(afvaltype_cfg[s_afvaltype].daysbefore) .. " dagen"
      end
      notificationtitle = notificationtitle:gsub('@DAG@',dag)
      notificationtitle = notificationtitle:gsub('@AFVALTYPE@',s_afvaltype)
      notificationtitle = notificationtitle:gsub('@AFVALTEXT@',tostring(afvaltype_cfg[s_afvaltype].text))
      notificationtitle = notificationtitle:gsub('@AFVALDATE@',s_afvaltype_date)
      notificationtext = notificationtext:gsub('@DAG@',dag)
      notificationtext = notificationtext:gsub('@AFVALTYPE@',s_afvaltype)
      notificationtext = notificationtext:gsub('@AFVALTEXT@',tostring(afvaltype_cfg[s_afvaltype].text))
      notificationtext = notificationtext:gsub('@AFVALDATE@',s_afvaltype_date)
      commandArray['SendEmail'] = notificationtitle .. '#' .. notificationtext .. '#' .. NotificationEmailAdress
   end
end

-- Do the actual update retrieving data from the website and processing it
function Perform_Update()
   print('afvalWijzer module start check')
   dprint('=== web update ================================')
   -- get data from afvalWijzer
   local commando = "curl --max-time 5 -s 'https://datalab.purmerend.nl/afvalkalender/?area=Weidevenne'"
   local tmp = os.capture(commando, 5)
   if ( tmp == "" ) then
      print("afvalWijzer: Empty result from curl command")
      return
   end
   -- strip html stuff and format for domoticz
   tmp=tmp:gsub('%c','')
   --~ <p class="firstDate">donderdag 22 maart</p>
   --~ <p class="firstDate">vandaag</p>
   --~ <p class="firstWasteType">Groente, Fruit en Tuinafval</p>
   -- get the data for these fields
   web_afvaldate,web_afvaltype=tmp:match('.-<p class="firstDate">(.-)</p>.-<p class="firstWasteType">(.-)</p>')
   dprint("web_afvaltype:"..tostring(web_afvaltype).."   web_afvaldate:"..tostring (web_afvaldate))
   if (web_afvaldate == nil or web_afvaltype == nil) then
      print ('! afvalWijzer: No valid data found in returned webdata.  skipping the rest of the logic.')
      return
   end
   -- set the date back to a real date to allow for future processing
   if web_afvaldate == "vandaag" then
      if WDAY_e_n[os.date("%A")] == nil then
         dprint(" Error: Not in table WDAY_e_n[]:"..os.date("%A"))
      end
      if MON_e_n[os.date("%B")] == nil then
         dprint(" Error: Not in table MON_e_n[]:"..os.date("%A"))
      end
      web_afvaldate = WDAY_e_n[os.date("%A")].." "..os.date("%d").." "..MON_e_n[os.date("%B")]
      dprint('Change web_afvaldate "vandaag" to :' .. web_afvaldate)
   end
   -- process new information from the web
   daysdifference = getdaysdiff(web_afvaldate)
   if (afvaltype_cfg[web_afvaltype] == nil) then
      print ('! afvalWijzer: Afvalsoort not defined in the "afvaltype_cfg" table for found Afvalsoort : ' .. web_afvaltype)
   end
   notification(web_afvaltype,web_afvaldate,daysdifference)  -- check notification for new found info

   dprint('=== device: ' .. myAfvalDevice .. ' check and update ========')
   -- update device when text changed
   local txt = ""
   curdevtext = otherdevices[myAfvalDevice]
   -- process each record in the device text first to check if still in future and notification needed
   for dev_date, dev_afvaltype in string.gmatch(curdevtext..'\r\n', '(.-)=(.-)\r\n+') do
      dprint("=> process:"..dev_date.."="..dev_afvaltype)
      if web_afvaltype == dev_afvaltype then
         dprint(".> skip same as Web  -> dev_afvaltype:"..dev_date.."="..dev_afvaltype)
      else
         -- Get DaysDiff
         daysdiffdev = getdaysdiff(dev_date)
         if daysdiffdev < 0  then
            dprint(".> skip old -> dev_afvaltype:"..dev_date.."="..dev_afvaltype.."   daysdiffdev:"..daysdiffdev)
         else
            dprint(".> Add back to TxtDev -> afvaltype:"..dev_date.."="..dev_afvaltype.."   daysdiffdev:"..daysdiffdev)
            notification(dev_afvaltype,dev_date,daysdiffdev)  -- check notification for new found info
            txt = txt..dev_date .. "=" .. dev_afvaltype .. "\r\n"
         end
      end
   end
   dprint('=== Update TxtDevice in Domoticz =============')
   dprint("=> Add Webinfo to TxtDev -> afvaltype:"..web_afvaltype.." - "..web_afvaldate)
   txt = txt..web_afvaldate .. "=" .. web_afvaltype
   -- always update the domoticz device so one can see it is updating and when it was ran last.
   if (curdevtext ~= txt) then
      commandArray['UpdateDevice'] = otherdevices_idx[myAfvalDevice] .. '|0|' .. txt
      print ('afvalWijzer: Update device from: \n'.. otherdevices[myAfvalDevice] .. '\n replace with:\n' .. txt)
   else
      print ('afvalWijzer: No updated text for TxtDevice.')
   end
end

-- End Functions =========================================================================

-- Start of logic ========================================================================
commandArray = {}
timenow = os.date("*t")

-- check for notification times and run update only when we are at one of these defined times
local needupdate = false
for avtype,get in pairs(afvaltype_cfg) do
   dprint("afvaltype_cfg :"..tostring(avtype)..";"..tostring(afvaltype_cfg[avtype].hour)..";"..tostring(afvaltype_cfg[avtype].min))
   if timenow.hour==afvaltype_cfg[avtype].hour
   and timenow.min==afvaltype_cfg[avtype].min then
      needupdate = true
   end
end
-- get information from website, update device and send notification when required
if needupdate then
   debug = true     -- activate debug here to only log the update process in detail
   Perform_Update()
else
   dprint("Scheduled time(s) not reached yet, so nothing to do!")
end

return commandArray
This is what I get when I try to run the script:

pi@raspberrypi:~/domoticz/scripts/lua $ ./script_time_afvalwijzer.lua
./script_time_afvalwijzer.lua: line 1: --: command not found
./script_time_afvalwijzer.lua: line 2: --: command not found
./script_time_afvalwijzer.lua: line 3: --: command not found
./script_time_afvalwijzer.lua: line 4: --: command not found
./script_time_afvalwijzer.lua: line 5: --: command not found
./script_time_afvalwijzer.lua: line 6: --: command not found
./script_time_afvalwijzer.lua: line 9: local: can only be used in a function
./script_time_afvalwijzer.lua: line 7: --: command not found
./script_time_afvalwijzer.lua: line 10: local: can only be used in a function
./script_time_afvalwijzer.lua: line 11: local: can only be used in a function
./script_time_afvalwijzer.lua: line 12: --: command not found
./script_time_afvalwijzer.lua: line 13: syntax error near unexpected token `('
./script_time_afvalwijzer.lua: line 13: `-- @DAG@ = Will be replaced by (vandaag/morgen/over x dagen)'
pi@raspberrypi:~/domoticz/scripts/lua $
Rpi, RFXcom, klikaan klikuit switch,thermosmart, z-wave, espeasy, smartevse
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by jvdz »

The command to run the lua script is:

Code: Select all

lua script_time_afvalwijzer.lua
Try that and see what it says.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
rron
Posts: 223
Joined: Sunday 30 March 2014 15:24
Target OS: Linux
Domoticz version: Beta
Location: Waterland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by rron »

jvdz wrote: Monday 13 August 2018 12:14 The command to run the lua script is:

Code: Select all

lua script_time_afvalwijzer.lua
Try that and see what it says.

Jos
Hello Jos,
My mistake. :ugeek:
Looks ok now but only the wrong garbage bin. I have to look into that.
Thanks
Rpi, RFXcom, klikaan klikuit switch,thermosmart, z-wave, espeasy, smartevse
rron
Posts: 223
Joined: Sunday 30 March 2014 15:24
Target OS: Linux
Domoticz version: Beta
Location: Waterland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by rron »

rron wrote: Monday 13 August 2018 21:57
jvdz wrote: Monday 13 August 2018 12:14 The command to run the lua script is:

Code: Select all

lua script_time_afvalwijzer.lua
Try that and see what it says.

Jos
Hello Jos,
My mistake. :ugeek:
Looks ok now but only the wrong garbage bin. I have to look into that.
Thanks
I was wrong there's no fault but also no reply. When i'm using https://datalab.purmerend.nl/afvalkalen ... ind=&date= in curl I do get a reply.
Is there a way how I can test This?
Rpi, RFXcom, klikaan klikuit switch,thermosmart, z-wave, espeasy, smartevse
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by jvdz »

Hoi Ron,
The script needs adapting for that website as it returns a totally different output!
I will have a look to see whether I can adapt a version I have made.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
rron
Posts: 223
Joined: Sunday 30 March 2014 15:24
Target OS: Linux
Domoticz version: Beta
Location: Waterland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by rron »

jvdz wrote: Tuesday 14 August 2018 15:32 Hoi Ron,
The script needs adapting for that website as it returns a totally different output!
I will have a look to see whether I can adapt a version I have made.

Jos
Thanks for that I appreciate that :D
Rpi, RFXcom, klikaan klikuit switch,thermosmart, z-wave, espeasy, smartevse
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by jvdz »

You have a PM.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by remb0 »

User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by jvdz »

... but that doesn't support the Deventer link rron needs. ;)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
rron
Posts: 223
Joined: Sunday 30 March 2014 15:24
Target OS: Linux
Domoticz version: Beta
Location: Waterland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by rron »

I have sent you a mail
Rpi, RFXcom, klikaan klikuit switch,thermosmart, z-wave, espeasy, smartevse
rron
Posts: 223
Joined: Sunday 30 March 2014 15:24
Target OS: Linux
Domoticz version: Beta
Location: Waterland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by rron »

Everything is working now with the help of JvdZ, thanks for that. :D
The script can be found on : https://github.com/jvanderzande/mijnafv ... merend.lua
Ronald
Rpi, RFXcom, klikaan klikuit switch,thermosmart, z-wave, espeasy, smartevse
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by Derik »

pwhooftman wrote: Thursday 15 June 2017 17:34 Nice :)

I grab part of that website to show on my frontpage:

Image
Dear...
Will you please share your small google maps code??
I do have the large only your small is nice to,..
Thanks/!!
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by jvdz »

rron wrote: Saturday 18 August 2018 12:29 Everything is working now with the help of JvdZ, thanks for that. :D
The script can be found on : https://github.com/jvanderzande/mijnafvalwijzer
Ronald
There is now also a version for Zuid Limburg available in github on request of @brutus.

Enjoy,
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by BarryT »

jvdz wrote: Saturday 29 September 2018 16:54
rron wrote: Saturday 18 August 2018 12:29 Everything is working now with the help of JvdZ, thanks for that. :D
The script can be found on : https://github.com/jvanderzande/mijnafvalwijzer
Ronald
There is now also a version for Zuid Limburg available in github on request of @brutus.

Enjoy,
Jos
Jos is a master!
Just saying 🤗
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
Reley
Posts: 13
Joined: Wednesday 03 August 2016 8:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: Sharing : Afval kalendar in text sensor

Post by Reley »

I am sorry guys, but i'm not able to get this to work.

I have created a LUA TIme event, pasted the code for AfvalWijzer, editted my personal info.

I then created a new Dummy Hardware named MyAfvalDevice and created a new text sensor named Afval Kalender.

The only thing I see in this sensor is 'Hello World'
I created it on monday, garbage day is thursday. So i gave it a few days to refresh, but nothing happens.

In log file:
2018-10-04 09:27:00.124 Status: LUA: @AFW:afvaltype_cfg :Groente, Fruit en Tuinafval;19;22
2018-10-04 09:27:00.124 Status: LUA: @AFW:afvaltype_cfg :Klein chemisch afval;19;22
2018-10-04 09:27:00.124 Status: LUA: @AFW:afvaltype_cfg :Restafval;19;22
2018-10-04 09:27:00.125 Status: LUA: @AFW:afvaltype_cfg :Papier en karton;12;0
2018-10-04 09:27:00.125 Status: LUA: @AFW:afvaltype_cfg :Plastic, Metalen en Drankkartons;19;22
2018-10-04 09:27:00.125 Status: LUA: @AFW:Scheduled time(s) not reached yet, so nothing to do!

any ideas?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest