Mail and Packages: Custom component for UPS, FEDEX, and USPS

That’s too bad. Thank you for the quick reply!

Hey,

Trying to get this set up and seem to be in an initialising/failed to initialise death spiral; initially the integration seemed to fail to connect with IMAP (checked all creds were correct, even confirmed with the separate IMAP integration).

Removed and reinstalled this integration, still in a death spiral, though according to the logs it DOES seem to be pulling emails… it just seems to get so far and then restarts :confused: I’ve changed the refresh to be every 60minutes but still no dice. (I also modified the code to remove the amazon, etc domains that weren’t relevant to me to speed things up!).

Initially I had created a shedload of sensors but I’ve since cut this down for testing; still no dice.

Any ideas?
Thanks in advance!

Increase your IMAP timeout.

Thanks for the speedy response! I had it at 600s but it was still having issues. I reinstalled and was more selective and that seems to have done the trick though!

Edit: seemingly this was causing a lot of errors where client connections were maxed out so I’ve had to disable the integration. I’m fairly sure it was caused by this integration but I cannot work out why or how it would be misbehaving.

Feature request:

I know I can accomplish it via a template, but it would be great if the integration provided a native sensor that automatically summed all the deliveries for all the carriers the user has configured.

e.g. if I have (1) FedEx delivery and (2) UPS deliveries coming, the ’ total_deliveries ’ sensor would have a value of (3).

(I did look, I don’t think it currently exists?!?)

Thanks!

Sensor already exists sensor.mail_packages_in_transit

Hello all, I submitted a feature request on GitHub but was wondering if there is an easier (faster) way. My mail is all delivered to a PO Box and I get an email from USPS that it’s ready for pickup. This doesn’t seem to trigger anything in the integration. Can I edit or update something to look for packages that are ready to be picked up?

Once I picked it up, it did trigger a “delivered” message which tells me the integration is working correctly.

You might be able to add the subject Ready for Pickup to the const.py file.

add it to the list of subjects here, maybe add Your package is ready for pickup in to the body list as well.

Thanks I’ve given this a shot. Now all I need is someone to send me a package! :slight_smile:

I wanted to double-check the behavior I was seeing before responding, but this doesn’t appear to work for me.

Right now I have:

sensor.mail_amazon_packages = 1
sensor.mail_packages_in_transit = 0

When I first noticed it I had: Amazon=1, USPS=1, Total=1

Thanks,

Adam

This would be accurate. Review the wiki.

Thanks for replying! I had read the Wiki but guess I missed it. It was right there in bold too:

Amazon is not fully integrated into the integrations accumulative sensors.

I thought perhaps I could just create a template sensor to add the Amazon count to the total deliveries, but I guess if it was that easy Amazon would be integrated already :slightly_smiling_face:

I have re-read the Wiki text a few times and can’t get my head around how the counts are expected to work for Amazon deliveries. I have noticed that if an Amazon order is going to be delivered by USPS, my Amazon and USPS counters will both show ‘1’…I guess that is the behavior the Wiki text is getting at? Would be great if there was a way to know they are both referencing the same physical package.

Thanks again

Correct.

Unfortunately this isn’t feasible with the way Amazon does their email notifications, and this is why they are not counted in the total counts.

I had a USPS package come today but the intergration shows neither that a package was delivered or that a package was delivering today. I have the USPS INFORMRED DELIVERY email in the same folder of my gmail that my Amazon emails go to, and Amazon counts in the integration are correct.

image

The sample notification from the configuration github errors or warns in the HA log now. So:

- platform: template
  sensors:
    mail_deliveries_message:
      friendly_name: "Deliveries Summary"
      value_template: > 
        {# Deliveries Sentence #}
          {% macro deliveries_sentence() -%}
                {%- if states("sensor.mail_usps_mail")|int(0) == 0 -%}
                  No
                {%- else -%}
                  {{states("sensor.mail_usps_mail")|int(0)}}
                {%- endif -%}
              {{' '}} 
                {%- if states("sensor.mail_usps_mail")|int(0) <= 1 -%}
                  pieces of mail
                {%- else -%}
                  pieces of mail
                {%- endif -%}
              {{' '}}will be delivered.{{' '}} 
                {%- if states("sensor.mail_usps_delivering")|int(0) == 0 -%}
                  No
                {%- else -%}
                  {{states("sensor.mail_usps_delivering")|int(0)}}
                {%- endif -%}
              {{' '}} 
                {%- if states("sensor.mail_usps_delivering")|int(0) == 1 -%}
                  USPS package is
                {%- else -%}
                  USPS packages are
                {%- endif -%}
              {{' '}}in transit.{{' '}}
                {%- if states("sensor.mail_fedex_delivering")|int(0) == 0 -%}
                  No
                {%- else -%}
                  {{states("sensor.mail_fedex_delivering")|int(0)}}
                {%- endif -%}
              {{' '}} 
                {%- if states("sensor.mail_fedex_delivering")|int(0) == 1 -%}
                  FedEx package is
                {%- else -%}
                  Fedex packages are
                {%- endif -%}
              {{' '}}in transit.{{' '}}
              {%- if states("sensor.mail_ups_delivering")|int(0) == 0 -%}
                  No
                {%- else -%}
                  {{states("sensor.mail_ups_delivering")|int(0)}}
                {%- endif -%}
              {{' '}} 
                {%- if states("sensor.mail_ups_delivering")|int(0) == 1 -%}
                  UPS package is
                {%- else -%}
                  UPS packages are
                {%- endif -%}
              {{' '}}in transit.{{' '}}
              {%- if states("sensor.mail_amazon_packages")|int(0) == 0 -%}
                  No
                {%- else -%}
                  {{states("sensor.mail_amazon_packages")|int(0)}}
                {%- endif -%}
              {{' '}} 
                {%- if states("sensor.mail_amazon_packages")|int(0) == 1 -%}
                  Amazon package is
                {%- else -%}
                  Amazon packages are
                {%- endif -%}
              {{' '}}in transit.{{' '}}
          {%- endmacro %}
        {{deliveries_sentence()}}

is returning

2023-11-08 19:23:03.042 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({# Deliveries Sentence #}
  {% macro deliveries_sentence() -%}
        {%- if states("sensor.mail_usps_mail")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_usps_mail")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_usps_mail")|int <= 1 -%}
          pieces of mail
        {%- else -%}
          pieces of mail
        {%- endif -%}
      {{' '}}will be delivered.{{' '}}
        {%- if states("sensor.mail_usps_delivering")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_usps_delivering")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_usps_delivering")|int == 1 -%}
          USPS package is
        {%- else -%}
          USPS packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
        {%- if states("sensor.mail_fedex_delivering")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_fedex_delivering")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_fedex_delivering")|int == 1 -%}
          FedEx package is
        {%- else -%}
          Fedex packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
      {%- if states("sensor.mail_ups_delivering")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_ups_delivering")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_ups_delivering")|int == 1 -%}
          UPS package is
        {%- else -%}
          UPS packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
      {%- if states("sensor.mail_amazon_packages")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_amazon_packages")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_amazon_packages")|int == 1 -%}
          Amazon package is
        {%- else -%}
          Amazon packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
  {%- endmacro %}
{{deliveries_sentence()}}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 568, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2231, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 64, in top-level template code
  File "/usr/local/lib/python3.11/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/runtime.py", line 777, in _invoke
    rv = self._func(*arguments)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "<template>", line 3, in template
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1944, in forgiving_int_filter
    raise_no_default("int", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1626, in raise_no_default
    raise ValueError(
ValueError: Template error: int got invalid input 'unknown' when rendering template '{# Deliveries Sentence #}
  {% macro deliveries_sentence() -%}
        {%- if states("sensor.mail_usps_mail")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_usps_mail")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_usps_mail")|int <= 1 -%}
          pieces of mail
        {%- else -%}
          pieces of mail
        {%- endif -%}
      {{' '}}will be delivered.{{' '}}
        {%- if states("sensor.mail_usps_delivering")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_usps_delivering")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_usps_delivering")|int == 1 -%}
          USPS package is
        {%- else -%}
          USPS packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
        {%- if states("sensor.mail_fedex_delivering")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_fedex_delivering")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_fedex_delivering")|int == 1 -%}
          FedEx package is
        {%- else -%}
          Fedex packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
      {%- if states("sensor.mail_ups_delivering")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_ups_delivering")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_ups_delivering")|int == 1 -%}
          UPS package is
        {%- else -%}
          UPS packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
      {%- if states("sensor.mail_amazon_packages")|int == 0 -%}
          No
        {%- else -%}
          {{states("sensor.mail_amazon_packages")|int}}
        {%- endif -%}
      {{' '}}
        {%- if states("sensor.mail_amazon_packages")|int == 1 -%}
          Amazon package is
        {%- else -%}
          Amazon packages are
        {%- endif -%}
      {{' '}}in transit.{{' '}}
  {%- endmacro %}
{{deliveries_sentence()}}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 694, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 570, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: int got invalid input 'unknown' when rendering template '{# Deliveries Sentence #}

Thoughts?

Anyone else having issues with the integration/sensors going unavailable pretty frequently lately? I’m using Gmail and just happened to notice that in the last 2 days it’s gone unavailable a TON. Reloading doesn’t necessarily bring it back either. I just upped my ‘timeout’ time from 30 to 60s to see if that helps. Guessing it’s a Gmail issue?

Yes that’ll be gmail having issues.

I would like to see that to. I have managed to end up with only the USPS portion working.

The card only grabs the USPS image last I checked, but you can add a picture entity card to display the amazon one.

Here’s an example:

show_state: false
show_name: false
camera_view: auto
type: picture-entity
entity: camera.mail_amazon_delivery_camera
camera_image: camera.mail_amazon_delivery_camera
aspect_ratio: '4:3'