Email recieved changes state, how do I change it back or reset after a certrain amount of time?

Hey all… I am setting up an automation that will automatically open a garage door if an email from a pre-approved sender is recieved in a special mailbox set up just for that purpose. I have setup the IMAP integration and the relevant template. I then created a sensor that changes if there is an email received that includes the subject line “open”.

That seems to be working fine… the question is this. I want to create an automation that opens the garage door for 5 minutes when this email is received, but the state of the sensor will stay as “open_recieved” until some other email is recieved that doesnt say “open” in the subject line. Since this email box will not be used for any other purpose I think the state will remain “open_recieved” indefinitely.

I would like the state to reset after a few minutes to “not_recieved” but cannot figure out how to do it.

Suggestions?

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "imap_content"
    sensor:
      - name: imap_content
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Entry: "{{ trigger.event.data['entry_id'] }}"
          UID: "{{ trigger.event.data['uid'] }}"
          Message: "{{ trigger.event.data['text'] }}"
          Sender: "{{ trigger.event.data['sender'] }}"
          Date: "{{ trigger.event.data['date'] }}"
          Subject: "{{ trigger.event.data['subject'] }}"
          Received-first: "{{ trigger.event.data['headers'].get('Received',['n/a'])[0] }}"
          Received-last: "{{ trigger.event.data['headers'].get('Received',['n/a'])[-1] }}"
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "Open_Email"
        event_data:
          sender: "[email protected]"
          initial: true
    sensor:
      - name: Email-Open-Garage
        state: >-
          {% if 'open' in trigger.event.data["subject"] %}
            open_recieved
          {% else %}
            open_not_recieved
          {% endif %}

Bonus second question: For this part:

    event_data:
      sender: "[email protected]"
      initial: true

that checks against a specific email address, how do i check multiple email addresses? I have a list of 4 allowed emails.

Thanks!!

Add another trigger for the sensor itself with a duration and modify the if to handle it.

Set up a list variable in an action block. The list could also be set as a variable in the state template if you preferred to do it that way.

  - trigger:
      - platform: state
        entity_id: sensor.email_open_garage
        for: "00:05:00"
      - id: email
        platform: event
        event_type: "imap_content"
        id: "Open_Email"
        event_data:
          initial: true
    action:
      - variables:
          allowed:
            - "[email protected]"
            - "[email protected]"
            - "[email protected]"
    sensor:
      - name: Email-Open-Garage
        state: >-
          {% if (trigger.id = 'email') 
          and (trigger.event.data['sender'] in allowed) 
          and ('open' in trigger.event.data['subject'])
          %}
            open_received
          {% else %}
            open_not_received
          {% endif %}

Thank Didgeridrew, for this comment:

By chance do you have any sample code you could point me at? I am a newb so thanks for your help.

See above…

Ok, for some reason I assumed the code only showed the multiple email addresses bit. Sorry for wasting time!

To make sure I understand:

This code:

causes the state to revert every 5 minutes? So, after an action happens (Email recieved with the word Open in the subject) then 5 minutes later it resets the state to “open_not_recieved”

Thank you so much for your help!

Yes, the trigger will fire once the sensor’s state has been stable for the defined amount of time. If desired, you can specify that it should only happen on state changes to “open_received”.

If you are open to switching to a binary sensor, you could just use the auto_off configuration option.