Page 1 of 18
Sharing : Afval kalendar in text sensor
Posted: Thursday 15 June 2017 16:12
by zicht
Hi
I made a script that will put a text dummy sensor with data of the next garbage calendar date (dutch)
It makes use of the site
http://www.mijnafvalwijzer.nl/ so you can test upfront if it works
So you have to make a dummie text sensor in hardware
Code: Select all
function os.capture(cmd, raw) -- os.command uitvoeren en resultaat daarvan lezen in string
if (dbug == true) then print("Os.Capture") end
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
function Afval()
-- haal data op van site
local Postcode='1234AA'
local Huisnummer='111'
commando = 'curl -s http://www.mijnafvalwijzer.nl/nl/'..Postcode..'/'..Huisnummer..'/'
result = os.capture(commando)
-- Beperk string tot infoblock van de site
local start = string.find(result,'title="Eerstvolgende ophaalmoment"')
local eind = string.find(result,'class="mobile pageBlock first')
local nData = string.sub(result, start, eind)
-- Haal uit het blok de data die nodig is
local test = " " i=0
while i<20 and string.find(nData,'>') and string.find(nData,'<',e) do
s= string.find(nData,'>') +1 e= string.find(nData,'<',s) -1 l= string.len(nData)
test = test .. string.sub(nData, s, e)
nData = string.sub(nData, e, l)
i = i+1
end
nData = test
-- strip onnodige troep (die er al het goed is niet is , just to be sure)
nData = nData:gsub('%W',' ') nData = nData:match("^%s*(.-)%s*$") nData=nData:gsub("%s+", " ")
return nData
end
I use the function os.capture as i am on windows, do not want to use openurl as i need to capture the content. Its a general function i call more often for other purposed using curl. (standalone curl.exe in domoticz directory)
i call it on time once a day in a lua_time script :
Code: Select all
time = os.date("*t")
if time.hour==7 and time.min<=1 then
print('Afval module')
local IDX = '123' --your IDX
local tmp = Afval()
if otherdevices['Afval Kalender'] ~= tmp then
commandArray['UpdateDevice'] = IDX..'|0|'..tmp
end
end
IDX = the idx of you text sensor you need te replace by yours, as wel as you postalcode and housenumber to make things work
Your text sensor will show a message like in my log : 2017-06-15 16:13:02.838 LUA: vandaag Papier en karton maandag 19 juni Restafval
I also push a notification on the text change but you can imagin how that has to be done
If you're free to use or to optimize. ( i am not a coder at al so things are quick an dirty)
Re: Sharing : Afval kalendar in text sensor
Posted: Thursday 15 June 2017 17:34
by pwhooftman
Nice
I grab part of that website to show on my frontpage:

Re: Sharing : Afval kalendar in text sensor
Posted: Thursday 15 June 2017 22:19
by assenzuid
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 10:15
by zicht
Just had a look at that site, but its very local (could not find my schedule at all) --> not able to test.
And based on your request i found that not all communties are supported yet on the site i used

(added a link to test in first post)
Can you provide me with some valid input for the site you mentioned ?
(it should be possible to make the script work for all sites as its based on source code reading)
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 10:22
by tlpeter
How easy is it for you to create a script for on a raspberry?
The lua script can be used anyway
I went to a raspberry as most script where made for that and now you made one for Windows

Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 10:48
by Thomasdc
would it be possible to let this work in belgium?
this is a site where the info can be found:
http://www.ophaalkalender.be/
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 11:17
by zicht
tlpeter wrote:How easy is it for you to create a script for on a raspberry?
The lua script can be used anyway
I went to a raspberry as most script where made for that and now you made one for Windows

Thanks, i was surprised to discover the site does not cover all , so added a test link.
It should work from the lua in RPI as well (curl is standard supported normally)
I read raw source code (as i am not a coder at all i do not know any better way) --> its a matter of having a valid postalcode to see how the site is build and then adjust the search criterea (if you provide me with valid enty i can have a look, its difficult to gues out of the blue

)
But this lua ONLY works if we can get the data visible in the source code of the site !
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 11:30
by tlpeter
I get an error:
017-06-16 11:29:00.336 Error: EventSystem: in AfvalKalender2: [string "..."]:8: attempt to call global 'Afval' (a nil value)
Afval is the function i guess with a tmp but where is that tmp file?
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 11:40
by tlpeter
Ok, i just put both scripts in one Lua and now it is working.
I will add some print and notifications in it.
Thanks as i now sometimes forget "oud papier" and "plastic"
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 12:17
by zicht
tlpeter wrote:Ok, i just put both scripts in one Lua and now it is working.
I will add some print and notifications in it.
Thanks as i now sometimes forget "oud papier" and "plastic"
great that it works out!
No temp file .. why using a temp file if you can use memory ? (thats why i use the os.capture

)
Indeed the function needs to be availabe for calling in the script

Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 13:03
by Thomasdc
zicht wrote:
I read raw source code (as i am not a coder at all i do not know any better way) --> its a matter of having a valid postalcode to see how the site is build and then adjust the search criterea (if you provide me with valid enty i can have a look, its difficult to gues out of the blue

)
postal code: 8554
street name: Kooigemstraat
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 13:04
by Thomasdc
zicht wrote:
I read raw source code (as i am not a coder at all i do not know any better way) --> its a matter of having a valid postalcode to see how the site is build and then adjust the search criterea (if you provide me with valid enty i can have a look, its difficult to gues out of the blue

)
postal code: 8554
street name: Kooigemstraat
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 June 2017 16:14
by zicht
Sorry Man : this is beyond my programming skills --> I need to fill in (put) data by a script in the input field (not available via direct link anyway) and i do not know how to do this

next is that in the source-code the values are not visible after filling in the requested input. As we see it on the display it should be somewhere, but here i also run into skill limitation
Same for this one

icons in calendar are serverside loaded, no way with my programming skills i can transfer this into something useable for domoticz. Think it could be possible, just lack of skils on my side

(tested with 7741AM)
Sorry to let you down ...
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 June 2017 12:34
by snuiter
Hi tipeter,
can you share the code you have put in Lua, specifically the first script as I am not able to get it working.
Thanks,
Ivo
tlpeter wrote:Ok, i just put both scripts in one Lua and now it is working.
I will add some print and notifications in it.
Thanks as i now sometimes forget "oud papier" and "plastic"
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 June 2017 14:07
by tlpeter
Sure, do not forget to set your postal code and housenumber!!!
Make a virtual text button and give it the name "Afval Kalender"
Code: Select all
commandArray = {}
function os.capture(cmd, raw) -- os.command uitvoeren en resultaat daarvan lezen in string
if (dbug == true) then print("Os.Capture") end
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
function Afval()
-- haal data op van site
local Postcode='1000AA'
local Huisnummer='1'
commando = 'curl -s http://www.mijnafvalwijzer.nl/nl/'..Postcode..'/'..Huisnummer..'/'
result = os.capture(commando)
-- Beperk string tot infoblock van de site
local start = string.find(result,'title="Eerstvolgende ophaalmoment"')
local eind = string.find(result,'class="mobile pageBlock first')
local nData = string.sub(result, start, eind)
-- Haal uit het blok de data die nodig is
local test = " " i=0
while i<20 and string.find(nData,'>') and string.find(nData,'<',e) do
s= string.find(nData,'>') +1 e= string.find(nData,'<',s) -1 l= string.len(nData)
test = test .. string.sub(nData, s, e)
nData = string.sub(nData, e, l)
i = i+1
end
nData = test
-- strip onnodige troep (die er al het goed is niet is , just to be sure)
nData = nData:gsub('%W',' ') nData = nData:match("^%s*(.-)%s*$") nData=nData:gsub("%s+", " ")
return nData
end
time = os.date("*t")
if time.hour==7 and time.min<=1 then
print('Afval module')
local IDX = '2362' --your IDX
local tmp = Afval()
if otherdevices['Afval Kalender'] ~= tmp then
commandArray['UpdateDevice'] = IDX..'|0|'..tmp
end
end
return commandArray
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 June 2017 15:29
by snuiter
Thanks it is working now
Re: Sharing : Afval kalendar in text sensor
Posted: Monday 03 July 2017 19:06
by K3rryBlue
Added a small piece to the code, so Domoticz will sent a notification to my phone, the evening when the garbage needs to be taken outside.
Code: Select all
time = os.date("*t")
-- Only want to be notified on the evenning the garbage can needs to be put outside.
if time.hour==18 and time.min<=1 then
print('Afval module')
local IDX = '400' --your IDX
local tmp = Afval()
if otherdevices['Afval Kalender'] ~= tmp then
commandArray['UpdateDevice'] = IDX..'|0|'..tmp
end
--Added to sent notification, on Tuesday (sunday=1)
if os.date("*t").wday==3 then
commandArray['SendNotification']='Garbage can#'..tmp
end
end
Re: Sharing : Afval kalendar in text sensor
Posted: Wednesday 05 July 2017 19:03
by tlpeter
K3rryBlue wrote:Added a small piece to the code, so Domoticz will sent a notification to my phone, the evening when the garbage needs to be taken outside.
Code: Select all
time = os.date("*t")
-- Only want to be notified on the evenning the garbage can needs to be put outside.
if time.hour==18 and time.min<=1 then
print('Afval module')
local IDX = '400' --your IDX
local tmp = Afval()
if otherdevices['Afval Kalender'] ~= tmp then
commandArray['UpdateDevice'] = IDX..'|0|'..tmp
end
--Added to sent notification, on Tuesday (sunday=1)
if os.date("*t").wday==3 then
commandArray['SendNotification']='Garbage can#'..tmp
end
end
I am gonna try that one. Tomorrow morning is garbage bin day

It will hopefully send a notification tomorrow morning at 7
Re: Sharing : Afval kalendar in text sensor
Posted: Wednesday 05 July 2017 19:16
by K3rryBlue
tlpeter wrote:K3rryBlue wrote:Added a small piece to the code, so Domoticz will sent a notification to my phone, the evening when the garbage needs to be taken outside.
Code: Select all
time = os.date("*t")
-- Only want to be notified on the evenning the garbage can needs to be put outside.
if time.hour==18 and time.min<=1 then
print('Afval module')
local IDX = '400' --your IDX
local tmp = Afval()
if otherdevices['Afval Kalender'] ~= tmp then
commandArray['UpdateDevice'] = IDX..'|0|'..tmp
end
--Added to sent notification, on Tuesday (sunday=1)
if os.date("*t").wday==3 then
commandArray['SendNotification']='Garbage can#'..tmp
end
end
I am gonna try that one. Tomorrow morning is garbage bin day

It will hopefully send a notification tomorrow morning at 7
If you change os.date("*t").wday==5 and adjust the time time.hour==7 and time.min<=1 it will
Sunday=1, Monday=2, Tuesday=3, Wednesday=4, Thursday=5, Friday=6 and Saturday=7
If you want a daily update in the text sensor, and once per week an e-mail. You can also use this:
Code: Select all
if time.hour==18 and time.min<=1 then
local tmp = Afval()
if os.date("*t").wday==3 then
commandArray['SendNotification']='Garbage can#'..tmp
end
end
if time.hour==7 and time.min<=1 then
print('Afval module')
local IDX = '400' --your IDX
local tmp = Afval()
if otherdevices['Afval Kalender'] ~= tmp then
commandArray['UpdateDevice'] = IDX..'|0|'..tmp
end
end
Re: Sharing : Afval kalendar in text sensor
Posted: Wednesday 05 July 2017 20:45
by dutchdevil83
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