IMAP Migration - Now Extremely Delayed?

Hi All,

Hoping someone can help. I used to use the IMAP_Email_Content integration to control a sensor for my alarm panel, showing if it was armed or not based on an email. It worked perfectly.

A few releases ago i was migrated to the ‘IMAP’ integration instead as email content was being depreciated. It did work (after a lot of trial and error) but has recently become extremely delayed at updating the sensor. Sometimes 15 mins, sometimes up to an hour delayed.

I have verified that the IMAP inbox is receiving the email instantly, so i know the issue is within HA. I’m not quite sure where to start so here is my config for the 2 sensors (Alarm and UPS).

Thanks in advance!

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "alarm_status_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'] }}"
          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] }}"    

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "network_cupboard_ups_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'] }}"
          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] }}"   
template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "alarm_status_event"
        event_data:
          sender: "[email protected]"
    sensor:
      - name: alarm_status_new
        state: >-
          {% if 'Alarm Set' in trigger.event.data["subject"] %}
            Armed
          {% elif 'Alarm Un-Set' in trigger.event.data["subject"] %}
            Disarmed
          {% elif 'Alarm Stopped' in trigger.event.data["subject"] %}
            AlarmStopped
          {% elif 'Alarm Sounding' in trigger.event.data["subject"] %}
            AlarmSounding
          {% elif 'Mains Power Lost' in trigger.event.data["subject"] %}
            MainsLost
          {% elif 'Mains Power Restored' in trigger.event.data["subject"] %}
            MainsRestored
          {% elif 'Device Missing' in trigger.event.data["subject"] %}
            Offline
          {% endif %}
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "network_cupboard_ups_event"
        event_data:
          sender: "[email protected]"
    sensor:
      - name: network_cupboard_ups_status_new
        state: >-
          {% if 'battery mode' in trigger.event.data["subject"] %}
            Battery
          {% elif 'AC mode' in trigger.event.data["subject"] %}
            Mains
          {% elif 'low battery' in trigger.event.data["subject"] %}
            NAS_Shutdown
          {% elif 'connected' in trigger.event.data["subject"] %}
            Mains
          {% endif %}

Why do you have two sensors with the same name and content?

Is this your actual configuration? IIRC, using a top-level key multiple times in series in a single file (as you have with template) will cause the first set to be overwritten.

Hi,

Honestly I have no idea, I struggled a lot to get the 2 sensors working after migrating from the IMAP_email_content integration and after a LOT of trial and error, this was the only config that worked… until it didn’t…

This is an actual copy and paste from my config, yes

Apologies for the delayed reply, I’d assumed no one had come to me but apparently I don’t have email notifications!

Keep in mind that IMAP is cloud push, so to some degree you’re at the mercy of the cloud and the email provider when it comes to update speed.

The first thing to do is remove the second template: key. I would also remove the entire second sensor named imap_content, since it’s providing the exact same output as the first one. Then reload your template entities or restart HA.

Are the alarm status and UPS event from the same sender? It’s hard to tell with all the X’s… :slight_smile:

Do you receive other emails from this/these senders that will need to be filtered out via templating? If yes, what data is included that we can use to differentiate between noise and signal?

The senders are different, but i have done your 2 suggestions and it seems to be working instantly! Thank you so much

1 Like

Hi,

As per my last reply, your suggestion immediately fixed the issue, but now again 2 weeks or so later, it’s delayed again and i cannot figure out why!

I have checked that the emails are coming into the inbox exactly when they should, but home assistant just isn’t seeing it and updating the sensor.

Do you have any further ideas?

Thanks!

Have you tried disabling IMAP-Push? The default is to have it enabled to keep traffic at a minimum, but some providers require that IMAP clients ping them more often than HA does when push is enabled. If they don’t, they disable the push from their side until they hear from the client again.

Interesting, how would I disable that please? Is it a setting in UI or a line to add to my config?

It’s in the IMAP integration:

Cheers, it’s now working incredibly quickly again. I’ll keep an eye on it over the coming weeks… thanks again

So far so good. Thank you very much, hopefully it stays this way!