Help with SMTP, images and templating

Hi,

Could someone help me with the following:

There is SMTP notification that works fine

  - name: Email
    platform: smtp
    server: smtp.gmail.com
    port: 587
    sender: [email protected]
    starttls: true
    username: [email protected]
    password: pasword
    recipient: [email protected]

and there is a code for automation that is supposed to send message with timestamp (it works) and also to attach image from ffmpeg camera:

  - service: notify.email
    data_template:
      message: >
        Motion/noise detected {{now().strftime("%a, %d %b %Y %H:%M:%S")}}
      data_template:
        images: >
          - {{states.camera.cam_hall.attributes.entity_picture}}

However, I receive this error in log file when trigger the notification:

16-10-23 00:46:22 homeassistant.core: Invalid service data for notify.email: extra keys not allowed @ data['data_template']. Got {'images': '- /api/camera_proxy/camera.cam_hall?token=66294480'}

Does anyone know how to attach photo from the ffmpeg camera? Or what is wrong in my templating code?

Hi @Molodax,

I’m not sure if this would help but, let me try.

I have created a Shell Command for firing the Shell Script.

This is the shell command:

`pushbullet_notification_cam_livingroom: 'sudo python3 /config/homeassistant/live/shell_commands/python_scripts/pushbullet_notification.py -c camera.living_room -t {{ states.camera.living_room.attributes.access_token }}'` 

where I’m passing the entity_id and the access token that is coming from the attributes.

the Script is getting that information and taking a snapshot, saving it in a folder (I want a local copy of it) and sending the notification with PushBullet. This is coming form here: https://pypi.python.org/pypi/pushbullet.py and I have adapted to fill my needs:

from pushbullet import Pushbullet
import urllib.request, os, time, sys,getopt

def main(argv):
   url = ""
 
   camera_url = ""
   try:
      opts, args = getopt.getopt(argv,"hu",["iurl="])

   except getopt.GetoptError:
      print ('pushbullet_notification.py -u <entity_picture_url>')
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print ('pushbullet_notification.py -u <entity_picture_url>')
         sys.exit()
      elif opt in ("-u", "--url"):
         url = arg

   print ("entity_picture_url is " + url)
   camera_url = "HA address" + url
   local_image = "/config/homeassistant/live/www/cam_captures/" + token
  
   print ("Final URL is is " + camera_url)

   pb = Pushbullet("o.glVrNbgmxHR0i0LJw9CFmjVc5pgO1rJ4")
   urllib.request.urlretrieve (camera_url, local_image)
   

   with open(local_image, "rb") as pic:
       file_data = pb.upload_file(pic, "Notification from Home Assistant", "image/jpeg")
   push = pb.push_file(**file_data)
   
   
if __name__ == "__main__":
   main(sys.argv[1:])

Hope this helps!

1 Like

Thanks, great! :smiley:

Hi Davedan,

Could you please explain a bit more how your script is dealing with entity_id and access_token? The script you shared below seems like looking for ‘-h’ and ‘-u’ option, but you are sending ‘-c’ and ‘-t’ and also variable token is never assigned.

Thanks.