A Working USPS Component - [SOLVED] for now :)

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!

Here is another way of pulling USPS information into HA https://blog.kalavala.net/usps/homeassistant/mqtt/2018/01/12/usps.html

I apologize in advance, as it may or may not be compatible with hassio (I have not tested with hassio).

CC: @brianjking @happyleaves @Vasiley

7 Likes

Looks great. Can it use mosquitto instead of paho-mqtt?

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 :slight_smile:

Also, great idea with just counting the image parts, that’s much more reliable than using the regex.

1 Like

It uses Mosquitto only - paho-mqtt is just a python library that interacts with Mosquitto.

@skalavala – Your link doesn’t work anymore. Would love to get a working USPS component setup again. Any ideas? Thanks!

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

The USPS package is right on the main page.

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?

@matthewk I am not a hass.io user, but our good friend @ludeeus worked on making my component hass.io friendly. It is available at https://github.com/custom-components/usps_mail/

Good luck!

3 Likes

@skalavala – Thanks.

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:

$ sudo -u homeassistant -H -s
$ source /srv/homeassistant/bin/activate

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.

Thanks!

You also need to activate the venv before running this part, since paho-mqtt are installed inside it.

@skalavala @ludeeus – Oops, I missed that I accidentally exited my venv.

In my usps.service file in /etc/systemd/system do I need to somehow activate the venv first as well?

[Unit]
Description=USPS Mail Retriever
After=network.target
Requires=network.target

[Service]
ExecStart=/usr/bin/python3 /home/homeassistant/.homeassistant/private/usps.py
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

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!

I was using @happyleaves template setup seen here: https://github.com/brianjking/homeassistant-config/blob/master/sensor/deliveries.yaml#L1-L28

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:

https://github.com/brianjking/homeassistant-config/blob/master/sensor/usps.yaml#L1-L9

That would be my guess, but I have no idea :man_shrugging:

Hello! Newbie 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 :frowning: FWIW I did install mosquitto instead of using the built in.

1 Like

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.

Thanks!

Does this script still work?

Yes, it does. Although I run my Home Assistant in venv, and I cant vouch for other installation methods.

Awesome! I made this the other day: https://hub.docker.com/r/aneisch/informed-delivery-home-assistant following you blog. I hadn’t gotten any mail until today to make sure it worked. It may be useful to others!

Then I discovered and started using https://github.com/moralmunky/Home-Assistant-Mail-And-Packages.

1 Like

@ludeeus

Does the custom component based on this still work?

is it in HACS? I looked but couldn’t find it.

I have no clue

fair enough. I figured if anybody knew you would, you know, since you wrote it. :wink:

I guess I’ll try it and let you know. :slightly_smiling_face: