Hai,
Some weeks ago a friend of mine came with the idea to “read” the local government office website and extract the dates on which the various types of garbage are collected. Great job by Corne! He made a script to do so and output the results to his domoticz system, which in turn would send telegram messages the night before to remind his family to set the correct type of trash outside. Very nice idea but done in java & domoticz. But all is working great (after some debugging).
So I took up the challenge and re-wrote the thing in Python + Openhab-Python interface. The end-result is shown below. An overview of all garbage collecting moments, daily updated and a telegram message the evening before and early in the morning.
Result
How does it work?
Basically crontab on my PI runs a python script named “afval.py” once a day (afval = trash in Dutch). The script is attached below. It basically gets the webpage of the city hall, uses PyQuery to understand the page and then finds the correct data. Then do some processing as the year is not included in the data. Then the script connects to my openhab and sets the items shown in the figure above.
afval.py (6.9 KB)
Generated rules file
Then I started to write some generic rules to trigger a Telegram message based on the dates written to certain openhab items, but while I was writing the rules came up with a different approach. Why not directly generate the rules file?
Python code snippet below writes the actual rules file. (Download the entire file if you want to try yourself)
Python code
def add_openhab_rules(soort_afval, file, ophaal_datum, ophaal_datum_dag_eerder):
day = ophaal_datum.day
month = ophaal_datum.month
day2 = ophaal_datum_dag_eerder.day
month2 = ophaal_datum_dag_eerder.month
# crontab format:
# Seconds Minute Hour Day Month Year
file.write('// auto generated rules file by afval.py script @' + str(datetime.now()) + '\n')
file.write('rule \"afval_notificatie_' + soort_afval +'\" \n')
file.write('when\n')
file.write(' Time cron \"0 30 07 ')
file.write(str(day) + ' ' + str(month) + ' ?\" or\n')
file.write(' Time cron \"0 30 19 ')
file.write(str(day2) + ' ' + str(month2) + ' ?\" \n')
file.write('then\n')
file.write(' logInfo(\"Afvalkalender\", \"Afvalkalender : ' + soort_afval + '\") \n')
file.write(' sendTelegram(\"domoticahajerstijn\", \"Afval notificatie: ' + soort_afval + '\") \n')
file.write('end\n\n')
The output is following: a file called afval_generated.rules
// auto generated rules file by afval.py script @2019-12-31 13:05:26.804218
rule "afval_notificatie_Restafval"
when
Time cron "0 30 07 31 12 ?" or
Time cron "0 30 19 30 12 ?"
then
logInfo("Afvalkalender", "Afvalkalender : Restafval")
sendTelegram("domoticahajerstijn", "Afval notificatie: Restafval")
end
At the end of the python script, the generated file is copied to the correct folder and openhab automatically processes the new file.
# copy the rules files. This will not work on Windows, so a try:finally mechanism is needed.
try:
os.system('cp afval_generated.rules /etc/openhab2/rules/')
os.system('chmod 777 /etc/openhab2/rules/afval_generated.rules')
finally:
return
Must say I am very pleased with the result as there is no need to use “hidden” openhab items to store the dates and there is no need to write generic rules which in turn uses these hidden items as input.
Maybe this will trigger one of you to try a similar approach.
Grtz Matthijs