Help creating a template sensor for IMAP

Sorry, I struggle to articulate myself sometimes. Yes, the sensor provides that output:

If I mark my original email (Garage PIR) as unread, it changes to:

but all other emails involving the living room PIR result in the "

It looks like we both struggle… :slight_smile:

I meant these formatted-text boxes:

I need to know if that is how the message text is coming into HA or if you copy/pasted it from elsewhere.

Ahh I am with you now. I think I complicated things here, this is copied and pasted directly from my email (I just added it with the email screenshots for ease of reading).

It looks like I inadvertently left a trailing quotation mark on the template, that’s the source of the " (I have corrected it and removed the “from” in my post above). But that doesn’t explain the lack of the device name for the others.

I tried the second sensor but it doesn’t seem to be able to pull the text.

While it would be weird for one to be encoded and the other not, just to rule it out… let’s change the testing sensor to make it as simple as possible:

  - trigger:
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: [email protected]
    sensor:
      - name: Testing Yale Event Plaintext
        state: "{{ now() }}"
        attributes:
          plaintext: |
            {{ trigger.event.data['text'] }}
      - name: Testing Yale Event Base64
        state: "{{ now() }}"
        attributes:
          base64: |
            {{ trigger.event.data['text'] | base64_decode }}

EDIT: It’s probably better to split them just in case…

This also displayed as unknown

Even after an email is received from the correct sender?

Yes, I am marking the email as unread, so it appears as a new email. I did this when testing the original template, and it worked ok. I also have an IMAP sensor to notify me when I receive emails, and that confirms a new email has been received.

I just restarted my config again to make sure it wasn’t something like that, but it doesn’t seem to have changed anything. Looking at the sensor history, it does look like it changed from unknown to unavailable, but I am not sure if this was caused by restarting HA.

This is all working find now, thank you. The quotation mark seemed to have confused it but after changing it there are no issues.

Do you have a coffee/beer link?

1 Like

So I am not sure what has changed but for some reason this has stopped working. Whenever I receive emails from [email protected] it does not update the state. I have no idea on where to start on troubleshooting this, can anyone point me in the right direction, please?

Template:

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        event_data:
          sender: [email protected]
    sensor:
      - name: Yale Alarm Event
        state: |
          {{ trigger.event.data['text'] 
          | regex_findall(find='(?<=Device Name:).*?(?=\sfrom Account:)', ignorecase=False)| first }}
        attributes:
          timestamp:  "{{ trigger.event.data['date'] }}"

The place to start is the email. Has something changed that would cause the regex portion to fail? This is one of the major drawbacks to using emails from a third party. I have a theory that Marketing and Customer Service departments make pointless changes just so they can tell their bosses that they contributed to the quarterly system update… :upside_down_face:

The state will only update when it changes, so repeated triggers of the same sensor will have no effect on the sensor state. This is why several of my earlier examples used the “date” value or now() for the state… it helps insure that a series of messages about 1 sensor don’t get lost.

Thank you for your reply @Didgeridrew. The emails look exactly the same, I also marked old emails as unread to test and this was also not working. However, I agree with your theory.

I tried this too to make it simple but they both show as unknown

Is your IMAP sensor still catching the yale emails?

If the emails are being picked up by the IMAP sensor, then set up a basic IMAP content sensor using the existing sender trigger. You can leave “Yale Alarm Event” in place and just add the following below it (make sure to indent everything properly):

      - name: Basic Yale Event
        state: "{{ trigger.event.data['text'] }}"
        attributes:
          timestamp:  "{{ trigger.event.data['date'] }}"

Reload template entities or restart HA then trigger one of the sensors.

Ok, so I think I can see where I am going wrong. It transpires that marking Yale emails as unread is not adequate for testing purposes. I just triggered my alarm and everything worked as it should, but marking the emails as unread does not work.

However, the other night, my alarm was triggered, and it did not work. I am not sure why but my Garage PIR alert is displayed a little differently from other PIR’s; it does not state interior/perimeter. Can you see any reason why the Garage email below wouldn’t work the same as the others?

Garage:

Living Room PIR (Interior):

Dining Room (Interior):

Living Room PIR (Perimeter):

I just manually triggered Garage PIR and it does not work for some reason. The state is blank.

The only difference I can see is that “Garage” goes to a new line after “Burglar” and the others do so before. I don’t really see a reason the regex would fail due to a line break symbol there and not elsewhere, but you could try an even simpler expression like:

{{ trigger.event.data['text'] | regex_findall(find='(?<=Device Name:).*?Burglar', ignorecase=False) | list | first }}

1 Like

Thank you so much! That did the trick.

Thanks for your help @Didgeridrew, I would love to repay the favour in some way, be it buyacoffee/beer link or similar.

Edit: cut waffle.

There is a link on my profile.

1 Like