IMAP integration - How do I check for a specific sender and body text?

Hi,

I’m new to HA and wanting to dig deeper but I’m struggling with the very basics and my Google searching isn’t coming up with any answers so I know I’m asking the wrong questions.

I’ve added the IMAP integration and have this connected to an email account and working on my dashboard and it displays the number of unread emails.

I would like it to turn a smart plug on when an email from a specific sender with specific words in the body text is received and then later turn off the smart plug when another email is received with different words in the body.

Can someone please point me in the direction of how to acheive this?

I know the integration is: IMAP and the Entity ID is: sensor.imap_my_email_account so I’ve got a sensor. I’ve edited my config.yaml to point towards a sensors directory and then created a sensor.yaml file and tried all sorts of things in here but keep on getting many errors. But I’m assuming that I’ve already got a sensor, the IMAP one so I shouldn’t be trying to create a new one and if so, does it have its own yaml file?

I’ve read the docs on the HA site, looked through posts but am still struggling to get my head round the basics and what I need to do next.

many thanks and sorry for being so dim.

Not dim. Just have to understand how HA works.

First understand ha is a big state engine when states happen you respond. So your best bet is how do I get what I want triggered (by a state) you want a specific mail. Ok imap. (good choice) they imap sensor works by denotiythe number of unread emails. But it ALSO CAN be configured to have a sensor that’s a match for a specific search pattern. (ths will likely be your ticket)

So you can configure a sensor to search for the specific pattern and count it. The imap sensor is just a simple count of items matching search. If you have it configured to search for a specific pattern then now if that sensor is >1, you have a trigger. Do the thing… Then move the email to another folder or delete it or do something to remove it and the trigger is zero again.

Two different emails for two different actions. For now do two automations. Later when you get better you can explore combining them if you want.

See configure imap searches here:

2 Likes

In his post above, @NathanCu proposes setting up a content sensor. If you plan on using emails as triggers for multiple different automations, that is probably the way to go. But if you are only employing this method in a limited scope, additional sensors are not actually necessary.


Just FYI, the way IMAP (especially IMAP content) works was updated some time within the last year, so older posts cannot be relied upon to be accurate.

Your sensor.yaml file is likely set up with sensor as it’s top-level key. If you are trying to set up one of the trigger-based Template sensors (shown under “Using Events”) in a file like that, it will throw errors. Trigger-based template sensors must be set up with template as their top-level configuration key.

Whether or not it needs to be in it’s own file, just in a different file, a different folder, etc is entirely dependent on how you set up inclusion in configuration.yaml… we can’t advise on that without you telling us what you have done. I advise anyone who is splitting their config to take a look at Packages. I find it easier, more flexible, and less annoying than managing merging and inclusion “manually”.

This can be done within an automation by using an Event trigger that listens for the imap_content event. These triggers can be made more specific by including event data like “sender”, “subject”, etc. If you plan on setting up sensors for more than one email account, make sure to include the username event data in your trigger where appropriate. Also, the IMAP search function that drives the sensor and event will always return matching emails, so you will likely want to include initial: true to avoid triggering your light based on old emails when you erase newer ones.

The values for event data need to be exact matches, it is not a partial search function. If you will be using exact subjects or body texts, you can include them. Otherwise, searching the subject or body text can be handled through templates in the condition or action blocks of the automation.

trigger:
  - platform: event
    event_type: "imap_content"
    event_data:
      username: [email protected]
      sender: [email protected]
      initial: true

If you need help with how to handle the conditions or actions of the automation you will need to tell us what you plan on using for the “specific words in the body text”.

2 Likes

Hi,

thanks for taking the time to reply and in such details and from me. Apologies in the delay in replying.

I’ve since ditched my install of HA as it was running on my Synology NAS (older version with no docker and couldn’t install a VPN, and have moved it to my Pi with an M.2 drive.

This caused issues because as I kept on getting I/O errors which were due to the UPS Hat not being able to provide enough power for the Pi & drive so I’ve removed that and have now got a stable platform.

Now turn my attention back to this issue. I’ll give your replies a good read. Do more research and when I have more questions, come back to you if you don’t mind.

Hi,

Thank you for your reply.

I’ve had a bit of a play and have my automation looking for three different phrases within an email (from a specific sender, to a specific recipient and with a specific subject line) and changing the message I play on one of my Alexa devices using TTS by having a template in the Action.

This makes me very (seriously I mean very) happy and I’m just starting to twig how powerful all of this is.

Playing a message on the Alexa is not the aim of this and I want to change the status of switch to either on or off.

I can interrogate the state of the switch by using:

{{ states(‘switch.repeater’)}}

and this gives me either on or off.

I know how to turn the switch on in an action using a call service:

action:
  - service: switch.turn_on
    data: {}
    target:
      device_id: 70516271105a726258cc1d05fde9b73b

but I’m struggling to combine the template if statements with the service call in the template.

From your previous reply I’m assuming I now need to move the template from the action to the condition block but I can’t work out how to then pass these states to the actions.

This is my current working automation that outputs to Alexa. I have got further than I thought I would and feel quite pleased but this is now two nights of not getting any further (I have learned loads though so it’s been valuable) but I’m not getting closer to my target.

alias: SARCALL Email
description: >-
  Trigger if an email is received from [email protected] to
  [email protected] with the SARCALL subject line and either CALLOUT, 
  STANDBY or STANDOWN in the text
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      username: [email protected]
      sender: [email protected]
      subject: "[TVMRT SARCALL] TWEED_MRT"
      initial: true
condition: []
action:
  - service: notify.alexa_media
    data:
      target: media_player.kitchen_echo
      message: |
        {% if states('sensor.imap_my_imap_sensor_name') > '0' %}    
          {% if 'TWEED_MRT [FT] STANDBY:' in trigger.event.data.text %}
            You have a SARCALL STANDBY
          {% elif 'TWEED_MRT [FT] CALLOUT:' in trigger.event.data.text %}     
            You have a SARCALL CALLOUT
          {% elif 'TWEED_MRT [FT] STANDOWN:' in trigger.event.data.text %}     
            You have a SARCALL STANDOWN
          {% endif %}
        {% endif %}
mode: single

You only need to add condition templates if your trigger isn’t specific enough to reliably allow through only the desired emails. Though I would move the check of the IMAP sensor to the condition block, since your example doesn’t have an else for the outer if.

There are a bunch of ways to handle the service call with templating. Here is one basic example:

alias: SARCALL Email to Switch service
description: >-
  Trigger if an email is received from [email protected] to
  [email protected] with the SARCALL subject line and either CALLOUT, 
  STANDBY or STANDOWN in the text
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      username: [email protected]
      sender: [email protected]
      subject: "[TVMRT SARCALL] TWEED_MRT"
      initial: true
condition:
  - condition: numeric_state
    entity_id: sensor.imap_my_imap_sensor_name
    above: 0
action:
  - variables:
      target_state: |
        {%- if 'TWEED_MRT [FT] STANDBY:' in trigger.event.data.text %}
        on
        {%- elif 'TWEED_MRT [FT] CALLOUT:' in trigger.event.data.text %}     
        on
        {%- elif 'TWEED_MRT [FT] STANDOWN:' in trigger.event.data.text %}     
        off
        {%- endif %}
  - service: switch.turn_{{ target_state }}
    data: {}
    target:
      device_id: 70516271105a726258cc1d05fde9b73b
1 Like