Getting notifications from your smart home

A chatty place back home

Smart home is a chatty place. Sometimes you want to be notified if a temperature has risen over a specific value and other times something crashed. Domoticz gives you a lot of options to sent out notifications, just take a look in the settings. If you are using the official Domoticz smart home app you can even use Google's Firebase Messaging, but for a privacy orientated smart home hacker this is naturally a no-go-area :)!
I tried a couple of things, including the Google's push messaging, but in the end settled for a good old E-Mail notification. Why? Because almost all *unix based services implement a simple mail the root if something goes wrong function. So if you setup a mail server in your smart home you can gather all those E-Mails on one place. I liked this approach.

Step One: Configure a mail server

Assumming you are using Raspbian, or any other Debian based distributing, installing a mail server is easy. Here you can find a complete description how to setup a complete mail server. If you want your E-Mail server to only receive E-Mails in the local network than the option Mail sent by Smarthost, received via SMTP or fetchmail. The important thing is to allow Exim4 to listen on the LAN interface as well (not only on localhost), otherwise services running on other hosts (e.g. I have a Synology NAS which sends notification E-Mails) will not be able to send messages to the central mail server.

Step Two: Getting the notifications out of your local network

If you want to get the notifications also when you are not at home and logged in your server, you have these options:

  1. Setup Exim4 to forward all your mails to your preferred external E-Mail address.
  2. Setup IMAP for your E-Mail server, so that you can fetch E-Mails directly.
  3. Use some bash-magic to forward the E-Mails to your XMPP or Matrix account.

I never managed to setup the Option 1, do not know why. There were always some problems with authentication between Exim4 and my mail provider.
Currently I am using the Option 2 and I have IMAPS setup with Dovecot. This is not hard to setup and works really good. The negative side here is, that you (probably) need a DynDNS setup somewhere and you are opening a port into your local network. So be aware what you are doing.
Option 3 is something I hacked up just to give it a try. Basically you say Exim4 to deliver all E-Mails to a local user and then pipe all the E-Mails to XMPP account or a Matrix room. So if you are not a E-Mail type, you can get all messages in your favourite FLOSS messenger. These are the steps you need, assuming the user who gets the root E-Mail is known as pi:

  • Create a .forward file in your home directory and type this inside:
    |sudo /sbin/runuser -l pi -c '/home/pi/forward_msgs.sh'
  • Create a forward_msgs with the below content and make it executable.
#!/bin/bash
#
# pipe e-mails from exim to xmpp or matrix recipient.
#
# dependencies: sendxmpp, matrixcli, mail-parser (pypi), html2text.
#
# create tmp files
tmpfile=$(mktemp)
# pipe the e-mail in a tmp file
cat /dev/stdin >> $tmpfile
# parse the e-mail and only extract the important information.
# extract the sender address and clean it up.
# this regex "\b[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\.[a-za-z]{2,6}\b" is for the fully qualified address, i only use the local part.
from=$(mailparser -f $tmpfile -m | grep -e -o "\b[a-za-z0-9._%+-]+@[a-za-z0-9.-]+")
subject=$(mailparser -f $tmpfile -u)
body=$(mailparser -f $tmpfile -b | html2text)
# send over xmpp.
# echo "$from:$subject=>$body" | sendxmpp -t -u moxnet -o mailbox.org anonimno@mailbox.org
# send over matrix.
matrixcli send -r '!czeapqpkuzpkgdzjot:tchncs.de' -t "$from:$subject=>$body"
rm $tmpfile
  • Dependencies sendxmpp (basic setup can be found here) and html2text can be installed using apt. for mail-parser please use pip and get it from PyPI. If on the other hand you want to use Matrix instead of XMPP, you will need matrixcli.

So that's it for now! If something is not working, just send me an E-Mail. We can debug together then.

Happy Hacking !!

License: CC BY-SA 4.0 Discuss on Mastodon