I was inspired by Luke Smith’s video and made my own notification script about COVID19 stats in Germany and in the world.
My setups is the following: Raspberry Pi with OpenHab automation platform with a Telegram binding.
Script for pulling data
I use the same GitHub project Coronavirus Tracker CLI, because I found its simple table output format very useful for processing.
First, create script folder and file:
1
2
|
mkdir /opt/corona-update
touch /opt/corona-updade/corona-info.sh
|
Then write following script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/usr/bin/sh
CURDATE=$(date +'%R %d-%m-%Y')
TEMPFILE="/temp/coronaupdate"
# I pull necessary information into the file first.
# This makes local processing faster because I need only one CURL request for each update.
# sed 's/\x1B\[[0-9;]*[JKmsu]//g' removes escape color codes for terminal.
curl -s https://corona-stats.online/Germany?source=2 | sed 's/\x1B\[[0-9;]*[JKmsu]//g' > ${TEMPFILE}
echo "_COVID19 stats_"
echo "_${CURDATE}_"
echo
# sed 's/║//g' removes outer borders of the table.
# sed 's/│/;/g' replaces inner borders with ";", which I later use as a delimiter in AWK.
# sed 's/▲//g' removes ▲ symbols.
# sed 's/\s\+//g' removes extra spaces.
# sed 's/,/ /g' puts space as a delimiter of thousands, millinions, etc. instead of ",".
grep "Germany" ${TEMPFILE} | sed 's/║//g; s/│/;/g; s/▲//g; s/\s\+//g; s/,/ /g' | awk -F';' '{print "*Germany*\nTotal number of infected: "$3"\nNew cases: "$4"\nTotal deaths: "$5"\nTotal number of recovered: "$7"\nCases per 1 mln: "$10}'
echo
grep "World" ${TEMPFILE} | sed 's/║//g; s/│/;/g; s/▲//g; s/\s\+//g; s/,/ /g' | awk -F';' '{print "*World*\nTotal number of infected: "$3"\nNew cases: "$4"\nTotal deaths: "$5"\nTotal number of recovered: "$7"\nCases per 1 mln: "$10}'
|
My configuration of OpenHab binding with Telegram bot is configured to use Markdown syntax for the formatting.
Then make file executable and change ownership to openhab
user, otherwise OpenHab rules engine will not be able to process the file.
1
2
|
chmod 750 /opt/corona-update/corona-info.sh
sudo chown openhab:openhab /opt/corona-update/corona-info.sh
|
I also delete temporary file from the /temp
directory which was created during troubleshooting and dry run of the script. It is necessary to remove this file because when script will be run by the openhab
user, it will not be able to write in this temporary file because it was created by another user with default rw-rw-r--
rights.
And now I am ready for my OpenHab rule.
OpenHab rule
In the OpenHab rules folder I simply created new file called telegram_corona.rules
with the following content:
1
2
3
4
5
6
7
|
rule "corona update"
when Time cron "0 30 8,19 * * ?" // updates are sent at 8:30 and at 19:30 every day
then
var String corona = executeCommandLine("/opt/corona-update/corona-info.sh", 5000)
// Use name of your Telegram bot instead of <bot_name>.
sendTelegram("<bot_name>", corona + "\n")
end
|
Job is done. Now I am receiving information about COVID19 statistics twice per day from my Telegram bot.