Trigger an automation when an email is recieved

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.

So far it looks fine… except, as the error says, your automation needs an action.

In the future please do not post screen shots of configuration text. No one wants to transcribe your configuration. Instead follow community guidelines and post the actual YAML, properly formatted.

Sorry for posting as a screenshot and not the code.
How can I add an action? Thx

If you are just starting out with Home Assistant and don’t have experience with YAML, it is highly recommended that you use the UI Automation editor.

Automation Basics

If you need a primer on how to transcribe YAML automation configurations that you find here on the forums using the UI Automation Editor, there is a (now slightly dated) tutorial available on the Resin Chem Tech Youtube channel.

I’m new, so still learning both ways. I was able to finally finish the automation, and each incoming email triggers the smart socket. Is there a way to restrict this to the specific Subject of the email only?

The answer is “yes”, but can you elaborate on the end goal? There are 2 different methods and each one is suited for specific needs/uses.

I need to activate my smart socket if the email with the subject line “New Project created” is received. Currently, this code triggers a socket on every email.


description: ""
trigger:
  - platform: state
    entity_id: sensor.imap_device
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state | int(0) > trigger.from_state.state | int(0) }}"
action:
  - type: turn_on
    device_id: 0d1855262bdd676fe0c801bf47efce92
    entity_id: ac2026550abdf08c3be574edc5249237
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - type: turn_off
    device_id: 0d1855262bdd676fe0c801bf47efce92
    entity_id: ac2026550abdf08c3be574edc5249237
    domain: switch
mode: single

I suggest you take a look at the Using Events section of the IMAP docs.

If this is a method you plan to use for other automations, you should go ahead and set up a trigger-based Template sensor for email content as shown in the linked docs.

If this is more of a one-off, you can use the Event trigger and trigger variables from the docs directly as your automation’s trigger and conditions.

Let us know which of those options makes sense for your case, then we can help you modify your automation appropriately.

I would say it’s a one-time automation for this device (project) So far I have, which doesn’t seem to work, I can see the Trigger being triggered but the socket is not turning on.

alias: New automation
description: ""
trigger:
  - platform: state
    entity_id: sensor.imap_device
  - platform: event
    event_type: imap_content
    id: custom_event
    event_data:
      sender: [email protected]
      initial: true
condition:
  - condition: state
    entity_id: sensor.imap_device
    state: |-
      {% if 'Project Created' in trigger.event.data["subject"] %}
         turn_on
      {% endif %}
action:
  - type: turn_on
    device_id: 0d1855262bdd676fe0c801bf47efce92
    entity_id: ac2026550abdf08c3be574edc5249237
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - type: turn_off
    device_id: 0d1855262bdd676fe0c801bf47efce92
    entity_id: ac2026550abdf08c3be574edc5249237
    domain: switch
mode: single

It looks like you’ve mashed together too many things… :grin:

You have assigned two triggers that will likely be firing in close succession, but left the automation mode set to single so only the first of the two will actually be processed. Since the base IMAP sensor-based trigger will not return the specific trigger variables you need, there is no reason to use it.

alias: New automation
description: ""
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      sender: [email protected]
      initial: true
condition:
  - condition: template
    value_template: "{{ 'Project Created' in trigger.event.data['subject'] }}"
action:
  - type: turn_on
    device_id: 0d1855262bdd676fe0c801bf47efce92
    entity_id: ac2026550abdf08c3be574edc5249237
    domain: switch
  - delay: 10
  - type: turn_off
    device_id: 0d1855262bdd676fe0c801bf47efce92
    entity_id: ac2026550abdf08c3be574edc5249237
    domain: switch
mode: single
1 Like

Worked like a charm! Thank you!