Help with sending pictures

I´m trying to send a picture from my camera to a Zanzito client.
But I have some issues to get the script filed to execute.

shell_command:
mqtt_send_file: ‘/home/pi/.homeassistant/mqtt_send_file.py {{ topic }} {{ imageFilePath }}’

Even if the path is correct I have

  File "/usr/local/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.4/site-packages/homeassistant/core.py", line 1030, in execute_service
    service_handler.func(service_call)
  File "/usr/local/lib/python3.4/site-packages/homeassistant/components/shell_command.py", line 70, in service_handler
    stderr=subprocess.DEVNULL)
  File "/usr/local/lib/python3.4/subprocess.py", line 537, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/local/lib/python3.4/subprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.4/subprocess.py", line 1456, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '/home/pi/.homeassistant/mqtt_send_file.py'

The file is there and it´s even chmod 777

Also how is the notification to be set up?

Atm I´m trying

  - service: shell_command.mqtt_send_file
    data:
     topic: 'zanzito/USERNAME/notification'
     imageFilePath: 'https://192.168.1.XX:8123/api/camera_proxy/camera.CAMNAME?token=TOKEN'

E.g. same path that is defined on my cameras entity_picture in HASS.

Noone used Zanzito for sending pictures?

@MrMep: Can you assist?

A Python script is not directly executable unless you have an interpreter specified on the first line of the script. It will look something like

 #!/usr/bin/python

in the file. Does that file exist? You should try running that “script” file from the command line shell while debugging this to ensure it works…

Alternatively, maybe your command invocation should just call python on the script file to avoid some of these problems, especially if you rely on the process’ PATH variable to find the python instance running in a virtualenv.

It says #!/usr/bin/python3.5 but I´m actually running 3.4 on my rPi
Tried to change to 3.4 and even #!/usr/bin/python but without luck

When I run the python from shell I get

pi@Telldus ~/.homeassistant $ python mqtt_send_file.py
Traceback (most recent call last):
File “mqtt_send_file.py”, line 3, in
import paho.mqtt.client as mqtt
ImportError: No module named paho.mqtt.client

but I have it installed:
Requirement already satisfied: paho-mqtt in /usr/local/lib/python3.4/site-packages

I am not familiar with the various Raspberry Pi installations of home assistant, as I run it on a Mac mini installed in a virtualenv. Its possible that your Home Assistant installation is running in a virtual environment and has different runtime environment than the default installation.

Is there a file ~/.homeassistant/bin/activate present? If source, from the shell you should do

source ~/.homeassistant/bin/activate

and you will now have the same environment that Home Assistant is using while its running, and when it spawns processes. Maybe that environment doesn’t have MQTT present (though that seems unlikely). But it might be useful to check.

Your path is wrong

Use HA API path

https://192.168.1.xx:8123/api/camera_proxy/camera.CAMNAME?api_password=YourHAPassword

Thank you for the info, I have updated the url

Hi
I´m not running in a virtual env. Thinking I must maybe update to Raspian Stretch and python 3.5…

It seems to be some issues with the default python path.

Specifying 3.4 when running fixes the paho error.
It gives me however another:

pi@Telldus ~ $ python3.4 .homeassistant/mqtt_send_file.py ‘zanzito/user/notification’ ‘https://192.168.1.XX:8123/api/camera_proxy/camera.CAM?api_password=PASS

Traceback (most recent call last):
File “.homeassistant/mqtt_send_file.py”, line 21, in
with open(imageFilePath, “rb”) as imageFile:
FileNotFoundError: [Errno 2] No such file or directory: ‘https://192.168.1.XX:8123/api/camera_proxy/camera.CAM?api_password=PASS

Maybe you need to extract to jpg first?

OK, I got it to work in a way. Running:
python3 mqtt_send_file.py zanzito/user/photonotification logo_sm_ny.gif sends the picture to the mobile.

Too bad you can´t use direct camera links though, would be much more convenient

I don’t use zanzito so don’t know image support.

I use HTML5 push and link I provide you work.
I did same as you and use links from entity page but found API link appaer to provide jpg.

Anyway, glad it work for you

OK, thank you for confirmation that the link can work.

@MrMep: would it be possible to implement functionality to use HASS links directly instead of first extact to jpg?

Does anyone know why the script provided from @MrMep does not work on a Ubuntu server but on a rPi

I have this script running fine on a rPi (python 3.4) but when using it on Ubuntu with python 3.6 my broker only returns socket error.

#!/usr/bin/python3.4
import paho.mqtt.client as mqtt
import io
import sys, getopt
import urllib.request

# MQTT parameters
mqttServerAddress = "192.168.1.IP"
mqttServerPort = 1883
mqttUsername = "USER"
mqttPassword = "PASS"
mqttQos = 0
mqttRetained = False
# Reads command line arguments
topic = sys.argv[1]
imageFilePath = sys.argv[2]

# Store File to disk
urllib.request.urlretrieve(imageFilePath, "local-filename.jpg")

# Connects to the MQTT broker
client = mqtt.Client()
client.username_pw_set(username=mqttUsername,password=mqttPassword)
client.connect(mqttServerAddress, mqttServerPort, 60)
# Reads the file
with open("local-filename.jpg", "rb") as imageFile:
 myFile = imageFile.read()
 data = bytearray(myFile)
# Publishes it
client.publish(topic, data, mqttQos, mqttRetained)

I have changed /usr/bin/python3.4 and I run same version of paho-mqtt on both (v.1.1)

When publishing within HASS it is also working (same Ubuntu server) so it is just this script

I ended up simplifying the script a lot and use mosquitto_pub instead.

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import datetime
import io
import sys, getopt
import urllib.request


now = datetime.datetime.now()

# Reads command line arguments
topic = sys.argv[1]
imageFilePath = sys.argv[2]

#store to disk
urllib.request.urlretrieve(imageFilePath, "movement.jpg")

os.system("mosquitto_pub -u USER-P PASS-h 192.168.1.IP-p 1883 -t " + topic + " -f movement.jpg" )

raise SystemExit

There is room for improvement such as dynamic filename etc,