Page 3 of 18
Re: Sharing : Afval kalendar in text sensor
Posted: Sunday 01 October 2017 12:21
by Goldwing1973
zicht wrote: ↑Sunday 01 October 2017 9:26
Thanks, i copy'd/pasted your code and the errors are gone, now i have to wait until tomorrow night for the "Hello World" to be replaced with the garbage being collected next monday.
Your welcome.
Your code will runt twice at 18h00 and 18h01
Off course if you want to test you can change 18 to the next hour.
Well done !
I just changed the time to test the code, but i'm getting an error again
2017-10-01 12:14:16.331 Error: EventSystem: in Afvalkalender: [string "commandArray = {}..."]:49: attempt to index global 'testjavascript' (a nil value)
Also it does show the correct dates in the log, but the switch isn't getting an update, it remains at "Hello World"
2017-10-01 12:15:49.352 LUA: Afval Kalender
2017-10-01 12:15:49.737 LUA: Groente,fruit & tuin ma 9 okt
2017-10-01 12:15:49.738 LUA: Metaal & Drinkpakken ma 2 okt
2017-10-01 12:15:49.738 LUA: Papier & karton wo 11 okt
I remarked the “testjavascript” line by adding — in front, the error is gone, but also my complete dashboard in Domoticz????
Disabling the Afvalkalender in events doesn’t return the Dashboard
EDIT: Restoring the database backup from an hour ago fixed the Dashboard
Re: Sharing : Afval kalendar in text sensor
Posted: Wednesday 04 October 2017 13:39
by zicht
probably you trying to set something wrong in you text device.... causing your problems
the gathering from data works well as it prints ok, you mixed the 2 scripts what will not work.
See code below, should work without any problem
You can add notifications and text as you want but be aware if you mallform a text sensor (Length to long or include faulty (HTML)code) this can mess up big time. The text code will print all you put into it, so influences the dashboard webpage !
Code: Select all
commandArray = {}
function os.capture(cmd, raw) -- os.command uitvoeren en resultaat daarvan lezen in string
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
commando='curl -s http://afvalkalender.alphenaandenrijn.nl/login/0484200000000000'
result=os.capture(commando)
if string.find(result,"rror") then
result=""
WriteToFile("Error >>> function 640"..commando,"url")
end
-- Beperk string tot infoblock van de site
local start=string.find(result,'<ul id="ophaaldata">')
local eind=string.find(result,'<i>Papier en karton</i></a>',start)
local nData=string.sub(result, start, eind)
nData=nData:gsub('%W',' ') nData=nData:match("^%s*(.-)%s*$") nData=nData:gsub("%s+", " ")
w,x=string.find(nData,"Groente fruit en tuinafval i class date")
y,z=string.find(nData," i i ",x)
Groente=string.sub(nData,x+1,y)
print("Groente,fruit & tuin"..Groente)
w,x=string.find(nData,"Metaal en Drinkpakken i class date")
y,z=string.find(nData," i i ",x)
Metaal=string.sub(nData,x+1,y)
print("Metaal & Drinkpakken "..Metaal)
w,x=string.find(nData,"Papier en karton i class date")
y,z=string.find(nData," i",x)
Papier=string.sub(nData,x+1,y)
print("Papier & karton "..Papier)
return Papier, Metaal, Groente
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')
Papier,Metaal,Groente=afval() -- roep functie aan en krijg de datums
local IDX = '525' -- text IDX
commandArray[#commandArray + 1]={['UpdateDevice']=IDX..'|0|Groente '..Groente.." Metaal "..Metaal.." papier"..Papier}
end
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 20 October 2017 22:51
by assenzuid
zicht wrote: ↑Friday 16 June 2017 10:15
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)
You can use the following input.
postcode 7827AA with number 1
Sorry for the late response
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 03 February 2018 9:37
by nf999
I recreated the script to do less parsing in lua, added timeouts to the get url, repeat if afvalwijzer get fails and send a reminder in the morning. Now only when afvalwijzer fails to respond the script goes over the 10secs and you get a warning in the log
Just for sharing since its based on the ideas and work done here. The script will run once daily and trigger a notification based on "vandaag" in the responses. The responses are stored in a dummy text variable. It's lua only (no python no dzVents) but requires curl and grep, 2 basic almost always available commands. (if you don't have curl installed, for rpi use: apt-get install curl). It's been running on my rpi2 steadily for the last month.
Add this script as domoticz time based lua event, and create the dummy device.
Code: Select all
-- afvalWijzer module
--
-- curl in os required!!
-- create dummy text device from dummy hardware 'Afval Kalender'
-- Check the timing when to get a notification below
-- based on script by zicht @ http://www.domoticz.com/forum/viewtopic.php?t=17963
-- 2017-12-27 working version
-- 2018-01-02 timeouts and repeats
commandArray = {}
local myAfvalDevice='Afval Kalender'
local Postcode='your-zip-here'
local Huisnummer='your-housenr-here'
time = os.date("*t")
-- time of notification
if time.hour==6 and time.min==47 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 commando = "curl --max-time 5 -s 'http://www.mijnafvalwijzer.nl/nl/"..Postcode.."/"..Huisnummer.."/" .. "' | grep -E 'class=\"first(Date|WasteType)'"
-- addded: grep -E '\<class=\"first(Date|WasteType)'
local tmp = os.capture(commando, 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
Re: Sharing : Afval kalendar in text sensor
Posted: Monday 05 February 2018 15:33
by mikeoo
nf999 wrote: ↑Saturday 03 February 2018 9:37
I recreated the script to do less parsing in lua, added timeouts to the get url, repeat if afvalwijzer get fails and send a reminder in the morning. Now only when afvalwijzer fails to respond the script goes over the 10secs and you get a warning in the log
Just for sharing since its based on the ideas and work done here. The script will run once daily and trigger a notification based on "vandaag" in the responses. The responses are stored in a dummy text variable. It's lua only (no python no dzVents) but requires curl and grep, 2 basic almost always available commands. (if you don't have curl installed, for rpi use: apt-get install curl). It's been running on my rpi2 steadily for the last month.
Add this script as domoticz time based lua event, and create the dummy device.
Code: Select all
-- afvalWijzer module
--
-- curl in os required!!
-- create dummy text device from dummy hardware 'Afval Kalender'
-- Check the timing when to get a notification below
-- based on script by zicht @ http://www.domoticz.com/forum/viewtopic.php?t=17963
-- 2017-12-27 working version
-- 2018-01-02 timeouts and repeats
commandArray = {}
local myAfvalDevice='Afval Kalender'
local Postcode='your-zip-here'
local Huisnummer='your-housenr-here'
time = os.date("*t")
-- time of notification
if time.hour==6 and time.min==47 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 commando = "curl --max-time 5 -s 'http://www.mijnafvalwijzer.nl/nl/"..Postcode.."/"..Huisnummer.."/" .. "' | grep -E 'class=\"first(Date|WasteType)'"
-- addded: grep -E '\<class=\"first(Date|WasteType)'
local tmp = os.capture(commando, 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
Try to use your nice script but it don't update the Text sensor. Name is exactly like yours. I get in the log file the information when and what the next pickup is but that's it.
Any hints?
LOG file shows
Code: Select all
2018-02-05 15:30:00.214 LUA: AfvalWijzer module
2018-02-05 15:30:01.049 LUA: afvalWijzer result: vrijdag 09 februari: Plastic, Metaal, Drankenkartons
Re: Sharing : Afval kalendar in text sensor
Posted: Monday 05 February 2018 19:11
by nf999
not really. Can you verify the dummy text sensor in devices tab?
mine shows as:
Hardware: Dummy
Name: Afval Kalender
Type: General
SubType: Text
and
Data: vandaag: Restafval woensdag 07 februari: Plastic, Metalen en Drankkartons
Only reason to not update is when the text sensor already has the exact same value as per:
Code: Select all
if otherdevices[myAfvalDevice] ~= tmp then
but I'm asuming that's not the case
notification should not be sent, since there is no "vandaag" in the text
How about if you remove the "if" mentioned above (and corresponding "end")?
Re: Sharing : Afval kalendar in text sensor
Posted: Tuesday 06 February 2018 7:56
by mikeoo
nf999 wrote: ↑Monday 05 February 2018 19:11
Only reason to not update is when the text sensor already has the exact same value as per:
I feel stupid. That was the problem
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 10 February 2018 8:53
by nf999
Good to see its working!
Re: Sharing : Afval kalendar in text sensor
Posted: Tuesday 20 February 2018 20:41
by LJ80
What a great idea!
Cleverly made, if you ever have time to explain how this works for my region:
https://www.gemeentewestland.nl/afval/a ... eling.html
Re: Sharing : Afval kalendar in text sensor
Posted: Wednesday 07 March 2018 18:36
by bvansteyn
@nf999:
Is there a way to test the script beforehand? I've made the dummy sensor last week and copied your script to LUA and activated it. I had to wait a week to see if it was working (because tuesday is garbage day). I saw some logging, but no message was sent. To bad I had to reboot the system and now my logging is gone and I don't want to wait another week. How can I edit the script to test if it is working?
Can you maybe post a short howto use this within Domoticz? It's my first script and I've little knowledge of domoticz. Do I have to do anything else within Domoticz next to making the dummy sensor with this parameters?;
Hardware: Dummy
Name: Afval Kalender
Type: General
SubType: Text
Data: Hello World (is default text? Should I replace this with something else?)
Edit; this is the log showing:
Code: Select all
2018-03-09 06:47:00.378 dzVents: AfvalWijzer module
2018-03-09 06:47:00.704 dzVents: os.capture needed more than 1 call: 5
2018-03-09 06:47:00.704 dzVents: afvalWijzer: Empty result from curl command
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 March 2018 14:09
by BarryT
Great script, thanks!
I only want to have the "firstWasteType" instead of the date also..
Is that possible, and if yes how?
Edit, found it allready..
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 March 2018 18:37
by jvdz
I have the basics working for gemeente Westland, Just need to clean it up a little. Will post something soon.
Jos
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 16 March 2018 19:16
by BarryT
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
this is not working..
i wish to have this notification one day before they come, @17.00 hour for example..
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 March 2018 12:08
by jvdz
Adding 1 to the current day will not work when today is the last day of the month and you want to get a warning for tomorrow.
I have solved that by using os.difftime().
See my next post for usage in het Westland which contains this logic when you are interested.
Jos
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 March 2018 12:13
by jvdz
@LJ80 ,
This is what I have working now. Just follow the instructions at the top of the script and you should be in business.
I have the script saved in "/home/domoticz/scripts/lua/script_time_huisvuil-westland.lua" but you can also save it in the internal editor as TIME script,
Let me know when you have issues or questions.
EDIT:
- some code updates
- fixed juni which should be jun in the table
- move the code to :
https://github.com/jvanderzande/mijnafvalwijzer file
script_time_huisvuil-westland.lua
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 March 2018 14:14
by BarryT
jvdz wrote: ↑Saturday 17 March 2018 12:13
@LJ80 ,
This is what I have working now. Just follow the instructions at the top of the script and you should be in business.
I have the script saved in "/home/domoticz/scripts/lua/script_time_huisvuil-westland.lua" but you can also save it in the internal editor as TIME script,
Let me know when you have issues or questions.
EDIT: some code updates
Can you do the same, but then with the afvalwijzer? this is very handy!
thankss
Re: Sharing : Afval kalendar in text sensor
Posted: Saturday 17 March 2018 14:26
by jvdz
BarryT wrote: ↑Saturday 17 March 2018 14:14
Can you do the same, but then with the afvalwijzer? this is very handy!
thankss
Shouldn't be too hard, but you will have to provide me with the current script you use and a postcode to test with, so I can test the basics.
PM is fine too in case you don't want to share your address info in open forum.
Jos
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 23 March 2018 16:48
by nf999
bvansteyn wrote: ↑Wednesday 07 March 2018 18:36
Is there a way to test the script beforehand? I've made the dummy sensor last week and copied your script to LUA and activated it. I had to wait a week to see if it was working (because tuesday is garbage day). I saw some logging, but no message was sent. To bad I had to reboot the system and now my logging is gone and I don't want to wait another week. How can I edit the script to test if it is working?
Code: Select all
2018-03-09 06:47:00.378 dzVents: AfvalWijzer module
2018-03-09 06:47:00.704 dzVents: os.capture needed more than 1 call: 5
2018-03-09 06:47:00.704 dzVents: afvalWijzer: Empty result from curl command
srry for the late reply ..
yes you can check the call to afvalwijzer. Just add
just after the
Execute the command from the command line and this should give some result. Could very well be that eg curl is not installed.
As to the howto, the basics are in the script itself, and since you already have loggings the script is running. Issue is probably related to the command being executed, 2b identified with by adding extra logging via the print command above.
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 23 March 2018 17:11
by BarryT
There is a new script, made by "jvdz"
This script is tested before, in many ways and the result is just what you need..
Check it here >>
https://github.com/jvanderzande/mijnafvalwijzer
All credits goes to Jos
Re: Sharing : Afval kalendar in text sensor
Posted: Friday 23 March 2018 17:46
by jvdz
I have indeed stored the scripts in
Github, which allows for others to contribute modification if required.
This Github currently version for:
Enjoy,
Jos