Can't get the new IMAP template to work to check my email

Go to Settings > Devices & Service > Integrations.
Click the “Add Integration” button, type “IMAP” into the search bar, and select it.
Input all the information for your IMAP server.
In the “IMAP Search” field paste the following:

UnSeen UnDeleted FROM “[email protected]” SUBJECT “atividade”

Click “Submit”.


This will create a sensor called something like sensor.imap_andre_outlook_com_2.

That sensor’s state is “0” when there are no emails from “[email protected]” with “atividade” in the subject. When an email that meets the search criteria is received, the state of the sensor will be a count of how many emails meet the criteria.

I see your point … tried that and I´m receiving the following error with this:

image

Maybe it´s a syntax issue?

IT WORKED!

Just changed to: UnSeen UnDeleted From [email protected]

wow that was something … thank you SO very much @Didgeridrew - do you have a buy me a beer account or something?

thanks again!

The search should work as I posted it… I use that exact syntax for one of my sensors. :man_shrugging:

can you add multiple email address’s ?

hi im still having issue that IMAP intergration can not detect the emails… and sort them in the 3 states i set in my orginal post above… i dunno if its a hotmail issue and you need gmail instead… the fix in the latest HA update 5.4 didnt fix the issue…i got it to work but not really on its own had used another email coming in to detect… and i have no idea why something is blocking i posted before to the github but never a reply … the info doesnt say whats blocking HA

Home Assistant Core
2023-05-28 09:12:45.658 INFO (MainThread) [homeassistant.bootstrap] Setting up stage 2: {'hacs', 'application_credentials', 'input_datetime', 'trace', 'file_upload', 'template', 'speedtestdotnet', 'group', 'automation', 'conversation', 'stream', 'device_automation', 'image_upload', 'onboarding', 'input_boolean', 'energy', 'homeassistant_alerts', 'auth', 'input_select', 'shelly', 'reolink_discovery', 'auto_backup', 'persistent_notification', 'search', 'browser_mod', 'climate', 'blueprint', 'mqtt', 'notify', 'repairs', 'device_tracker', 'remote_homeassistant', 'sun', 'my', 'tts', 'local_calendar', 'assist_pipeline', 'uptime', 'analytics', 'switch', 'config', 'timer', 'counter', 'input_text', 'lovelace', 'scene', 'tag', 'sensor', 'diagnostics', 'media_source', 'schedule', 'person', 'binary_sensor', 'imap', 'default_config', 'dlna_dmr', 'logbook', 'map', 'vlc_telnet', 'zone', 'dlna_dms', 'python_script', 'panel_custom', 'input_number', 'stt', 'cast', 'history', 'script', 'adguard', 'hardware', 'tplink', 'met', 'mobile_app', 'volumio', 'input_button', 'system_health', 'esphome', 'environment_canada'}
2023-05-28 09:12:47.394 INFO (MainThread) [homeassistant.setup] Setting up imap
2023-05-28 09:12:47.394 INFO (MainThread) [homeassistant.setup] Setup of domain imap took 0.0 seconds
2023-05-28 09:12:57.640 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Finished fetching imap data in 0.000 seconds (success: True)
2023-05-28 09:12:57.642 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.imap
2023-05-28 09:13:04.244 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Message processed, sender: [email protected], subject: How to Make an Epoxy Clock
2023-05-28 09:13:04.244 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Manually updated imap data
2023-05-28 09:18:25.084 WARNING (MainThread) [homeassistant.core] Something is blocking Home Assistant from wrapping up the start up phase. We're going to continue anyway. Please report the following info at https://github.com/home-assistant/core/issues: sensor.systemmonitor, application_credentials, sensor.mqtt, hacs, cover.shelly, frontend, file_upload, ping, media_player.browser_mod, system_log, sensor.shelly, notify.smtp, sensor.time_date, button.mqtt, binary_sensor.shelly, sensor.speedtestdotnet, humidifier.mqtt, image_upload, cover, sensor.remote_homeassistant, energy, switch.esphome, auth, dhcp, binary_sensor.esphome, reolink_discovery, webhook, auto_backup, browser_mod, blueprint, mqtt, ssdp, device_tracker, remote_homeassistant, sun, my, binary_sensor.mqtt, light, vacuum, local_calendar, media_player.dlna_dmr, sensor.sun, assist_pipeline, uptime, camera.mqtt, weather.environment_canada, update, api, config, counter, input_text, update.mqtt, scene, tag, diagnostics, sensor, media_source, person, binary_sensor, imap, light.browser_mod, update.shelly, vlc_telnet, lock, weather.met, dlna_dms, light.shelly, panel_custom, scene.mqtt, input_number, network, sensor.mobile_app, sensor.hacs, history, sensor.adguard, adguard, binary_sensor.mobile_app, sensor.tplink, sensor.browser_mod, humidifier, tplink, met, number.mqtt, mobile_app, input_button, system_health, climate.generic_thermostat, text, fan.mqtt, switch.adguard, switch.tplink, environment_canada, input_datetime, binary_sensor.hassio, trace, reolink, template, speedtestdotnet, binary_sensor.command_line, sensor.template, tts.google_translate, switch.mqtt, group, sensor.uptime, automation, select.mqtt, conversation, stream, device_automation, onboarding, input_boolean, media_player, homeassistant_alerts, light.mqtt, select, input_select, button.shelly, shelly, zeroconf, climate.mqtt, scene.homeassistant, fan, siren.mqtt, button, logger, device_tracker.bluetooth_tracker, persistent_notification, search, device_tracker.mqtt, climate, camera.environment_canada, hassio, alarm_control_panel, notify, repairs, tts, usb, homeassistant, lock.mqtt, calendar.local_calendar, switch.shelly, light.tplink, analytics, text.mqtt, binary_sensor.ping, switch, notify.mobile_app, timer, siren, lovelace, schedule, camera, dlna_dmr, map, logbook, default_config, alarm_control_panel.mqtt, bluetooth, sensor.imap, vacuum.mqtt, zone, media_player.cast, calendar, cover.mqtt, python_script, media_player.volumio, sensor.auto_backup, sensor.energy, update.hassio, media_player.vlc_telnet, stt, http, cast, weather, script, binary_sensor.browser_mod, hardware, number, cloud, sensor.environment_canada, recorder, sensor.hassio, volumio, websocket_api, sensor.esphome, esphome, camera.browser_mod, device_tracker.mobile_app
2023-05-28 09:23:43.605 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Message processed, sender: [email protected], subject: Your Epic Games Receipt F2305281318320451
2023-05-28 09:23:43.605 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Manually updated imap data
2023-05-28 09:23:51.286 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Message processed, sender: [email protected], subject: Your package has been received!
2023-05-28 09:23:51.286 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Manually updated imap data
2023-05-28 09:23:52.351 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Manually updated imap data
2023-05-28 09:25:00.911 ERROR (MainThread) [homeassistant.components.imap.coordinator] Error requesting imap data:
2023-05-28 09:26:16.866 DEBUG (MainThread) [homeassistant.components.imap.coordinator] Manually updated imap data
2023-05-28 09:27:10.397 INFO (MainThread) [aioimaplib.aioimaplib] ignored untagged response : b'BYE Connection is closed. 13'
2023-05-28 09:27:19.839 INFO (MainThread) [aioimaplib.aioimaplib] ignored untagged response : b'BYE Connection is closed. 13'
2023-05-28 09:27:23.308 INFO (MainThread) [aioimaplib.aioimaplib] ignored untagged response : b'BYE Connection is closed. 13

To overcome issues with event sizes, you could consider using the new custom template setting. This templated is evaluated before the event is sent, and will also work on large emails. The result of the custom template will be added to the event data.
See: IMAP - Home Assistant

Note that the setting is an advanced setting.

I could not figure out how to use the custom template from the documentation. From what I could tell it did not provide a example you could copy/paste and make it work. It referenced some custom template code but didn’t say where to put it. It just says Custom: True in the yaml.

You decide what the output of your custom template is. The custom template is not limited to the message size. The result of the template will be available in the event data.

Hi,

I have a similar problem, the IMAP integration works fine, it reads the number of emails I have and I can create a sensor with the email data.

My problem comes when I want to create sensors with the information that comes in the body of the message.

The “main” sensor shows the message text, but when I try to create a new one with part of it I get Unavailable.

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "mail_eventid"
    sensor:
      - name: "mail_sensor"
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Message: "{{ trigger.event.data['text'] }}"

image

As you can see, in the sensor attributes I can see the message data.

I do go to templates, and I put this:

{{ ((state_attr('sensor.mail_sensor', 'Message') | regex_findall_index(find='([0-9].*C)', index=0))[:-1]) }}

The correct result appears, 24.7

But if I enter the code for a new sensor I get the message UNAVAILABLE.

What does this mean? Show the actual sensor configuration.

I got it. Thanks

Where are you putting the template and sensor setup?
is it in the configuration.yaml file?

I have followed the imap example, but not getting any new sensors except the imap count of unread emails.
any working examples with specific places to put them would be really helpful.
something simple like the subject in a sensor or text regex to populate another sensor.

The trigger-based template sensor used to get the content of incoming emails can go directly in configuration.yaml or any properly split file that uses template as it’s top-level configuration key.

One easy and flexible method for splitting the config is to use a Packages folder. Here is a screenshot of the basic “subject” sensor.

image

For reference, my Packages folder is split in configuration.yaml as follows:

homeassistant:
  packages: !include_dir_named packages

Hi, thanks for the pointers on the IMAP integration;
just one issue here and couldn’t find an answer so far

Everything works as expected, I can trigger on subject but not on body (“text”)

  - trigger:
      - platform: event
        event_type: "imap_content"        
    binary_sensor:
      - name: gmail_hydro_test
        auto_off: "00:02:00"
        state: >
          {% if trigger.event.data['text'] is match
          "test|Test|Zzz|zzz"
          %}
            true
          {% endif %}        

To troubleshoot I’ve added a sensor to extract all data and the “text” is in the wrong format;
(the body of the message here is simply “test” but here’s the result)

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "custom_event"
    sensor:
      - name: imap_content
        unique_id: 2604a5fa-d31e-4ab1-bced-084b0cfe81a2
        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']['Delivered-To'][0] }}"
          Return_Path: "{{ trigger.event.data['headers']['Return-Path'][0] }}"
          Received-first: "{{ trigger.event.data['headers']['Received'][0] }}"
          Received-last: "{{ trigger.event.data['headers']['Received'][-1] }}"            

image

I’ve tried changing the charset to US-ASCII instead of utf-8 but still doesn’t work

Any idea what could be wrong ?

It’s base64 encoded.

- trigger:
      - platform: event
        event_type: "imap_content"        
    binary_sensor:
      - name: gmail_hydro_test
        auto_off: "00:02:00"
        state: >
          {{ trigger.event.data['text'] | base64_decode  is match("test|Test|Zzz|zzz") }}

FWIW, match() will only match the find expression at the beginning of the string. If the criteria could be anywhere other than the beginning you’ll need to use search().

ok will test and report back;
what’s odd though is the fact that using the integration template does work

image

Every single one of my dozen tests now working … :sunglasses:
Thank you :+1:

Does the base64 text comes from Gmail or the sender ?
surprised this hasn’t surfaced elsewhere on the forum if it is Gmail related…

As I understand it, the actual search is performed by the email provider/server so they must decode it at some point… I don’t know why they don’t forward the decoded version.

The sender.

There have been a couple questions about it in the past month or two.