Trigger an automation when an email is recieved

Is there a way to trigger an automation when an email is received?
I am trying to ring a bell every time that I get a new order on my website.
I have set up a Gmail account for my home assistant and i forward the order received email to that address.
I have tried the IMAP_CONTENT_SENSOR integration and i am able to set the state to ‘order received’ once i get an order. The problem is that i can not reset the sensor to something like ‘waiting’. I have tried to change it using the developer tools and as soon as i do, the sensor reads my inbox and reverts to ‘order received’

Is there a way to make it change state only when a new email is received?

Is there a way to forward the email directly to my home assistant server?

Is there another way to trigger an automation with an email?

I set up a new gmail account just for home assistant and i can set the state of a sensor based on the information in the subject. the problem is that it is a persistent state, even if i set the state

Delete or move the email from the inbox?

Thanks for the reply but how can I make sure that the email has been read by home assistant and then delete it?

Trigger on the email sensor state.

Home assistant can’t delete an email in my Gmail account can it?

I doubt it. Maybe you could make a filter rule in Gmail?

States values for the Gmail IMAP content sensor are templatable. For example, take a look at my post from yesterday showing how to have the sensor “age out” after a set amount of time. You could easily add additional options if you wanted to.

Also, there are many built-in options for automating mail handling within Gmail to tag, filter, move, archive, and delete messages.

1 Like

Thanks! I think that is exactly what I need. Can you post your template?
Do you know how often the IMAP content sensor checks if there is a new email?

It can. I call a python script to do it though. The IMAP integration sees the email with a specified sender/subject and then runs an automation and I have an action on a button to clear it by deleting the email. It’s pretty straightforward. The automation could also perform the call to the script to delete. In my case the trigger is an alarm has been triggered at home that sends the email so I like to see the event before I dismiss the email.

IMAP Content sensor config w/time out template
  - platform: imap_email_content
    name: X IMAP
    server: imap.gmail.com
    port: 993
    username: !secret x_google
    password: !secret x_google_pw
    folder: Inbox
    senders:
      - [email protected]
      - [email protected]
      - [email protected]
      - [email protected]
    value_template: >-
      {% if now() - (strptime(date, "%a, %d %b %Y %H:%M:%S %z")) < timedelta(minutes=5) %}
      on
      {% elif now() - (strptime(date, "%a, %d %b %Y %H:%M:%S %z")) >= timedelta(minutes=5)%}
      off
      {% else %}
      unavailable
      {% endif %}

It’s a cloud push based sensor, so Home Assistant will be notified as soon as a new state is available from Google.

Thanks. I didn’t realize that the black arrow could be expanded.

1 Like

This imap_email_content platform is only for gmail account? I tried to set it up using yahoo, but it didn’t work. The state is unknown. Maybe my settings was incorrect, but I got tried of restarting HA everytime I change settings.

No, I use it with gmail and hotmail… It should work with any email provider who allows connecting IMAP clients, but each will have their own required settings. According to Yahoo’s docs you should use the following:

sensor:
  - platform: imap_email_content
    server: imap.mail.yahoo.com
    port: 993
    username: YOUR_USERNAME
    password: YOUR_PASSWORD
    folder: YOUR_FOLDER *

* If folder: is left off the config it defaults to folder: INBOX. Neither of the providers I use worked with the default setting; they both required folder: Inbox.

Once you have it set up, the state will stay unknown until you receive a new email.

Thanks for the reply. I have tried again. Still shows unknown. Does the folder needs to be all caps, lower or proper? I have it proper as how I have created.

I’m a novice, but I needed something similar to you, and because I an unskilled unlike these other folks, I was forced to acomplish it via a very simple method. Which happens to work great for me.

In your “email sensor”, if you use “gmail search tags”, then the value of the sensor will simply be an integer: the count of emails that matched your search.

So I simply limit the timeframe of my gmail search to: the last 5 minutes (because I dont want the lights blinking more than that).

Then, it’s just an extremely simple automation, i.e just trigger the lights when the sensor (email count) changes from “0” to “1”.

sensor:

  • platform: imap
    name: emailsensor
    server: imap.gmail.com
    search:
    'X-GM-RAW “newer_than:5m subject:New Order Placed”

Then, the Automation trigger is simply…

platform: state
entity_id: sensor.emailsensor
from: ‘0’
to: ‘1’

(If someone knows how to say “if state changes from (Zero) to (>Zero)”, I would appreciate knowing how…This current automation would fail if you got two new emails at the same time)

What search tags would i use if i need to just search for words in the body of the message? Would:

“newer_then:5m Test Bla Delay” work ?

Also do you still use the value_template from Didgeridoo now that you use the newer_then:5m option ?
I’ve read somewhere that after a restart HA will scan all email, does this prevent that ?

also, would be really nice if there was a way of not having to specify a specific email address, and i can just search for all emails from a specific domain.

So it seems you use a different platform rather then imap_email_content ? Does this work better then that? I notice you don’t have to specific senders here…

The search strings run as part of Gmail.

So, you can test your search string directly in Gmail - just type the search string you want to test in the “search” box in the Gmail Web Interface…

If it works their correctly, and shows you the emails you want selected, then the same string will work in HA as well…

Use a numeric state trigger:

trigger:
  - platform: state
    entity_id: sensor.emailsensor
    above: 0

A better option might be to use a state trigger without a specific to: so you get a new notification for every new email. Add a template condition to reject changes that happen when you read or delete emails.

trigger:
  - platform: state
    entity_id: sensor.emailsensor
    not_from:
      - unavailable
      - unknown
condition:
  - condition: template
    value_template:  "{{ trigger.to_state.state | int(0) > trigger.from_state.state | int(0) }}"

Hello. Can you help me to understand how to enter this code for automation?
I already have IMAP integration installed (IMAP - Home Assistant), and my smart device added to HA.