Trigger an automation when an email is recieved

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
2 Likes

Worked like a charm! Thank you!