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 %}
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.
...
- 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?