IMAP Template Sensor

I need some help with a template sensor for my IMAP integration.

I’m using the IMAP integration and have added this sensor in my template_sensor.yaml.

  - sensor:
    - name: RCC IMAP Template
      state: '{{["subject"] }}'
    - event_data:
        sender: [email protected]
        username: [email protected]
      event_type: imap_content
      id: custom_rcc_event
      platform: event

This creates the sensor: sensor.rcc_imap_template

This works for showing me the subject of the email but I want to limit it down further on specific emails containing “New order # (followed by variable numbers)” and pull numbers from that email.

So I want the new sensor to read: New order # (insert the number from the email).

Using ChatGPT, I’ve tried so many variations of sensors (added to sensors.yaml), I’ve lost track. Currently I’m getting unavailable for this first example. The second example, I keep getting "No Order Number Found (Invalid Sender).

- platform: template
      friendly_name: "RudoCC New Order Number"
      value_template: >-
        {{["subject"] | regex_findall_index("\\\\New order #\\\\ ([0-9]+)") }}
- platform: template
      friendly_name: "RudoCC New Order Number"
      value_template: >-
        {% set subject_sensor = states('sensor.rcc_imap_template') %} 
        {% set subject = state_attr(subject_sensor, 'state') %}
        {% set sender = state_attr('sensor.imap_emailaddress_gmail_com', 'from') %}

        {% if sender == '[email protected]' %}
          {% set regex_match = subject | regex_findall_index('New order# (\d+)', ignorecase=True) %}
          {{ regex_match[0] if regex_match else 'No Order Number Found' }}
        {% else %}
          No Order Number Found (Invalid Sender)
        {% endif %}

This is one way to approach it. Since it’s based on the same trigger, you can just add it beneath the existing sensor (as shown) if your plan is to keep them both. Since you didn’t give any specifics about the IMAP search you are using, I’ve added a time-based condition to avoid false values that can occur when you view or delete messages and the search results update.

# template_sensor.yaml.
  - trigger:
    - platform: event
      event_type: imap_content 
        sender: [email protected]
        username: [email protected]
      - name: RCC IMAP Template
        state: '{{["subject"] }}'

      - name: RudoCC New Order Number
        state: |
          {% if["subject"] is search('New order', ignorecase=true) and 
["date"] > now() - timedelta(minutes = 1) %}
            {{["subject"] | regex_findall('(New order[ ]{0,1}#[ ]{0,1}[0-9]+)') | join }}
          {% else %}
            No New Order Number
          {% endif %}


It’s showing, No New Order Number. I just pushed an order though so it isn’t reading something correctly.

This is technically what the sensor.rcc_imap_template showing. Do I need to add something to read [RCC]

[RCC]: New order #2872

No, it’s that the spaces in your examples have been all over the place… :grin:

I updated the regex above to so it should cover all the bases.

Still getting No New Order Number

With a new email or the one from 2 hours ago?

New email, 20 mins ago.

The rcc_imap_template sensor shows the correct email subject.

I’m wanting to tie this into an automation to create a Todoist task with the order number.

First, double check that your template matches what I have above. The two things I changed previously were the find expressions in regex_findall() and search().

Second, make sure you have deleted or commented out all the previous attempts at the sensor then reload template entities.

Third, check the sensor’s state history, to make sure it hasn’t actually been catching the value and then getting overwritten based on the IMAP integration.

Do you foresee needing this information for other purposes or just this automation? If the only purpose is that automation, you don’t need to set up an intermediary sensor.

I’ve copied your code each time.

The imap template sensor is updating correctly and reads the subject until the email is deleted. The integration drops the number of emails but the template subject will keep the subject/state until a new email comes in.

I’ve been trying to think of how to take the subject from the template sensor to trigger the automation and then import the data from the subject (if it is a New Order) and create the Todoist task. If I can figure that out, that would negate the need for this 2nd sensor.

Since you said the first template sensor is working, you should be able to just use a state trigger.

I don’t use Todoist, so the service call in the following might not be exactly right with recent additions of the todo integration… but it should give you an idea of how to do it.

  - platform: state
    entity_id: sensor.rcc_imap_template
      - unknown
      - unavailable
  - condition: template
    value_template: "{{ trigger.to_state.state is search('new order', ignorecase=true) }}"
  - service: todo.add_item
      entity_id: todo.example
      item: "{{ trigger.to_state.state | regex_findall('(New order[ ]{0,1}#[ ]{0,1}[0-9]+)') | join }}"

Keep in mind that you might get some false positives with this method if you delete a newer email and the IMAP search “rolls back” to a previous email that includes the phrase “new order”. To avoid that you would need to add an attribute to sensor.rcc_imap_template to pull either the initial or date attribute from the email so you can base a condition off their value.


This worked. Thank you so much for your help.

1 Like