Imap_email_content help required

Has anyone got this up and running yet?

sensor:
  - platform: imap_email_content
    server: imap.gmail.com
    port: 993
    name: 'email'
    username: username
    password: password
    senders:
      - [email protected]
    
    #value_template: not sure what goes here?

I have successfully set up the sensor, (using my own credentials) which shows in the gui as…

sensor.email unknown

At present the sensor just displays unknown & i can’t work out what to do!

Is there a specific format that the email or subject should be written in? or should i be using the value_template, but not sure how to utilise this?

thanks in advance,

james

This is an optional argument for the text in the emails that you are searching for. It becomes the state of the sensor.

Yes, that bit i sort of understand, i’m just not sure exactly how to write / format the value_template

Basically what i hoped to do was use my tado thermostat via an ifttt recipe to update states in hass, which could then trigger other automations

so for example if the tado status is ‘Away’ the ifttt recipe will fire an email with the subject as ‘Away’ & hass in turn would update…

sensor.email - 'away'

I have successfully set up the sensor & the ifttt recipe, i just can’t work out the final piece of the puzzle!!

Cheers
james

I’ve never used this so I would be wildly guessing at the syntax, but I’m thinking something like this;
Your email subject is an attribute of the sensor and your message sent has the subject “tado status away” and then you use this as your trigger. The sensor setup would look something like this (again, my snesor name is probably wrong but you get the idea):

"{% if is_state('sensor.imap.subject', 'tado status is away') %}true{% endif %}"

Sweet, I’ll have a look at this as a starting point

Cheers
James

I’m still having no luck here. Sensor has “unknown” and I can’t see how to use this. Can you share what you’ve done to make this work in any way?

I have not had time to tinker with it yet, so still no further on than before- sorry

If I do get anywhere I’ll post here
J

I’ve tried all sorts of configurations. I just can’t figure out how to work this one. Can someone share an example of how they’ve implemented this sensor? Thanks,.

  • platform: imap
    server: imap.gmail.com
    port: 993
    name: Gmail
    username: …
    password: …

form my sensors.yaml

thats it for me.

Maybe the security settings within your google account?

Sorry, I missed the senders value in my configuration.
I added 1 mail address to my configuration and tried 4 test emails, 2 from the address specified in the config and 2 from a different address.
Sensor is acting the same as without the senders value, unread email 4.
Will try with subject.

Thanks @Sitnalta - Actually, I’m referring to the imap_email_content sensor, not the imap sensor.

Anyone have any luck or examples of how to get the imap_email_content sensor working?

Did you ever get this going. If not; I got mine going in a round about way and I will be glad to share.

1 Like

I’m still interested and I’m sure others would benefit!

Sorry. Yes - it just started working after an upgrade. Currently works straight out of the box on version 0.31.1. I’ll share my configuration later on today in case that will help anyone.

1 Like

I never could get the sensor id so i created two sensors to solve the problem to get the email subject line. I would appreciate a better option.

sensor 1:
platform: imap_email_content
name: imap_emails
server: imap.gmail.com
port: 993
username: uname
password: pword
senders:
- [email protected]

sensor 2:
platform: template
sensors:
parsed_email:
value_template: ‘{{ states.sensor.imap_emails.attributes.subject }}’
friendly_name: ‘Email Subject Line’

1 Like

yes i noticed that where before it always just said unknown, since update to 0.31.1 it does now show info from an email. i still can’t work out how to set the state from the subject line though

Edit: If I write this into the template editor in the home assistant frontend…

{% if states.sensor.gmail %}
  {{ states.sensor.gmail.attributes.subject }}
{% else %}
  ??
{% endif}

I get the emails subject line to show up successfully within the template editor.

Just don’t know how should that be written as a value_template

Edit 2: so if I write this in my sensors config…

value_template: >-
      {% if states.sensor.gmail %}
          {{ states.sensor.gmail.attributes.subject }}
      {% else %}
          ??
      {% endif %}

It now successfully sets the state of the sensor, but unfortunately it sets it to the previous email sent, not the current email.

So if I send an email with ‘test’ as subject, then an email with ‘test2’ as subject it will display ‘test’ as the state, then when I send a ‘test3’ email the state updates to ‘test2’

:confounded:

The way I set this up was to simply not use the value_template on the sensor configuration, parse full load via a shell script, and trigger events via MQTT. You could search on any word/s you require. Been years since I was scripting, so excuse my hack-y solution. Of course, if anyone wants to make suggestions on how to improve this, I’m not proud!

I have an automation that checks for email state change:

alias: 'Email State Change - Parse'
trigger:
  platform: state
  entity_id: sensor.emails
action:
  - service: shell_command.parseemail
    data_template:
      value: "{{ trigger.to_state.state }}"

Calling a shell command:

'/home/pi/.homeassistant/parseemail.sh "{{ value }}"'

The command parses the emails:

#!/bin/sh

echo "****"  >> /home/pi/.homeassistant/parseemail.log
echo $1 >> /home/pi/.homeassistant/parseemail.log

if [ $# -ne 1 ]; then
  echo "Unknown"
else
  # Alarm Status
  IGNORECASE = 1
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/Security System Disarmed/ {print "adtstatus"}'` -m "Home"
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/Security System Armed Away/ {print "adtstatus"}'` -m "Away"
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/Security System Armed Stay/ {print "adtstatus"}'` -m "Stay"
  # Front Door Motion
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/Front Door Camera changed to Motion/ {print "FrontDoorMotion"}'` -m "ON"
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/Front Door Camera changed to Motion/ {print "FrontDoorMotion"}'` -m "OFF"
  # Back Door Motion
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/Back Camera changed to Motion/ {print "BackDoorMotion"}'` -m "ON"
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/Back Camera changed to Motion/ {print "BackDoorMotion"}'` -m "OFF"
  # First Floor Smoke Detector
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/1stfloorsmoke/ {print "1stfloorsmoke"}'` -m "ON"
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/1stfloorsmoke/ {print "1stfloorsmoke"}'` -m "OFF"
  # Second Floor Smoke Detector
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/2ndfloorsmoke/ {print "2ndfloorsmoke"}'` -m "ON"
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/2ndfloorsmoke/ {print "2ndfloorsmoke"}'` -m "OFF"
  # Basement Smoke Detector
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/basementsmoke/ {print "basementsmoke"}'` -m "ON"
  mosquitto_pub -h 127.0.0.1 -t home-assistant/emails/`echo $1 | awk '/basementsmoke/ {print "basementsmoke"}'` -m "OFF"
  # Front Door
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/frontdooropen/ {print "frontdoor"}'` -m "open"
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/frontdoorclosed/ {print "frontdoor"}'` -m "closed"
  # Garage Door
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/garagedooropen/ {print "garagedoor"}'` -m "open"
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/garagedoorclosed/ {print "garagedoor"}'` -m "closed"
fi

My ADT Alarm status sensor:

- platform: mqtt
  state_topic: 'home-assistant/emails/adtstatus'
  name: 'ADT Alarm Status'

My Binary Sensors for the other devices:

- platform: mqtt
  state_topic: 'home-assistant/emails/FrontDoorMotion'
  name: 'Front Door Motion'
  payload_on: "ON"
  payload_off: "OFF"
  sensor_class: motion

- platform: mqtt
  state_topic: 'home-assistant/emails/BackDoorMotion'
  name: 'Back Door Motion'
  payload_on: "ON"
  payload_off: "OFF"
  sensor_class: motion

- platform: mqtt
  state_topic: 'home-assistant/emails/1stfloorsmoke'
  name: '1st Floor Smoke Detector'
  payload_on: "ON"
  payload_off: "OFF"
  sensor_class: smoke

- platform: mqtt
  state_topic: 'home-assistant/emails/2ndfloorsmoke'
  name: '2nd Floor Smoke Detector'
  payload_on: "ON"
  payload_off: "OFF"
  sensor_class: smoke

- platform: mqtt
  state_topic: 'home-assistant/emails/basementsmoke'
  name: 'Basement Smoke Detector'
  payload_on: "ON"
  payload_off: "OFF"
  sensor_class: smoke

- platform: mqtt
  state_topic: 'home-assistant/emails/frontdoor'
  name: 'Front Door'
  payload_on: "open"
  payload_off: "closed"
  sensor_class: opening

- platform: mqtt
  state_topic: 'home-assistant/emails/garagedoor'
  name: 'Garage Door'
  payload_on: "open"
  payload_off: "closed"
  sensor_class: opening

Hope that helps!

3 Likes

Hi mplawner,

thank you very much for sharing your configuration - i tried to implement your parsing and scripting…with limited success. :wink:

So i started with the “imap_email_content”:

platform: imap_email_content
server: imap.gmail.com
port: 993
name: emails
username: …
password: …
senders:

  • sender1
  • sender2

Followed your automation:

  • alias: Email State Change - Parse
    trigger:
    • platform: state
      entity_id: sensor.emails
      action:
    • service: shell_command.parseemail
      data_template:
      value: “{{ trigger.to_state.state }}”

And used your script for parsing:

!/bin/sh

echo “****” >> /home/homeassistant/.homeassistant/parseemail.log
echo $1 >> /home/homeassistant/.homeassistant/parseemail.log

if [ $# -ne 1 ]; then
echo “Unknown”
else
IGNORECASE = 1
mosquitto_pub -h 127.0.0.1 -r -t RaspHA/emails/echo $1 | awk '/TEST-BACKUP FAIL/ {print "Backup_TEST"}' -m “Failed”
mosquitto_pub -h 127.0.0.1 -r -t RaspHA/emails/echo $1 | awk '/TEST-BACKUP SUCCESS/ {print "Backup_TEST"}' -m “Successful”
fi

My “sensor.emails” is showing new mails with the content “TEST-BACKUP FAIL”, but the mqtt sensor remains empty.

parseemail.log is showing:

****
****
****
****

Do you have a hint for me, where to start? Thanks in advance!

Hi! Whew, had to go back an reread what I did there, it has been a while!

If all you are seeing is the *****, and you see a number of them, then that would seem to suggest that the automation is being triggered, but that the value isn’t being passed.

First, I suggest trying this - run parseemail.sh manually to narrow down where the problem is (and rule out any issues upstream).
e.g.
./parseemail.sh “TEST-BACKUP FAIL”

If this works (and you see the line in the log, and your sensor switches to “Failed”), then you’ve narrowed down the problem to nothing being in the trigger.to_state.state portion of the automation.

To narrow it down further, send an email that would trigger the automation and check the sate in home assistant states <> page. If it isn’t empty (and says TEST-BACKUP FAIL or whatever you sent), then the problem seems to be in the automation and not in the sensor.

I’m not too strong on HA and the templates, but I would check carefully the yaml (it is real particular about spacing). I don’t know, but if the trigger is the problem, perhaps using something like {{ states(‘sensor.emails’) }} instead would work. But technically, I don’t see why the trigger wouldn’t work.

Also… I should note…

The state is the body of the email… not the subject line!
That’s pretty important to note.

So, make sure your email BODY includes the text you want to parse.

I’m kinda expecting an ‘Ah Ha’ moment from you.

Edit: Yeah, confirmed. That’s going to be your problem. Just tested it myself and got the same results you did. If you have TEST-BACKUP FAIL in the Subject, but not in the body, you’ll get ***** only (state is empty). Move TEST-BACKUP FAIL to the Body and you’ll be all set.