IMAP Sensor - Need Help re-writing sensor

GURUS,

Beginner here, so please be gentle. Want to start off by saying YES< i have read the documentation, done lots of trial and error and still failing. Finally coming here with the hope someone can tell me how to fix this.

I am trying to simply RE-CREATE a sensor that used to work before a few HASS upgrades and its related to the new way IMAP sensor works. The below code used to work before and now I am trying to get it to the new way, but not sure where i keep making errors. YAML does not complain about it, but it does not work simply put.

please assist.

Background: When the school bus comes, i get an email > trying to update a sensor based on that email from that specific sender so i can make the house yell on google home to get out of the house basically :slight_smile:

template:
  - trigger:
      - platform: event
        event_type: imap_content
        id: custom_event
        event_data:
          sender: [email protected]
    sensor:
      - name: here_comes_the_bus2
        state: >- 
         {% if 'bus stop radius' in trigger.event.data["text"] %} 
         get_avi
         {% elif 'PM' in trigger.event.data["subject"] %} 
         take_avi
         {% endif %}

This is my first time creating a post here, so not sure if formatting will come through, including a pastebin portion too in case above does not format properly.

Your template sensor configuration looks fine… as long as you have correctly added it to your configuration.yaml file (or other properly included file). If your notification automation triggers off the state of the sensor you may want to consider adding a third state so you can reliably capture state changes. For example, if you receive two “bus stop radius” emails in a row, the second would not create a state change.

If this configuration worked previously, the first point to check is the actual emails. Since your sensor’s state relies on matching strings in the body or subject, changes to those by the sender (or a changed sender) will cause the sensor to fail.

Thank You @Didgeridrew
I actually went to the GURUs over at discord and they helped me a bit through it. Not much changed other than an else towards the bottom and now it works great.

Posting code for others:

template:
  - trigger:
      - platform: event
        event_type: imap_content
        id: custom_event
        event_data:
          sender: [email protected]
    sensor:
      - name: here_comes_the_bus2
        state: >- 
         {% if 'bus stop radius' in trigger.event.data["text"] %} 
         get_avi
         {% elif 'PM' in trigger.event.data["subject"] %} 
         take_avi
         {% else %}
         none
         {% endif %}

Do you regularly receive enough emails from the sender for the else alone to act as a reliable reset? I would probably include a second trigger.

template:
  - trigger:
      - platform: event
        event_type: imap_content
        event_data:
          sender: [email protected]
      - platform: state
        entity_id: sensor.here_comes_the_bus2
        not_to: 
          - 'unknown' 
          - 'none'
        for: "00:00:30"
        id: reset
    sensor:
      - name: here_comes_the_bus2
        state: >-
         {% if trigger.id == 'reset' %}
           none 
         {% elif 'bus stop radius' in trigger.event.data["text"] %} 
           get_avi
         {% elif 'PM' in trigger.event.data["subject"] %} 
           take_avi
         {% else %}
           none
         {% endif %}