How to include camera snapshot in e-mail via automations

Hi,

I have been trying to build a automation that when doorbell is rang, the camera takes a picture and an e-mail is send.

I added in Config YAML my e-mail settings:

notify:
  - name: "email_1"
    platform: smtp
    sender: "[email protected]"
    recipient: "[email protected]"
    server: mail.xx.com
    port: 465
    username: xxxx
    password: "XxXxX"
    encryption: tls

As per instructions I found on this site.
Then I created an automation:
When YAML:

platform: device
device_id: 67e9fdb1a48b4cd68a892a869e524579
domain: rfxtrx
type: command
subtype: "On"

Then do (part 1):
Camera ā€˜Take snapshotā€™ on Voordeur RZW 33 YAML:

service: camera.snapshot
metadata: {}
data:
  filename: /config/www/images/doorbell_last_image.jpg
target:
  entity_id: camera.voordeur_rzw33_high

Then do (part 2)
Call a service ā€˜Notifications: Send a notification with email_1ā€™

service: notify.email_1
data:
  message: Er staat iemand voor de deur
  title: Voordeur On
  image:
    - local/images/doorbell_last_image.jpg

When I run the last bit I get:
Error running action
extra keys not allowed @ data[ā€˜imageā€™]. Got None

Tried also at image:
/config/www/images/doorbell_last_image.jpg
Same error.

I have no clue how to get a simple mail notification working, I was told HA would be simpler the Domoticzā€¦

Hope that there is someone who can assist and let me know how I get this working.

Thank you!

Try this:

service: notify.email_1
data:
  message: Er staat iemand voor de deur
  title: Voordeur On
  data:
    images:
      - /local/images/doorbell_last_image.jpg

Thanks!

Tried it and get:
Error running action
Unknown error

When I check the Traces I see this message:
Error: please run connect() first

Any other thoughts on how to get this working? All the support is much appreciated!

Looking at my working snapshot examples, the path used between creating the snapshot and using the snapshot is the same

service: camera.snapshot
metadata: {}
data:
  filename: /config/www/images/doorbell_last_image.jpg
target:
  entity_id: camera.voordeur_rzw33_high


and then

service: notify.email_1
data:
  message: Er staat iemand voor de deur
  title: Voordeur On
  image:
    - /config/www/images/doorbell_last_image.jpg

Now that being said the error isnā€™t very descriptive and Iā€™m not familiar with email notification, I use Pushover to my phone with this:

action:
  - service: camera.snapshot
    metadata: {}
    data:
      filename: /config/www/snapshot/"{{ trigger.from_state.name }}".jpg
    target:
      entity_id: camera.driveway_snapshots_clear
  - service: notify.pushover
    metadata: {}
    data:
      message: Motion Detected by Reolink Camera
      title: >-
        {{ trigger.from_state.name }} at {{ now().strftime('%Y-%m-%d') }} {{
        now().strftime('%H:%M') }}  
      target: Samsung-S20
      data:
        attachment: /config/www/snapshot/"{{ trigger.from_state.name }}".jpg

Did also try the same path, same error.

Is there a way to test the SMTP connection?

I would start by sending an basic email no attachment and build from there, Iā€™m not aware of any method to test e-mail from within HA

I just did that, just e-mail when light is switched on or of.
Same error:
Error: please run connect() first
No clue what it is

This is a Python error related to opening the connection to the mail server.

Seems this is more a email connection problem that an automation problem can I suggest we try a few thingsā€¦

Did the ISP or email provider provide guidance on the selecting port 465? Using port 465 is regarded as legacy or deprecated. If they provided explicit guidance on that, try changing the encryption to ā€œstarttlsā€

Should that not work, change to port 587 again test with either ā€œtlsā€ or ā€œstarttlsā€

From the code snippet I canā€™t tell which service you are using, so need to try both.

Thanks that confirms my thoughts email connection issue.
I use TLS on port 465, this is how my e-mail client is configured too and there is works fine. I will play around with the email settings and see if I can get this up and running.

Yippee, it is working now.
To get the mail send I needed to add a time out in the SMTP yaml. Also went from tls to starttls just te be more up to date.

notify:
  - name: "email_1"
    platform: smtp
    server: "my_mail_server"
    sender: "my_sender"
    port: 587
    timeout: 15
    encryption: starttls
    username: "my_username"
    password: "my_password"
    recipient: 
      - "my_email_addres"

Picture gets attached. Need to sort out why it is a low quality snapshot.

Thank you for helping out.

1 Like