Question about using IMAP sensor for TTS announcements

Hello again,
I felt like another evening of self-flagellation and so I decided to try to switch my email notifications completely over to Home Assistant rather than using the Frankenstein’s monster that I’ve been using with Tasker on my Android phone and the REST API.
I’ve managed to trigger an automation when I receive an email in my Gmail account, and I’ve also figured out how to make a TTS announcement using trigger.event.data. The thing that I’m grappling with now is how to use the fields nested under “headers” in the imap_content sensor that’s populated when the event is triggered. I’ve found that trigger.event.data[‘sender’] works, but it returns the entire email address, which is not ideal for TTS. I noticed that there is a group in the table under “headers” that contains the field “From” and it would be much easier to parse a TTS-friendly name from that. Can someone clue me into the correct syntax to get that extra level down? I’ve tried a bunch of guesses but it’s getting me nowhere.
Thanks

You probably want something like the following:

{{ trigger.event.data.headers['From'][0].split('<')[0] }}

1 Like

Thanks for your help. The code I’m using now is:

data:
  data:
    type: announce
    method: speak
  message: >-
    {% set Emailer_Name = trigger.event.data['sender'] %} You have
    email from {{Emailer_Name}}.
action: notify.alexa_media_everywhere
enabled: true

…and that works, but when I switch out the relevant variable to trigger.event.data.headers[‘From’] it fails. I’ll figure out how to get the whole thing working before I work on the parsing. Do you have any other ideas of where it could be failing?

Not all imap_content events will contain headers, either because the original email didn’t have any or because they were left out due to event size limits.

I’m not sure I fully understand because I can see the fields I’m after in the developer tools if I listen to imap_content in the events section. The info is literally right there nested under headers:, From: – along with 8 other fields.

Did you try what I posted previously, it worked for me…

I’m just guessing here, but trying to use the value of the From header directly might be causing an issue because it’s an object not a string.

I tried exactly what you suggested, leaving out the {{ }} brackets since the trigger.event.data[‘sender’] worked and I figured it would need to be formatted similarly. When I try to add them in, I get a “message malformed” error when trying to save the automation.

  message: >-
    {% set Emailer_Name = {{ trigger.event.data.headers['From'] }} %} You have
    email from {{Emailer_Name}}.

Leaving out the curly brackets was correct, but I meant did you try the whole thing like:

message: >-
  {% set Emailer_Name = trigger.event.data.headers['From'][0].split('<')[0] %} 
  You have email from {{Emailer_Name}}.

Although, for error prevention you probably want something more like:

message: >-
  {% set Emailer_Name = trigger.event.data.headers['From'][0].split('<')[0] 
  if trigger.event.data.headers is defined else trigger.event.data.sender %} 
  You have email from {{Emailer_Name}}.
1 Like

Holy crap, dude. WTF? It works! THANKS!
Now I’m completely baffled as to why parsing something out of it would allow it to run, but at least it’s functional!
I’m still pretty bad at YAML, but my understanding of the parsing bit was that it was taking everything from the beginning of the string until the “<” character where the email address is encased in <>. Does it have anything to do with the array format you mentioned?

  1. The [0] after ['From'] selects the first item from the From array:

Bob Foobar <[email protected]>.

  1. The split('<') function turns that string into a new list by splitting it at each “<” it finds:

["Bob Foobar ", "[email protected]>"]

  1. The final [0] selects the first item from that new list:

Bob Foobar

Good to know! I’m always happy to learn something new.
Thanks again!