Using the IMAP integration to create a custom event from an email - HELP PLEASE!

I’ve spent some time searching through the forums, but can’t seem to find an answer to my problem.

I have a warning at the bottom of my template: “This template does not listen for any events and will not update automatically”.

Any thoughts on what I’m doing wrong? I believe I have followed the instructions here.

Where are you seeing that warning? If in the Template editor tool, that is normal. The variable trigger has no definition in the the Template editor (unless you give it one). In the linked documented configurations it is generated by the actual trigger of the sensor and is only available inside the sensor.

Thanks @Didgeridrew, it is showing that in the template editor.

My template still has a state of ‘Unavailable’ even after a few days and several emails have been received. Any idea why that would be?

You will need to post the configuration… almost any error in the trigger or state could lead to a state value of ‘unavailable’ that doesn’t update the way you expect it to.

I copied it from the example on the IMAP integration documents.

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "custom_event"
    sensor:
      - name: imap_content
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Message: "{{ trigger.event.data['text'] }}"
          Server: "{{ trigger.event.data['server'] }}"
          Username: "{{ trigger.event.data['username'] }}"
          Search: "{{ trigger.event.data['search'] }}"
          Folder: "{{ trigger.event.data['folder'] }}"
          Sender: "{{ trigger.event.data['sender'] }}"
          Date: "{{ trigger.event.data['date'] }}"
          Subject: "{{ trigger.event.data['subject'] }}"
          Initial: "{{ trigger.event.data['initial'] }}"
          To: "{{ trigger.event.data['headers'].get('Delivered-To', ['n/a'])[0] }}"
          Return-Path: "{{ trigger.event.data['headers'].get('Return-Path',['n/a'])[0] }}"
          Received-first: "{{ trigger.event.data['headers'].get('Received',['n/a'])[0] }}"
          Received-last: "{{ trigger.event.data['headers'].get('Received',['n/a'])[-1] }}"

You can’t set up template sensors with attributes or triggers in the UI template helper, it must be done in your configuration file(s).

Doh. Thanks. When I was originally getting this set up I had wondered if that was the case, but didn’t see ‘Templates’ anywhere in my configuration file. It’s working now. Thanks a million for your help.

1 Like

I want to drop in and shout out my thanks. I was able to use this entire thread to get it to work. I’m sure I’ll figure out a way to display some sort of color change or icon change based on the state, but I’m in exactly the same boat dealing with Cox Home Security trying to alter home behavior based on that and I was able to get it to work. My only major issue was the fact I had to use gmail instead. I had an old yahoo account I was initially trying to use but the imap integration just wasn’t working the same way for some reason.

By the way, what helped, following your discussion of how the event worked, was using the event listener listening for the imap_content event to see what data it pulled.

your imap integration setup screen isn’t what I see though. Maybe its because I’m using a docker container installation? All I see is folder and search options.

hello, i’m novice and i need help.
i have copy in config.yaml this configuration, i create label on gmail and modify imap on HA with folder Alarm ecc

on IMAP the counter email is correct
but status of zTest IMAP Folder Content 2 is always unknown

sorry for my english (im italian)

i have an alarm non HA compatible and i need to create a event when arrive email from alarm (on gmail)

The post marked as Solution has a lot of “moving parts”. So the first thing to do is to make sure you have all the parts in place:

  • Alarm Folder in Gmail
  • Filters to move the desired emails to the folder
  • 3 IMAP integration sensors each with a specific search criteria:
    • UnDeleted X-GM-RAW “disarm”
    • UnDeleted X-GM-RAW “arm -disarm -part”
    • UnDeleted X-GM-RAW “arm part -disarm”

If all of that is in order, the next step is to check that the events are being created. You can monitor events using the Events tool. Start the listener for the imap_content event…

Listen to Events

then cycle your alarm through its different states, making sure each one registers an event.

Grazie per l’aiuto / Thanks for Help
now is ok i have create 3 integration. ( i have lost this step)
i have other questione:

if i have 4 person in home
arrive the same email : alarm on and allarm off …with the name
i must create one integration for any person?

total 12 integration?

I don’t think that you need to do that… you should be able to extract the data using templates.

We can probably advise you on the best approach if you provide a few examples of the emails’ body text, explain what information you are trying to extract, and how you want it handled. It could be added as either an attribute or a separate sensor.

i use email
inserimento
disinserimento
sirena

for 3 integration and is perfectly .
but on email is present name of user when we arm or disarm alarm.

INSERITA Nicola 006 (Volumetrico) (18:57:37 07/Ago/24)

i want to read name (Nicola or other) for check user activation or disactivation

if nicola insert alarm at 10.00 who have disarm alarm at 12.00

For ease of comparison in the History tool, I think it would be best to set it as a separate sensor instead of an attribute. There are many ways to search the email’s text for the names, the simplest way is listing the names (separated only by the | symbol) in a regex_findall() filter as follows:

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: Disinserimento
        event_data:
          folder: Alarm
          search: UnDeleted X-GM-RAW "disinserimento"
          initial: true
      - platform: event
        event_type: "imap_content"
        id: Inserimento
        event_data:
          folder: Alarm
          search: UnDeleted X-GM-RAW "inserimento"
          initial: true
      - platform: event
        id: Sirena
        event_type: "imap_content"
        event_data:
          folder: Alarm
          search: UnDeleted X-GM-RAW "sirena"
          initial: true
    sensor:
      - name: Security Status
        state: |-
          {% set current = this.state | default("Errore")%}
          {{ trigger.id if 
          trigger.event.data['date'] > now() - timedelta(minutes=1) 
          else current}}
      - name: Security Person
        state: |-
          {{ trigger.event.data['text'] 
          | regex_findall(find="Nicola|Pietro|Anna", ignorecase=true)
          | first | default("Errore") }}

Make sure that the IMAP integrations you set up earlier have the option enabled to include the body text as part of the event data.

first of all…thanks for help

i have insert in config with correct person name but is not resolve.
the result is always error…

image

Did you enable the “Body text” option in the IMAP integrations?

yes
image

There is a limit of the size of the event, so it’s best to not include the message headers if you don’t need them.

You’re going to need to look at the actual value held by the text variable. You can get it directly by using the Events tool or add a sensor to your configuration that gets the value.

      - name: Security Email Body Text
        state: "{{ trigger.event.data['text'] }}"

Once you get the value you can use the Template editor tool to figure out if the template works.

hi, i have test now (holiday)
the problem is email text. (allarm send a text but on HA i can’t filtrer)
i resolve with a single trigger inserimento e disinserimento for each person.
when person start allarm or stop allarm arrive a email on gmail . gmail filtred the email and HA control singol different folder on gmail.
i need help fo 2 problems:

1 - the email counter check email but i need to now how and when is the last email
2 - there is a way for see on HA entire email Object?

template:

  • trigger:
    • platform: event
      event_type: “imap_content”
      id: disinserimento
      event_data:
      folder: Alarm
      search: UnDeleted X-GM-RAW “disinserimento”
    • platform: event
      event_type: “imap_content”
      id: inserimento
      event_data:
      folder: Alarm
      search: UnDeleted X-GM-RAW “inserimento”
    • platform: event
      id: sirena
      event_type: “imap_content”
      event_data:
      folder: Alarm
      search: UnDeleted X-GM-RAW “c/o Abitazione:Allarme”
    • platform: event
      id: Alice ins
      event_type: “imap_content”
      event_data:
      folder: Alice
      search: UnDeleted X-GM-RAW “c/o Abitazione:inserimento”
    • platform: event
      id: Alice dis
      event_type: “imap_content”
      event_data:
      folder: Alice
      search: UnDeleted X-GM-RAW “c/o Abitazione:disinserimento”
    • platform: event
      id: Vale ins
      event_type: “imap_content”
      event_data:
      folder: Vale
      search: UnDeleted X-GM-RAW “c/o Abitazione:inserimento”
    • platform: event
      id: Vale dis
      event_type: “imap_content”
      event_data:
      folder: Vale
      search: UnDeleted X-GM-RAW “c/o Abitazione:disinserimento”
    • platform: event
      id: Ilaria ins
      event_type: “imap_content”
      event_data:
      folder: Ilaria
      search: UnDeleted X-GM-RAW “c/o Abitazione:inserimento”
    • platform: event
      id: Ilaria dis
      event_type: “imap_content”
      event_data:
      folder: Ilaria
      search: UnDeleted X-GM-RAW “c/o Abitazione:disinserimento”
    • platform: event
      id: Nicola ins
      event_type: “imap_content”
      event_data:
      folder: Nicola
      search: UnDeleted X-GM-RAW “c/o Abitazione:inserimento”
    • platform: event
      id: Nicola dis
      event_type: “imap_content”
      event_data:
      folder: Nicola
      search: UnDeleted X-GM-RAW “c/o Abitazione:disinserimento”
      sensor:
    • name: zTest IMAP Folder Content 2
      state: |-
      {% set current = this.state | default(“Error”)%}
      {{ trigger.id if
      trigger.event.data[‘date’] > now() - timedelta(minutes=1)
      else current}}

- name: zTest IMAP Folder Content 1

state: |-

{{ trigger.event.data[‘object’]

| regex_findall(find=“inserimento”, ignorecase=true)

#    | first | default("Errore") }}
# - name: Security Person
#   state: |-
    #   {{ trigger.event.data['text'] 
    #   | regex_findall(find="Nicola|Alice|Vale|Ilaria", ignorecase=true)
    #   | first | default("Errore") }}

sorry for caos

image

I suggest you figure out how to fix this. Some previous threads [1[2][3] using this technique have needed to have the body text decoded from base64.

AFAIK, the full contents of the event are not stored anywhere.

You can view the data from the imap_content event live, as they are received, using the “Listen for Events” function of the Events tool.

There is also an action imap.fetch which can be used to get the text, subject, sender contents of a message. However you must have extracted the uid from the event in order to use the action.