IMAP trigger on To address

I want to set up an IMAP trigger on the sent to address rather than on the sender’s address of an email. More specifically, I am sending myself an email with the + suffix so I can differentiate specific emails. In configuration.yaml I have the folllowing but it never triggers although it triggers without the +suffix (but of course that then gets overwhelmed by all my other emails). Is it something that could work or am I trying the impossible?

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "event1"
        event_data:
          sender: "[email protected]"
      - platform: event
        event_type: "imap_content"
        id: "test_imap_event"
        event_data:
          To: "[email protected]"
    sensor:
      - name: received_email
        state: >-
          {% if 'text I want' in trigger.event.data["subject"] %}
            Free
          {% endif %}
        attributes:
          subject: >-
            {% if 'text I want' in trigger.event.data["subject"] %}
              {{ trigger.event.data["subject"] }}
            {% endif %}

Perhaps watch the event when it happens to see if the + symbol is escaped somehow?

To is not a valid variable for event data, and username won’t show the +suffix. You’ll need to filter in a template and alter your state and attribute templates to cover for trigger events that don’t meet all your criteria.

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "event1"
        event_data:
          sender: "[email protected]"
      - platform: event
        event_type: "imap_content"
        id: "test_imap_event"
        event_data:
          username: "[email protected]"
    action:
      - variables:
          to_suffix: "{{ '[email protected]' | lower in trigger.event.data['headers'].get('Delivered-To') | map('lower') | list }}"
          allowed: "{{ to_suffix or (trigger.id == 'event1') }}"
    sensor:
      - name: received_email
        state: >-
          {% if allowed and 'text I want' in trigger.event.data["subject"] %}
            Free
          {% else %} 
            Not Free
          {% endif %}
        attributes:
          subject: >-
            {% if allowed and 'text I want' in trigger.event.data["subject"] %}
              {{ trigger.event.data["subject"] }}
            {% else %}
              {{ this.attributes.subject | default('No subject', 1) }}
            {% endif %}

EDIT: Added missing variables configuration key.

1 Like

Thanks. Where did you find that out? I’ve never seen the action: statement.

As it happens I’ve found an alternative approach as I can do the myself+suffix approach for the sender (I discovered gmail allows you to create multiple such senders).

It was added in the past 8 month (I don’t remember which release exactly) to enable creating sensors that incorporate response data from service calls like weather.get_forecasts and calendar.get_events.

1 Like
2 Likes