Trying to configure IMAP content sensor but not sure if done right

So i setup the imap integration (not the imap email content integration) (what’s the difference?)

It looks good and is counting email whenever it comes in. I have not filled in anything else in the

IMAP Search
or
Template to create custom event data

Fields.

I also setup this :


# Tesco order delay
      
  - trigger:
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: "[email protected]"
        id: "custom_event"
      - platform: template
        id: timeout
        value_template: >
          {{ now() - states.sensor.tesco.last_changed >= timedelta(minutes=5) }}
    sensor:
      - name: Tesco
        state: > 
          {% if trigger.id == 'custom_event' and 
          trigger.event.data['text'] | lower is search('late|delays') %}
            delay
          {% elif trigger.id == 'timeout' %}
            no_delay
          {% endif %}

Would this work?

I’m expecting to get a custom_event as per the manual on IMAP - Home Assistant but it also keeps mentioning that advanced mode needs to be enabled (user settings) but then proceeds to not list anywhere on the page where user settings can be configured, unless i am seriously overlooking it.

Thank you for your help.

Advanced mode can be enabled in the User Profile menu.

Ah that user settings! Yes i have that enabled.

Other then that it should work i think ? I’m unsure wether to use:

sender: "[email protected]"

or

search UnSeen UnDeleted 

Format.

I need to be able to specify a list of email addresses or use a @domain in one sensor. I tried both variants and without quotes but it’s not triggering the sensor (i created one coming from my own domain to test). IMAP sensor shows a count of email addresses so that’s working.

There are two ways to specify a list of senders within an trigger-based IMAP content sensor:

I’m testing it with just the sender option for one email address, but the sensor doesn’t seem to change state. Not sure what’s going on. The imap integration/sensor does change amount of emails.

Since the content sensor is dependent on the configuration of the IMAP sensor, it’s hard to diagnose issues without seeing both.

I have nothing added to the configuration in the IMAP integration, just the email address and the state is the amount of emails i have.


That’s a lot of emails… :laughing:

Lol yes i haven’t reached inbox zero on that account yet. To much spam and i haven’t really bothered trying to get it down :slight_smile:

The issue might be the lack of a default in your “reset” trigger… but I would expect there to be errors in the logs if that was causing the sensor setup to fail.

      - platform: template
        id: timeout
        value_template: >
          {% set last = states.sensor.tesco.last_changed | default( 0 | as_datetime) %}
          {{ now() - last >= timedelta(minutes=5) }}

If you don’t absolutely need to have delay/no_delay as the output, I would use a binary sensor with a 5 minute auto_off instead.

# Tesco order delay
      
  - trigger:
      - platform: event
        event_type: "imap_content"
    binary_sensor:
      - name: Tesco
        auto_off: "00:05:00"
        state: |
          {% set email_test = trigger.event.data['sender'] is search('@example_domain') and 
          trigger.event.data['text'] | lower is search('late|delays') %} 
          {{ 'on' if email_test else 'off' }}

Yes, this binary sensor seems to work better. I will use that instead, it also covers the case of matching on domain name only nicely.

I’m assuming it will still only trigger on new emails (UnSeen Undeleted) ?

Many thanks!

UnSeen Undeleted is not the same as “new”… You have 40K UnSeen Undeleted emails. If you delete or read the newer emails but the most recent “old” UnSeen Undeleted matches the template criteria the sensor will turn on again. You could add another condition to the state template to filter by the datetime the email was sent…

...
state: |
  {% set email_test = trigger.event.data['sender'] is search('@example_domain') and 
  trigger.event.data['text'] | lower is search('late|delays') and
  trigger.event.data['date'] >= now() - timedelta(minutes=5) %} 
  {{ 'on' if email_test else 'off' }}

Cool, i will use that. Many thanks.

One final question if you don’t mind… i also have an email from dominos that is getting sent in base64. Do i need to apply the base64 conversion to all fields in the event or is it sufficient to just apply it to the body of the email. I’m thinking only the body as when i view it raw only the content of the email is base64.

e.g.

         {% set email_test = trigger.event.data['sender'] is search('@dominos.co.uk') and 
         trigger.event.data['text'] | base64_decode | lower is search('sorting') and
         trigger.event.data['date'] >= now() - timedelta(minutes=5) %} 
         {{ 'on' if email_test else 'off' }}  

That’s not an issue I’ve ever dealt with personally, but I’m pretty sure you should only have to decode the body/text not the other properties.

1 Like