Newby issues for using IMAP

I’m trying to use the IMAP integration to trigger automations based on the email subject.

I’m using a slightly tweaked example from the IMAP page. I added the following to the end of configuration.yaml:

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "custom_event"
        event_data:
          sender: "[email protected]"
          initial: true
    sensor:
      - name: "LightTriggerEmail"
        state: >-
          {% if 'LightOn' in trigger.event.data["subject"] %}
            LightOn
          {% elif 'LightOff' in trigger.event.data["subject"] %}
            LightOff
          {% endif %}

This is just a toy to get things going.

What I’m missing is what’s the next step to use the newly created sensor to actually control anything. When I tried to create automation, I didn’t find the LightTriggerEmail sensor as something that I can condition.
How can I actually use it?

Newby alarm - this is the first template I’m copy/pasting.

If you don’t see the sensor entity in the Entities menu then you should restart Home Assistant.

Once the sensor entity is available, you will be able to use it in State triggers and conditions.

1 Like

Thank you. I rebooted my machine, and it indeed showed up.
Before, reloading all the YAML configurations did not do.

First question, does that mean that I must reboot HA for any change that I make in configuration.yaml?

Second question - seeing the sensor now, it is always “unknown”, so I assume that my code did failed somehow. I also added a default “else” to the if, and the state also didn’t get this value. So I’m afraid that the sensor state code never actually executed.

I did see that IMAP have seen the new email since the email count was incremented. What can cause this blockage? What even tells HA that this code in configuration.yaml is even related to the IMAP integration? Is it the

event_type: "imap_content"

line?

Also, how can I debug it to see what’s actually happening?
I’m a veteran for system and software, but new to this part of HA.

You shouldn’t need to reboot, just restart…

No, you should only need to restart when you add new integrations (like in this case, you added the template integration.) or you add new files in a split configuration. Reloading is sufficient when you’ve only edited the configuration of existing integrations or files.

A trigger-based template sensor will be ‘unknown’ until it is triggered.

If the IMAP search criteria is less exclusive than the content sensor’s trigger event data, the sensor will increment but the content sensor will not be triggered. Setting the initial configuration variable to true, as you have, means the content sensor will be more exclusive.

Any time an IMAP sensor receives an update an imap_content event is posted to the event bus. Setting up a trigger creates a listener on the event bus specific to the trigger’s configured event type and event data.

You can use the Developer Tools: Events tool to raise or listen for specific events.

1 Like

Thank you!
Using Developer Tools: Events I indeed seen the events coming. I changed the template to not condition on “initial” and now it works.

To be able to use this method for controlling stuff around, I need to bring the sensor to idle state between events, else the automation does not react to two sequential emails of the same type (i.e. the sensor does not change state).
Can you please recommend a way to do that?

I was thinking that the automation may fire an imap_content event to return the sensor to idle, but it seems like I’m hacking something that can be significantly simpler.

Get rid of the middleman… use an event trigger in your automation. Then there’s nothing to reset.

1 Like

I successfully reacted to any imap_content in an automation.

How can I perform the conditioning on the subject?

In the sensor case, I used trigger.event.data to access the data. Here when I try to edit the condition: part, I don’t know the right reference.
Should I perform the keyword search in the condition: part or in the trigger: part?

Guidance is appreciated…

alias: EmailBalconyLight
description: ""
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      sender: [email protected]
condition:
WHAT SHOULD GO HERE?
action:
  - type: turn_on
    device_id: b16775cc2c2e18d168fdd015f0737f95
    entity_id: cfb3f9ffccbf4d16a207cf38a5e21762
    domain: light
mode: single

You can filter in both the trigger or condition block. However, the trigger is less flexible as the values must match exactly including punctuation, capitalization, and even line breaks etc. Depending on how deep down the rabbit hole you want to go and how much control you have over the emails being sent, you can set up a custom template in the IMAP integration which will create a variable called custom which can pre-parse any of the attributes or any combination of them into a value you can use as a filter.

The reference is the same, you just set it up as a Template condition.

alias: EmailBalconyLight
description: ""
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      sender: [email protected]
condition:
  - condition: template
    value_template: "{{ 'LightOn' in trigger.event.data["subject"] }}"
action:
  - type: turn_on
    device_id: b16775cc2c2e18d168fdd015f0737f95
    entity_id: cfb3f9ffccbf4d16a207cf38a5e21762
    domain: light
mode: single

You can also move the conditions to the action block so that you can use the Choose or If/Then actions for branching logic or just to keep all the actions relating to a given device in one place.

alias: EmailBalconyLight
description: ""
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      sender: [email protected]
condition:
  - condition: template
    value_template: "{{ 'BalconyLight' in trigger.event.data["subject"] }}"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ 'LightOn' in trigger.event.data["subject"] }}"
        sequence:
          - type: turn_on
            device_id: b16775cc2c2e18d168fdd015f0737f95
            entity_id: cfb3f9ffccbf4d16a207cf38a5e21762
            domain: light
      - conditions:
          - condition: template
            value_template: "{{ 'LightOff' in trigger.event.data["subject"] }}"
        sequence:
          - type: turn_off
            device_id: b16775cc2c2e18d168fdd015f0737f95
            entity_id: cfb3f9ffccbf4d16a207cf38a5e21762
            domain: light
mode: single

And that could be significantly compacted by using service calls and templates instead of device actions.

Compact Version
alias: EmailBalconyLight
description: ""
trigger:
  - platform: event
    event_type: imap_content
    event_data:
      sender: [email protected]
    variables:
      subject: "{{ trigger.event.data["subject"] }}"
condition:
  - condition: template
    value_template: "{{ 'BalconyLight' in subject }}"
action:
  - service: light.turn_{{ 'on' if 'LightOn' in subject else 'off' }}
    entity_id: cfb3f9ffccbf4d16a207cf38a5e21762
mode: single
1 Like

Thanks a lot!