USPS Mail Camera

Tutorial:

  1. Download Google Cloud SDK
    https://cloud.google.com/sdk/docs/ (Follow the instructions for your platform)

  2. Authorize the SDK
    gcloud auth application-default login

  3. Install cloud vision python library
    https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-python

  4. My cloud vision code:
    https://github.com/keatontaylor/ha-google-cloud-vision/blob/master/vision.py (You can update the keywords in this file too)

  5. Place the vision.py file in your HA config directory.

  6. Now add the following to your HA config:

Config:

- platform: command_line
  name: Important Mail
  command: python3.6 /PATH_TO_CONFIG/vision.py
  scan_interval: 14400

Replace PATH_TO_CONFIG with the absolute path to your config directory.

I have the scan interval set to every 4 hours, the Google Vision API only allows for 1000 images analyzed. So we want to try and keep our total requests under that value. The informed delivery service can provide up to 7 images per day, and you will always get zero on sunday. The solution is just to check for important mail once per day

Anyway, let me know if this works for you. :).

3 Likes

Very, very nicely done! Glad to see jury duty made it in!!

:joy::joy::joy:

I am about to integrate this into my setup. I wanted to check though if there was any plan to make this as a usps sensor like packages and mail?

I’ve been scratching my head on how to integrate it with the camera component. If anyone wants to chime in with pointers or help that’d be awesome.

Do you already have another camera component? For example a weather gif? I ask this because I had the same issue. What I did was as follows.

1 make a cameras directory.
2 make a weather_camera.yaml file with the weather information.
- platform: generic
still_image_url: https://radblast.wunderground.com/cgi-bin/radar/WUNIDS_map?station=AMA&brand=wui&num=10&delay=15&type=N0R&frame=0&scale=1.000&noclutter=1&showstorms=0&mapx=400&mapy=240&centerx=400&centery=240&transx=0&transy=0&showlabels=1&severe=0&rainsnow=1&lightning=1&smooth=1&rand=24978256&lat=0&lon=0&label=you
name: Panhandle Weather
3 made a mail_camera.yaml file with the mail camera information.
- platform: local_file
file_path: /home/homeassistant/.homeassistant/mail/mail.gif
4 went to my configuration.yaml file and did
camera: !include_dir_merge_list cameras

Hope this helps and maybe someone can see a better way of doing this.

I thought this idea was pretty awesome so I want ahead and made the changes needed on the hass side to make this part of the standard usps component without any extra work from the end user.

If you are familiar with testing things out you can give it a try by pulling the updated usps component files from my hass repo here: https://github.com/mezz64/home-assistant/tree/usps_cam

You’ll need to tweak your configuration.yaml to be like the following:

usps:
    username: userhere
    password: password
    name: optional

I’ve done some testing and it seems to be working well, but i’ll give it a couple days to flush out any errors before I submit it as a PR to the main repo. Feedback is welcome.

2 Likes

Just tested this and it seems to be working. I’m also working on building my google vision API into a component that can digest camera entities and return a string of OCR text.

EDIT: I’d be nice if the camera showed a “no mail” image instead of it just having an error loading when zero mail items are found via myusps

Agreed on the no mail image, i’ll add that in.

I tried deleting my mail.gif because it was 3 days old and Informed delivery showed new mail now getting Mail(Error loading image). Tried deleting usps.pickle and still getting the error

I made some tweaks, including adding in a no mail image, but I rarely have no mail so it’s difficult for me to test. I’d appreciate it if someone could test that has that condition.

Also improved the camera image rotation delay and added an interval parameter to the config to make it customize-able. All changes are in my repo linked above.

I don’t see these updates on your branch. Showing last push 15 hours ago.

Sorry about that. I made the commit locally and forgot to push to github. Changes should be there now.

Thank you! I can’t say I understand the image rotation (based on what I read from your reply this exists)

Can you explain how this works with multiple mails received?

It’s just the amount of time to show each piece of mail. So if you set it at 30s the camera will show each piece of mail for 30s before switching to the next one. Anything less than 10s won’t reflect in the dashboard, only in the pop-up view, due to hass limitations with refreshing the main camera cards.

It would be completely fine to take the nomail image and toss it in the camera component folder so it can be pulled locally.

Which files do I need to pull from your repo??? Is it just the usps.py file in the sensors folder?

And do I need to add @aneisch’s changes as well?

(Sorry for the noob questions :wink: )

You need the following files:

components\usps.py
components\sensor\usps.py
components\camera\usps.py

The usps.py in the sensor folder should overwrite your existing file. You’ll also need to adjust your configuration.yaml to look like this:

usps:
    username: userhere
    password: password
    name: mailname (optional)
    interval: 10 (optional, sets delay in rotating through the images)

I did it that way on my first test run, but there isn’t necessarily a precedent for packaging in another file like that. I know the demo images are there, but they kinda serve a different purpose.

Since it is the cleaner solution i’ll probably give it a shot and can always switch back during the PR process if someone doesn’t like it.

One step after the other :wink:

Now I have the following error messages - all files are in the right folder and configuration.yaml adjusted!

2017-07-25 22:32:00 ERROR (MainThread) [homeassistant.setup] Error during setup of component usps
Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/setup.py", line 190, in _async_setup_component
component.setup, hass, processed_config)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/usps.py", line 43, in setup
conf = config[DOMAIN]
KeyError: 'usps'
2017-07-25 22:32:00 ERROR (MainThread) [homeassistant.setup] Unable to setup dependencies of sensor.usps. Setup failed for dependencies: usps
2017-07-25 22:32:00 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform sensor.usps: Could not setup all dependencies.

Thanks for your help!

Ok, lets figure out a few things… What version of HASS are you running and what version of python (looks like 3.4)? Did you have the usps component working before giving my changes a try?