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

I changed all the quotes in the search: to quotes with straight lines, restarted HA, sent a new manual email to this inbox with the body just reading “Disarm disarm” - added a label immediately so it was in the alarm folder but still an error for Alarm_Status…

I’m at a loss… I went ahead and set up IMAP integrations and a sensor as follows (as well as a Gmail filter to automatically apply the “Alarm” label) and it’s working as expected:

image

IMAP sample config

Content sensor configuration
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: Disarm
        event_data:
          folder: Alarm
          search: UnDeleted X-GM-RAW "disarm"
          initial: true
      - platform: event
        event_type: "imap_content"
        id: Fully Armed
        event_data:
          folder: Alarm
          search: UnDeleted X-GM-RAW "arm -disarm -part"
          initial: true
      - platform: event
        id: Part Armed
        event_type: "imap_content"
        event_data:
          folder: Alarm
          search: UnDeleted X-GM-RAW "arm part -disarm"
          initial: true
    sensor:
      - name: Alarm Status
        state: |-
          {% set current = this.state | default("Error")%}
          {{ trigger.id if 
          trigger.event.data['date'] > now() - timedelta(minutes=1) 
          else current}}
1 Like

so i decided to delete everything and start again…and it now works!!!

Thanks so much @Didgeridrew for your help here, you’ve been absolutely brilliant!

1 Like

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