I know the USPS component has not been working for a while now, and lot of folks are frustrated. I got pointed to a python code by @Vasiley apparently written by @arychj (great start) that uses imap to login to your email account and pulls the number of mails/packages using regular expressions and string search.
I took that base code and expanded it to integrate with Home Assistant with a simple [few] steps and no longer parse the html content of the email. The new python program does not run within Home Assistant, rather it runs as a standalone program that queries the inbox, and publishes the mail and package counts to MQTT, and also extracts images from the mail, stitches them together to make an animated GIF, and that animated GIF is loaded into Home Assistant UI via camera component. I know, it sounds crazy, but woks like charm!
Awesome work! Thanks for picking this up and running with it. The code I posted originally was just a quick and dirty proof of concept that I had intended to clean up later, but now I don’t have to
Also, great idea with just counting the image parts, that’s much more reliable than using the regex.
I’ve updated the link now… I have been playing around with the blog site and didn’t realize the link got changed. It is still in the same domain and the root URL is still the same - which is https://blog.kalavala.net
Hi @skalavala, for those of us running Hass.io (I’m on version 0.79.3, running on a RPi 3 B+), how would we get Paho MQTT and Imagemagick installed? Is it possible?
I followed the setup instructions from your blog here.
However, I’m using Hassbian and if I try to run sudo pip install paho-mqtt I get a pip: command not found error. If I activate my venv by following the instructions from the Hassbian page here:
I can run pip install paho-mqtt successfully (see below):
(homeassistant) homeassistant@hassbian:/home/pi $ pip install paho-mqtt
Looking in indexes: https://pypi.python.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: paho-mqtt in /srv/homeassistant/lib/python3.5/site-packages (1.3.1)
I also am able to confirm that I have MQTT installed based on this page.
However, where I get stuck is when I attempt to test my usps.py file by running python usps.py manually:
homeassistant@hassbian:~/.homeassistant/private $ python usps.py
Traceback (most recent call last):
File "usps.py", line 13, in <module>
import paho.mqtt.client as mosquitto
ImportError: No module named paho.mqtt.client
I also verified MQTT's service is in fact running:
pi@hassbian:/etc/systemd/system $ sudo systemctl status mosquitto.service
â—Ź mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; generated; vendor preset: enabled)
Active: active (running) since Fri 2018-10-12 16:29:08 UTC; 1h 30min ago
Docs: man:systemd-sysv-generator(8)
CGroup: /system.slice/mosquitto.service
└─28496 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Oct 12 16:29:08 hassbian systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker...
Oct 12 16:29:08 hassbian mosquitto[28490]: Starting network daemon:: mosquitto.
Oct 12 16:29:08 hassbian systemd[1]: Started LSB: mosquitto MQTT v3.1 message broker.
I’m seeing this when I run python usps.py from my venv:
(homeassistant) homeassistant@hassbian:~/.homeassistant/private $ python usps.py
12-Oct-2018 19:10:56PM USPS: Connected to MQTT Server successfully
12-Oct-2018 19:10:57PM USPS: Logged into your email server successfully!
12-Oct-2018 19:10:58PM USPS: Selecting folder 'inbox'
12-Oct-2018 19:10:58PM USPS: Found '0' mails
12-Oct-2018 19:10:58PM USPS: Found '0' packages
12-Oct-2018 19:10:58PM USPS: Error occured while either processing email or publishing messages to MQTT.
12-Oct-2018 19:10:58PM USPS: [Errno 2] No such file or directory: '/home/homeassistant/.homeassistant/www/todays_mails.gif'
I see the message below when I check the status of the usps.service:
pi@hassbian:~ $ sudo systemctl status usps.service
â—Ź usps.service - USPS Mail Retriever
Loaded: loaded (/etc/systemd/system/usps.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Fri 2018-10-12 19:51:06 UTC; 2s ago
Process: 2768 ExecStart=/usr/bin/python3 /home/homeassistant/.homeassistant/private/usps.py (code=exited, status=1/FAILURE)
Main PID: 2768 (code=exited, status=1/FAILURE)
Oct 12 19:51:06 hassbian systemd[1]: usps.service: Failed with result 'exit-code'.
Thanks, any guidance would be greatly appreciated!
However, with the FedEx component having issues as you can see here and the USPS sensor original setup being borked at the moment I have my USPS setup here:
This was working well for me until recently. I’ve recently rebuilt the venv to eliminate any other issues, but I can’t seem to get the script to publish to mqtt. I can use all of the same vars on the cli with py2 and py3 in and out of the venv and I can call mqtt just fine with the same credentials through HA and this script appears to work, but the messages never post. I edited the script to use a connection loop and I’m getting a 5 error: not authorised when trying to connect. I can’t figure out how this stopped working FWIW I did install mosquitto instead of using the built in.
Those of you using my USPS code, I made a small update to it recently. USPS has decided to throw advertisements in the emails, that messes up the actual mail count. Mails are counted based on the number of email attachments (pictures of mails), and since those advertisements are also images, they are counted as mails. Luckily I noticed a pattern where all the advertisements have underscore (_) in the file name for advertisements. I added a simple condition to avoid ads when counting emails.