An automation to trigger events based on emails from USPS

I am attaching the yaml to create an automation for doing something based on if a USPS email has been received indicating your mail was delivered. For my particular use, I am only turning on an input_boolean for 10 minutes and then turning it off. I intend to use it to turn on a light bulb in a certain color to let us know we have email to go pickup.

I created a second automation that will also do the same, but for indicating via the USPS daily digest email, indicating that there WILL /should be a mail delivery today, toggling another input boolean for the digest. I will also change this to do a different notification, that I will create.


alias: "Mail: has arrived"
description: >-
  Checks to see if there is new mail in the mail box by checking email from
  USPS, and sets the boolean mail_delivered to ON, waits 10 minutes and clears
  mail_delivered.
triggers:
  - trigger: event
    event_type: imap_content
    id: custom_event
conditions:
  - condition: and
    conditions:
      - condition: template
        value_template: |-

          {% if 'Mail Was Delivered' in trigger.event.data.subject %} true
          {% else %} false
          {% endif %}
    enabled: true
actions:
  - action: input_boolean.turn_on
    target:
      entity_id:
        - input_boolean.mail_delivered
    data: {}
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - action: input_boolean.turn_off
    target:
      entity_id:
        - input_boolean.mail_delivered
    data: {}
mode: single

If you do decide to capture both email events, the ‘Mail Was Delivered’ message needs to be changed to ‘Your Daily Digest’.

I hope this proves to be helpful to others wanting the same, or similar information captured from email.

Regards!

Just FYI, there is an integration that you can use for this that will create sensors for both mail items and packages.

I saw that, and realized it was a couple orders of magnitude
in complexity and wanted something simple and understandable.

Maybe this can be a place to collect additional methods!

Regards!

What’s the complexity? You provide it your credentials and it automatically does what your above code does without the need for toggles & automations.

Plus you can opt to include any other delivery services

For future reference, you can simplify this:

      - condition: template
        value_template: |-
          {% if 'Mail Was Delivered' in trigger.event.data.subject %} true
          {% else %} false
          {% endif %}

to this:

      - condition: template
        value_template: |-
          {{ 'Mail Was Delivered' in trigger.event.data.subject }}

Or here is an alternative that gets rid of the automation and the input Boolean, and instead just creates a regular read-only binary_sensor. Put this in your configuration.yaml:

template:
  - trigger:
      - platform: event
        event_type: imap_content
    binary_sensor:
      - name: Mail Delivered
        state: >
          {{ 'Mail Was Delivered' in trigger.event.data.subject }}
        auto_off: '00:10:00'