IMAP Intergration Question order reading mails

Hello,

i have successfully intergrated the imap e-mail integration.
But it seems the order for reading new messages is newest first, right?

The mailbox receives states from sensors. But if the automation reads the mails in the wrong order, the state is wrong. The automation deletes the message after reading, so the mailbox is always empty, but the messages from the sensors are sometimes coming only seconds between.

Is it possible to read oldest mail first?

Best regards,
Joerg

Please post your automation configuration so we can see what you are trying to do.

This is one of many automations with different subjects from the emails

alias: Mail_Bad_Balkon wurde geöffnet
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.imap_content
    attribute: Entry
conditions:
  - condition: state
    entity_id: sensor.imap_content
    state: Bad_Balkon Alarm aktiv
    enabled: true
actions:
  - action: imap.seen
    metadata: {}
    data:
      entry: 01JW4B8ME5FDG7BZCW20VWKVT6
      uid: "{{state_attr('sensor.imap_content','UID') }}"
  - action: imap.delete
    metadata: {}
    data:
      entry: 01JW4B8ME5FDG7BZCW20VWKVT6
      uid: "{{state_attr('sensor.imap_content','UID') }}"
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.boolbadtuer
  - action: notify.joerg_luebkert
    metadata: {}
    data:
      message: Imap Bad Balkon wurde geöffnet
mode: single

configuration:

template:
  - trigger:
      - trigger: event
        event_type: "imap_content"
        id: "custom_event"
    sensor:
      - name: imap_content
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Entry: "{{ trigger.event.data['entry_id'] }}"
          UID: "{{ trigger.event.data['uid'] }}"
          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] }}"

Instead of querying the state of the entity (which may have changed already), use the values available in the trigger variable:

{{ trigger.to_state.attributes.UID }}

Use queued as your mode so things are processed in the order they happen, make sure to set max to a reasonable value for the rate you receive emails… if you are unsure what that would be, there is a max_exceeded configuration variable you can set to get log messages.

You may also want to consider setting up a specific IMAP integration sensor for just these emails instead of using one generic sensor. Mail servers have different policies about how often they update IMAP search results, so a sensor based on a generic search may not post an event for every email if multiple emails are received in short order.

I am new to home assistant, do you mean the setting in the configuration.yaml?

To make a sensor for every type of E-Mail subject which comes in?

Please can you make me an example how to change the config?

It sounds good to me.

Like this?

    sensor:
      - name: imap_content
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Entry: "{{ trigger.event.data['entry_id'] }}"
          UID: "{{ trigger.event.data['uid'] }}"
          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] }}"
      - name: imap_badbalkon
        state: >-
          {% if 'Bad_Balkon Alarm aktiv' in trigger.event.data["subject"] %}
            Bad_Balkon_Offen
          {% elif 'Bad_Balkon Alarm inaktiv' in trigger.event.data["subject"] %}
            Bad_Balkon_Zu
          {% endif %}

ok, i think now i have the solution, thank you for your tips!

configuration.yaml:

      - name: imap_terrassentuer
        state: >-
          {% if 'Terrassentuer Alarm aktiv' in trigger.event.data["subject"] %}
            Terrassentuer_Offen
          {% elif 'Terrassentuer Alarm inaktiv' in trigger.event.data["subject"] %}
            Terrassentuer_Zu
          {% endif %}
        attributes:
          Entry: "{{ trigger.event.data['entry_id'] }}"
          UID: "{{ trigger.event.data['uid'] }}"
          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] }}"

and the automation for that sensor:

alias: Mail_Terrassentuer
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.imap_terrassentuer
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.imap_terrassentuer
            state: Terrassentuer_Offen
        sequence:
          - action: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.boolterrasse
          - action: notify.joerg_luebkert
            metadata: {}
            data:
              message: Terrassentuer wurde geöffnet
      - conditions:
          - condition: state
            entity_id: sensor.imap_terrassentuer
            state: Terrassentuer_Zu
        sequence:
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.boolterrasse
          - action: notify.joerg_luebkert
            metadata: {}
            data:
              message: Terrassentuer wurde geschlossen
  - action: imap.seen
    metadata: {}
    data:
      entry: 01JW4B8ME5FDG7BZCW20VWKVT6
      uid: "{{state_attr('sensor.imap_terrassentuer','UID') }}"
  - action: imap.delete
    metadata: {}
    data:
      entry: 01JW4B8ME5FDG7BZCW20VWKVT6
      uid: "{{state_attr('sensor.imap_terrassentuer','UID') }}"
mode: queued
max: 10

This works great, now i have to change alle the other automations!

Thanks for giving the right ideas

hm, the main problem is not solved.

if i open the door for 5 seconds and close it.
the mails with open door and close door arrives in the correct order in the mailbox,

But the sensor ececutes the newer one (close door) first and after that the older one (open door).
Now my boolean sensor is in the state door open, and also the messages send to telegram are in the wrong order.

i think the imap integration prefers the newer push from the imap server and read the newer mail first.

No, you can set up multiple services in the IMAP integration:

You don’t need to make one for every subject or sender, even making the search criteria a little more specific to it’s end use purpose can make it significantly more reliable.

Since, you are really using any filtering for the template sensor, it would be more efficient to use the event trigger instead of relaying it through the IMAP sensor.
Make sure you fix the templates you are using for uid so they use the trigger variable, you can easily delete the wrong emails the way you have it currently configured.

Yes, the event data is always based on the newest message. Sometimes when you delete a message, there may be a new event for a message that was actually older. Using the value of the initial variable can help prevent those from affecting your sensor.

Just be aware that there are some combinations of cloud-based services and email providers that will never produce reliable and accurate results using the IMAP integration. It might be possible to force them to work if you use the fetch function… but I don’t think it would be worth the effort since all the time data for the end sensor would be inaccurate.

Relaying sensor data from your house, through a cloud service, then to your email, and then back to your house will always be less reliable, less timely, and less accurate than using sensors that connect directly to HA locally.

hm, i tried now everything, but the integration imap doesn’t work as i expect.
it always takes the newest mail and not the oldest mail, also if i disable push mail. That doesn’t makes sense if you want to track the state from a device, which only can send emails.

Is there any other mail intergration, which supports polling a mailbox and fetch mails in the order “oldest first” ?