Imap_email_content event trigger not working for me

Hi,

I’m trying to setup an IMAP event trigger. When a new email from a specific recipient with a number X in the body comes in, activate my water heater for X minutes. The email body has only the number in it.

I setup the following in configuration.yaml:

sensor:
  - platform: imap_email_content
    server: imap.gmail.com
    port: 993
    name: "email"
    username: XXX
    password: xxx
    senders:
      - [email protected]

template:
- sensor:
      - name: "Water Heater Manual Time"
        unit_of_measurement: "min"
        state: >
          {% set heater_time = state_attr('sensor.email','body') | int %}
          {{ heater_time }}

I see the email come in:

from: [email protected]
subject: Water Heater On
date: Mon, 7 Mar 2022 01:08:54 -0800
body: 4

and I successfully see the sensor state update with the correct values for each email being received./

When I setup my automation though, I can’t seem to be able to get the event to trigger when a number X larger than 0 comes in. the event never gets triggered - even though under developer tools I can see the value for sensor.water_heater_manual_time gets updated correctly. This is my automation:

alias: Set water heater on - email time
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.water_heater_manual_time
    above: '0'
condition: []
action:
  - type: turn_on
    device_id: XXX (I deleted this for this post)
    entity_id: switch.water_heater
    domain: switch
  - delay: >-
      {{ '00:{:02}:00'.format(states('sensor.water_heater_manual_time') | int)
      }}
  - type: turn_off
    device_id: XXX (I deleted this for this post)
    entity_id: switch.water_heater
    domain: switch
mode: single

Any help or thoughts would be much appreciated!

Make sure the sensor goes to zero between setting emails. If the sensor value changes from 4 to 5 without passing through a 0 state, the Numeric State trigger will never reset to a “listening” state.

In gmail you should be able to set an automation to delete the email after a couple minutes or you could automate a follow up email to reset the sensor to 0.

Hi, thanks for the reply!
I actually tried that, it didnt help. The problem seems to be that it never gets triggered to begin with (I followed the automation trace and it looks like it never gets triggered even when i start from zero)

@idanl3 I recreated your sensor and automation and it is working fine for me. The only two difference are:

  1. I have my IMAP content sensor set up with a value template to switch the state to “off” when the newest email is more than 5 minutes old
IMAP Content sensor config
  - platform: imap_email_content
    name: X IMAP
    server: imap.gmail.com
    port: 993
    username: !secret x_google
    password: !secret x_google_pw
    folder: Inbox
    senders:
      - [email protected]
      - [email protected]
      - [email protected]
      - [email protected]
    value_template: >-
      {% if now() - (strptime(date, "%a, %d %b %Y %H:%M:%S %z")) < timedelta(minutes=5) %}
      on
      {% elif now() - (strptime(date, "%a, %d %b %Y %H:%M:%S %z")) >= timedelta(minutes=5)%}
      off
      {% else %}
      unavailable
      {% endif %}
  1. I used Call Service actions with switch.turn_on and switch.turn_off instead of Device actions.
4 Likes

Worked! thanks a million

1 Like

Hello. Im new with HA, and trying to make this solution work. I have integrated IMAP service (IMAP - Home Assistant) and added my gmail account. I cant seem to find the way to integrate the script above to trigger my smart socket to turn on once I receive an email. Any help will be appreciated.

Thank you.

We can’t help if you don’t tell us what is working and what isn’t working…

Is the IMAP sensor working?

Are you triggering directly off the event or using a trigger-based template sensor to pass the email data?

Post the content sensor and/or automation configuration.

IMAP graph shows 0 new, but on the top it shows all emails in the inbox.

and I tried to create automation, but got his error:
Message malformed: must contain at least one of below, above.

Please post the YAML configuration of the automation, screen shots of the UI editor collapse all the information. You can access it by clicking the menu button at the top right in the automation editor and choose “Edit in YAML”

Copy everything from the YAML editor then copy/paste it into a post. Be sure to format it properly by adding 3 back-ticks (```) on the lines above and below the pasted configuration


From the error message it looks like you have attempted to use a Numeric State trigger or condition without specifying an above or below value.

Currently, it shows like this, after I select IMAP under When


type: value
platform: device
device_id: 6a442f8d79bcb7003f2999e6a196b3bc
entity_id: 9d8290f7add1f4bf1d77c4fcd8d789f1
domain: sensor

That history graph doesn’t look right for the normal ways IMAP sensors work. I would expect the graph value to match the state value. I think your sensor is either misconfigured or you may be running up against a reporting limited like that mentioned in the IMAP: Troubleshooting.