Imap Email Content Automation

I have an old security camera DVR that cannot be directly connected to HA. But it can send emails. So I have it setup to send an email when there is motion detected i.e. a car has entered the driveway. I want to turn on the z-wave outside light.

In the config yaml I have the following


  • platform: imap_email_content
    username: [email protected]
    password: my_password
      value_template: >-
      {% if ‘CAMERA1’ in subject %}
      {% endif %}

OK, so now what? I’m a real beginner at HA so I’m hoping someone can point me towards the next step.



First you should read this, especially point #11, how to post code.

My question was of a general nature and not about specifics.

I’ve followed the line by line instructions for the Imap Email Content on this page.

I’m not a programmer. I’ve spent hours checking syntax, watching videos and reading various docs. Sorry, but I just have no clue what to do next.

It appears I’ve posted to the wrong forum.

Sorry to have been a burden

Hi Bill, it’s not easy to be a beginner.
But @VDRainer points out the first thing you must do, and I think it’s well meant.
When you post code examples you must format the code correctly. If you don’t it’s almost impossible to help.
What to do next? Well, first of all you have to confirm that your imap_email_content sensor actually works. Then you should create some automations to turn your outside light on when motion is detected, and of course off again when you don’t want the light on anymore.
I wish you the best of luck :smiley: Home Assistant is really fun, but can sometimes be a little frustrating when you are a beginner.

1 Like

I appreciate your response. My question was never about about my code, which was copied from this page, it was about what to do next.

And you are spot on when I need to confirm that the imap_email_content is working… BUT how do I do that? Does anyone have a link to that procedure.

I assume you’re receiving an email when the camera detects motion?
Then the next step is to confirm that the imap_email_content sensor exists in Home Assistant and works properly. If the sensor exists you should see it listed under States in the Developer Tool. And you should also see that the state is motion if it reads the email correctly.
If you don’t see the sensor then it would have been very useful to see your code formatted…

I have a couple of imap_email_content sensors myself and one looks like (sorry the values are in my native language):

- platform: imap_email_content
  port: 993
  username: !secret gmail_username
  password: !secret gmail_password
    - [email protected]
  name: pollen_burot
  value_template: >-
    {% if 'Beskjeden spredning av Burot' in body %}
    {% elif 'Moderat spredning av Burot' in body %}
    {% elif 'Kraftig spredning av Burot' in body %}
    {% elif 'Ekstrem spredning av Burot' in body %}
    {% else %}
    {% endif %}

In the Developer Tool I see a sensor named sensor.pollen_burot and one of the states in the value_template.

Ah thank you… no I have not seen the variables in the Developer Tools -> States. That’s so helpful. so here is the code

  - platform: imap_email_content
    name: motion_status
    port: 993
    username: [email protected]
    password: mypassword
    value_template: >-
      {% if 'BB DVR' in subject %}
      {% endif %} 

You don’t see a sensor named sensor.motion_status in the Developer Tool? Or you see it, but it hasn’t got any state? Your sensor definition looks okay as far as I can see.
Another thing, when the camera stops detecting motion, will you then receive a new email? Otherwise your sensor will always have the state motion_yes . Or are you planning an automation that resets it to motion_no a few minutes after motion is detected?

That is correct;. I do not see sensor.motion_status in the developer tool.

Another thing that’s told me I’ve goofed, I can’t find the inbox. I have that samba app installed so I can see my RPi folders on my W10 PC… just nothing that’s like an inbox. Am I supposed to create one and if so where does it go?

And your right about my if statement. I’ll do an elif later on. :slight_smile:

If you don’t see the sensor, try restarting Home Assistant and look in the log for errors.

I receive the emails in my regular gmail inbox (somewhere up in the Google cloud) and that’s where my imap_email_content sensors looks for new emails. I have configured Gmail with an app password as described here and I use this app password within Home Assistant. If your camera has a build-in email server I guess emails can be sent to your gmail address and then read by the sensor (if it’s configured correctly).

OK so the inbox refers to the gmail inbox, not an inbox on the RPi. Great more clarifications. And I’ve done many a restart.

So it’s got to be something I’m doing wrong with the gmail stuff…

Time for a lot of head scratching… but you’ve helped me direct my focus.

Another possibility… Right now I simply type in to get to HA. Do I need to have a secure (https) connection to read gmails?

BTW my router is a LinkSys 3200 running DD-WRT

No, you don’t need https…
Open a terminal window on your RPi (or use the ssh plugin in HA) and simply see if you can reach out to Google’s IMAP server by sending ping -c 5 . If success, the response should be something like:
If you don’t get any response, something must be wrong in your end.

Thank you for the suggestion and the “I’m clueless how to do it” procedure. Very helpful.

Here is the ping
Screenshot 2021-03-21 070712

I’m still scratching my head

Well, at least now you know that you can get in touch with gmail from HA.
But do you receive emails in your gmail inbox from your camera when motion is detected?
(jnrksv assumes you are receiving emails, but I can’t see you confirming that you are actually receiving emails when motion is detected.)

Yes, I’m checking my Gmails. I’m recieving new ones often. I’ve temporarily set the sensitivity of the camera motion sensor to trigger if a bird sneezes so I get plenty of emails to the gmail account.

Nice. If you have enabled multi-factor authentication in your Google account, you have to create an app password (as described in the link above in the last reply from jnrksv).

I’m not sure if I have. Up to now I’ve never had any cause to have a IMAP/gmail account… so I’ll give it a try.

I do not have 2 step verification.

I also just tried the DD-WRT integration… I don’t think that’s working…

I’m now googling how to set DD-WRT so HA will be happy

BTW I’m on build 44048

I just installed the NMAPper and it works fine. Can see all my IP stuff. So my HA can see other things. Which I guess is some progress. :slightly_smiling_face: