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

There’s nothing else to “send” you. IMAP sensors are now set up in the UI from the Integrations page. Set up a new sensor, use the search criteria I posted above.

1 Like

so the 2nd sensor should look like this?

template:

  - trigger:

      - platform: event

        event_type: "imap_content"

        event_data:

          search: UnSeen UnDeleted FROM "[email protected]"

    sensor:

      - name: Garmin_W

        state: >-

          {% if 'atividade' in trigger.event.data["subject"] %}

            Em Atividade

          {% else %}

            Descansando

          {% endif %}

I´m not really understanding the logic, the automation I´m looking for depends on the sensor changing it´s status. Having a 2nd sensor I´m not sure how to work it out.

No.

Post the automation… There is likely no need to setup a Content sensor.

You can use the Event trigger in your automation.

trigger:
  - platform: event
    event_type: "imap_content"
    event_data:
      search: UnSeen UnDeleted FROM "[email protected]" SUBJECT "atividade"
condition: []
action:

OR, use a State/Numeric state trigger based on the IMAP sensor. The selection depends on the automation and how you use the emails.

If you think you need a sensor, you could setup a template binary sensor based on the IMAP sensor.

template:
  - binary_sensor:
      - name: Atividade do Garmin
        state: '{{ states("sensor.imap_garmin_w")|int > 0 }}'
alias: Test Garmin
description: ""
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      search: UnSeen UnDeleted FROM "[email protected]" SUBJECT "atividade"
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.garmin_w
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.garmin_w
mode: single

If there was a way for the integration to constantly check if the email is there, that would be great. The problem is that there´s no 2nd email stating the activity is done, so I´m trying to figure out a way to identify that. Before version 5.03 the documentation example was working for me:

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "custom_event"
        event_data:
          sender: "[email protected]"
    sensor:
      - name: house_electricity
        state: >-
          {% if 'UPS On Battery' in trigger.event.data["subject"] %}
            power_out
          {% elif 'Power Restored' in trigger.event.data["subject"] %}
            power_on
          {% endif %}

The IMAP integration is Cloud Push, meaning Home Assistant should be notified by your IMAP server as soon as a new state is available. In my tests, the sensor usually updates in under 15 seconds.


The IMAP sensor must be configured through the UI. To do so, go to Settings > Devices & Service > Integrations. Then click the “Add Integration” button and type “IMAP” into the search bar. 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”.


If you set up a second sensor as I have instructed, it will create a sensor called something like sensor.imap_your_email_domain_2. That sensor’s state is “0” when you have no unread emails from “[email protected]” with “atividade” in the subject. If you have an email that meets the search criteria, the state of the sensor will be a count of how many emails meet the criteria.

1 Like

I really appreciate your patience with me :slight_smile:

The second sensor should look like this? I don´t really need any additional data in the email like the subject for instance, it´s a simple email from [email protected] in my mailbox would do.

Does it look correct?

template:

  - trigger:

      - platform: event

        event_type: "imap_content"

        event_data:

          search: UnSeen UnDeleted FROM "[email protected]"

    sensor:

      - name: Garmin_W

I don’t know which sensor you are talking about, but what you have posted is not correct. Please clarify if you are talking about the IMAP sensor or the Content sensor so I can address your question.

You´ve mentioned a second sensor, but can we start this over? :slight_smile:

I need a sensor that tells me when an email from [email protected] is found in my outlook.com inbox. When there´s no email from that sender (either no existing or recently deleted email), it should be considered off or whatever other status you want to call. I can work on an automation after the sensor is working (on / off - whatever we want to call them).

Like I was sayijng, it was working before 5.03 if I´m not mistaken with the original documentation in the IMAP integration but it stopped working for some reason.

thank you again for your patience

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.