Syntax for multiple search criteria for IMAP search

I’m trying to create an IMAP sensor which detects when an email is received in the last 5 minutes, and the subject line contains one of three fixed texts. I need to surround the fixed texts with double quotes,but am having problems using the necessary escape characters, so am getting syntax errors. Could anyone advice on the correct syntax that I should be using?

  - platform: imap
    name: parking_emails
    server: imap.gmail.com
    port: 993
    username: [email protected]
    password: myapipassword
    senders:
      - [email protected]
      - [email protected]
    search:
    'X-GM-RAW "newer_than:5m \"You have a new booking\" OR \"Booking received\" OR \"JustPark booking\""'

The IMAP integration was overhauled a while ago and YAML configuration was removed in 2023.4

I couldn’t get it working with the Raw search either but, as described in the current IMAP docs, you can use the imap_content event trigger for template sensors or binary sensors:

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: [email protected]
    binary_sensor:
      - name: Parking Booking
        auto_off: "00:05:00"
        state: >
          {% set current = this.state|default('off', 1) %}
          {% if trigger.event.data['subject'] is match
          "You have a new booking|Booking received|JustPark booking"
          %}
            true
          {% else %}
            {{ current }}
          {% endif %}

Hi. The imap integration only work for gmail accounts? I have a yahoo account I want to use, but I cannot get pass the initial setup.

I’ve never used a yahoo acct, but AFAIK it should work.

Does it work with the default search criteria i.e. UnSeen UnDeleted?

If the defaults don’t work, try spelling the folder name in all caps… IIRC some IMAP servers required that.

You may also need to use an app password instead of your normal user password.

no. nothing I have tried works. low caps, proper caps, default settings. the error message is random. It tells me that my user and password is invalid.
I had it working initial using the imap_email_content, but it has stopped working for quite some time now.

Many thanks for this. I’ve replicated this exactly as per your code, but added additional senders (as per below) and sent an email which matches the criteria. The status of the binary sensor did not change from unknown.

I can see from the HA log that the trigger was initialised when I restarted HA, but there’s no other reference to it in the log.

Any ideas for how to diagnose/debug this?

template:

  - trigger:
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: 
            - [email protected]
            - [email protected]
            - [email protected]
            - [email protected]
        id: "custom_event"
    binary_sensor:
      - name: Parking Booking
        auto_off: "00:05:00"
        state: >
          {% set current = this.state|default('off', 1) %}
          {% if trigger.event.data['subject'] is match
          "You have a new booking|Booking received|JustPark booking|Booking request for Onslow Road"
          %}
            true
          {% else %}
            {{ current }}
          {% endif %}

You can’t use a list under the sender event data like that. Due to how the event data works, you have essentially told it to only listen for emails that are from all 4 senders at once, which is impossible.

Option 1: Multiple triggers

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: "[email protected]"
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: "[email protected]"
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: "[email protected]"
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: "[email protected]"
....

Option 2: Filter with Templates

...
 - trigger:
      - platform: event
        event_type: "imap_content"
    binary_sensor:
      - name: Parking Booking
        auto_off: "00:05:00"
        state: > 
          {% set sender_list = ['[email protected]', '[email protected]', 
          '[email protected]', '[email protected]'] %}
          {% set current = this.state|default('off', 1) %}
          {% if trigger.event.data['sender'] in sender_list and 
          trigger.event.data['subject'] is match
          "You have a new booking|Booking received|JustPark booking|Booking request for Onslow Road"
          %}
            on
          {% else %}
            {{ current }}
          {% endif %}

I just tried the template with just a single sender (my own Yahoo account, for testing purposes). The binary sensor did get set to “on” when the email arrived. So I just need to work out how/if it’s possible to specify more than one sender email address?

That’s great - I’ll try the multiple triggers option.
Many thanks!

Yay!! I figured it and it was stated in the docs.

Charset: US-ASCII