Attach photo in notification

I am wondering is it possible to attach photo in notification? I have added few generic cameras in configuration.yaml and I can view the snapshot using the URL set in still_image_url.

It would be great if I can attach those snapshots in my notifications. For example, when the home alarm is triggered, it will notify me with the snapshot from the specific camera.

4 Likes

That would be awesome!!! At least to send the image by email when the alarm is triggered… I’m sure that there is a workaround to do it through scrips or command lines…
Let’s see. For the moment Im still learning the basics and connecting my sensors from mysensors.

Regards,
Alfredo.

If anyone is still looking, I setup a shell command calling a script to send a camera image to pushbullet. It works well as a notification service.

2 Likes

Hello rpr,
I’m interested :slight_smile:
Would you mind to share the script to do that?

I’m thinking about the posibilites and this is huge!!

Thank you so much!!!
Regards,
Alfredo.

While I’d like to make it better, here is the script. I shamelessly copied it from a Reddit post, I just added the lines at the top to grab the image via shell commands. I’m going to fix that part, it was a quick and dirty hack.

Script:

#!/usr/bin/perl

use JSON;
use LWP::UserAgent;

# credentials and device identifier
my $api_key  = 'pushbullet api key';
my $device   = 'device iden';

# file to send
`rm /tmp/camera1.jpg`;
`wget -q -O /tmp/camera1.jpg http://x.x.x.x:8123/api/camera_proxy/camera.camera1`;
my $file_name = '/tmp/camera1.jpg';
my $file_type = 'image/jpeg';
my $user_agent = LWP::UserAgent->new();
$user_agent->agent('My File Pusher v1.0');

# Send a request for authorisation to upload a file ..
my $request_response =  $user_agent->post('https://api.pushbullet.com/v2/upload-request',
                                          'Authorization' => 'Bearer '.$api_key,
                                          'Content'       => ['file_name' => $file_name,
                                                              'file_type' => $file_type]);
if ($request_response->is_success) {
  # no obvious error from the server, parse the JSON ..
  my $json = from_json($request_response->content);
  if ($json->{'upload_url'}) {
    # we have an end point to push to and we can upload the file
    my @data;
    # the order of these fields is important, AWS will cry if you don't respect the ordering!!
    foreach my $item ('awsaccesskeyid', 'acl', 'key', 'signature', 'policy', 'content-type') {
      push(@data, $item => $json->{'data'}->{$item})
    }
    # and add the file
    push(@data, 'file' => [ $file_name ]);
    # now attempt to upload the file
    my $upload_response = $user_agent->post($json->{'upload_url'},
                                            'Content_Type' => 'form-data',
                                            'Content'      => \@data);
    if ($upload_response->is_success) {
      # looks like we uploaded the file successfully
      # so now send the actual push to the device
      my $push_response = $user_agent->post('https://api.pushbullet.com/v2/pushes',
                                            'Authorization' => 'Bearer '.$api_key,
                                            'Content-Type'  => 'application/json',
                                            'Content'       => to_json({'device_iden' => $device,
                                                                        'type'        => 'file',
                                                                        'file_name'   => $json->{'file_name'},
                                                                        'file_type'   => $json->{'file_type'},
                                                                        'file_url'    => $json->{'file_url'}}));
      if ($push_response->is_success) {
        #print "Pushed OK!\n";
        exit 0;
      }
      else {
        #print "Push failed!\n";
        exit 1;
      }
    }
  }
}

and the relevant part of my configuration.yaml:

camera:
  platform: generic
  still_image_url: http://x.x.x.x/jpg/image.jpg
  name: Camera1
  username: username
  password: password

shell_command:
  pb_send_image: /path/to/perl/script

automation:
  - alias: DeviceStatus
    trigger:
      platform: state
      entity_id:
      - binary_sensor.front_porch_motion
    condition:
      condition: state
      entity_id: device_tracker.my_phone
      state: away
    action:
      service: shell_command.pb_send_image
2 Likes

Thank you rpr for the tip.
Is it working for you? In my case, I get the upload-request successful but then it stuck when uploading the file into the upload_url.
I also tried with curl and I have the same problem…

curl -i -X POST https://upload.pushbullet.com/upload-legacy/blablablablablaaaa -F [email protected]g

Curl command is never ending…

Any idea why the upload it is not working?

Thank you.
Regards,
Alfredo.

Yes, it works fine for me. Did you generate an API key in pushbullet and get your device iden? You need to get those and put them in the script at the top, and change the url of your camera to grab a still frame.

You can’t just use curl like that to upload to pushbullet. You need to send a request to upload, then process the response, then tell it what file you are uploading, then upload the file. It’s detailed in the script above. If it was a simple one-line curl, we wouldn’t need the script!

Hi, Yes, I understand. The upload-request works fine and after doing it I received the upload_url to upload the file and the file_url to perform the push later. But on the next step when I try to perform the upload of the file (POST) it get’s stuck.

It get’s stuck in this part of the code:
my $upload_response = $user_agent->post($json->{'upload_url'}, 'Content_Type' => 'form-data', 'Content' => \@data);

I tried to do the POST with curl (after doing the upload-request) first but I get the same behaviour.

Something that is a little bit strange is the name of the upload_url that receive. It contains upload-legacy in its name…

Example:
https://upload.pushbullet.com/upload-legacy/CorwfYCqq4vDw5VZ6cjZUUGcrusEyJmI

Any idea? There is any way to debug in perl? Or to debug what curl is doing when it is stuck?

Update: I have just discovered something strange with my account.
When I go to settings, look at the max file upload size:

Do you have the same?

Regards,
Alfredo.

My apologies, I mis-understood your post. I do not have the same, mine says max file upload size is 25MB.

I have tried from another Linux PC and also I tried to re-create my account and I have the same problem. I can’t upload the file…
Summary:

  1. Get the upload URL: Working OK. I get the upload URL and the URL to do the push.
  2. Upload the file: Fail. It is never ending.
  3. Push: Never reached.

I’m wondering if there is a way to debut the command:
post($json->{'upload_url'}, 'Content_Type' => 'form-data', 'Content' => \@data);

Or to debut the curl command:
curl -i -X POST https://upload.pushbullet.com/upload-legacy/blablablablablaaaa -F [email protected]

I don’t understand what is happening…

Regards,
Alfredo.

See my other post, are you using the device iden or the nickname? You need to use the iden.

In my case, the scrip is failing on the upload step (before the push) where the deviceID is not necessary yet… I’m sure that it is something related to my account because I have the same problem with another script (based on bash) and also doing it manually with curl.

I will write to pushbullet support to see what happens…

Regards,
Alfredo.

Sorry, I can’t add anything to that.

Hi guys.
I’m using pushbullet to push an image from my camera at the door when someone rings the bell. :slight_smile:

I used this python library:

My python script:

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

pb = Pushbullet(“pushbullet API key”)
urllib.request.urlretrieve (“http://x.x.x.x/image/jpeg.cgi”, “/local dir/picture.jpg”)
with open(“/local dir/picture.jpg”, “rb”) as pic:
file_data = pb.upload_file(pic, “ringerpaa.jpg”)
push = pb.push_file(**file_data)

Add your own pushbullet API key and the ip to your camera.
It will get the picture from the camera and store it localy in the folder spcified.

I start this python script from a script.

4 Likes

Ok. I’m progressing with my problem with pushbullet. I know that the problem is not with my account but with the curl version on Linux. I tried from two different ubuntu distribution and I have the same problem. I also tried with curl on the Raspberry (weezy) and I got the same.
But when installed and tried curl on windows the upload is working fine! That’s very strange… I don’t know what could be the root cause on Linux. It seems that all the scripts around (perl, php, phyton, bash) are based on curl or curl library.

I will keep trying…

I’m becoming crazy… The same command works on windows but not in Linux. (any linux)

I am using Ubuntu 14.04.02 LTS (64bit) with no issues. That’s weird.

Hello,

Problem solved!!!
After doing a lot of tests I discovered that the problem was on the router. For some reason the server is trying to stablish a connection to the client or something like this. That’s very strange for a POST but I noticed that If I set the DMZ to the client’s IP then everything is working.
I still don’t know why this is working on windows… And why it is working with very small files… But it is working now :slight_smile: !!!
I discovered this when I run the Linux on a Window’s virtual machine. Then it was working… because it was using the Windows NW adaptor.

Anyway, it is working now.

Thanks,

1 Like

So is this working using @rpr script or something else? Cause I’d like to try this myself.

Yes. I’m using that version written in Perl. It is working fine and it is easy to understand.
I modified the code a little bit in order to push the message to all my devices (instead of one) and also to change the Subject of the message (instead the name of the file).

There are more versions available in bash, python and php with more functionalities but this one is doing the job pretty well.

Regards,
Alfredo.

Yeah, after I found that Perl script I found a python library that does it all, much easier. Meh, same result for me. Glad you got it working!