Receive images via MQTT returns 500 Internal Server Error

Hi,

I’m looking to send an image through to HA via MQTT. My configuration is as follows:

mqtt:
  camera:
    - topic: cctv/notification
      name: "CCTV images"

When I add a picture entity card, it doesn’t show the image that is sent.

Screenshot 2022-09-17 at 14.58.47

Picture entity card:

show_state: true
show_name: true
camera_view: auto
type: picture-entity
name: CCTV
entity: camera.cctv_images

One thing I’ve noticed is that when I view the state attributes via Developer Tools, it shows:

access_token: a3b8147a90ee1d5e039cd3eb6f9b047205eb7456d25081b16b3e1ba7c8ed40fc
entity_picture: >-
  /api/camera_proxy/camera.cctv_images?token=a3b8147a90ee1d5e039cd3eb6f9b047205eb7456d25081b16b3e1ba7c8ed40fc
friendly_name: CCTV images
supported_features: 0

When I attempt to view the URL by adding my HA hostname to the start of the entity_picture

(https://ha.mydomain.com/api/camera_proxy/camera.cctv_images?token=a3b8147a90ee1d5e039cd3eb6f9b047205eb7456d25081b16b3e1ba7c8ed40fc)

it returns 500: Internal Server Error

I’m not sure what I need to do to get this working. My thoughts were that it has something to do with the way the image is sent via MQTT.

I have tried including

    encoding: "image_encoding"
    image_encoding: "b64"

to the mqtt config but still seeing the same behaviour.

If it’s of any use, I’ve tried various ways to send the image in python:

Attempt 1 - Converting to base64

with open("path-to-image.jpg", "rb") as imageFile:
    image = base64.b64encode(imageFile.read())
mqttClient.publish('cctv/notification', image)

Attempt 2 - Converting to bytearray

with open("path-to-image.jpg", "rb") as imageFile:
    myFile = imageFile.read()
    image = bytearray(myFile)
mqttClient.publish('cctv/notification', image)

Any help would be appreciated

Cheers,
Kev

MQTT is not suited for sending images. I’ve never heard of an MQTT camera.

The MQTT protocol allows messages with a maximum size of 260MB. Most brokers, especially public brokers, will restrict you to a few hundred bytes. If you are trying to send the complete image then you are likely hitting this limit.

The client side will have a Maximum Queued Messages limit (there is not a spec but it depends on the client and depends on the buffer size). When that limit is reached, new packets are discarded.

@stevemann See Camera MQTT - it is possible.

I don’t believe I’m hitting the 260MB limit. A 12 megapixel jpeg is only about 3.5mb, a lossless PNG will be about 7mb. (source)

Checking the image file size itself, it is 212K.

@KevSex did you get this to work?
facing the same issue

@gman The problem I had was that I was attempting to do multiple things with the open image file. I had to go back to the start of the file using seek(0).

For example:

with open("path-to-image.jpg", "rb") as imageFile:
    myFile = imageFile.read()
    #doing something with myFile
    myFile = imageFile.seek(0) # This is what I added to fix
    image = bytearray(myFile)
mqttClient.publish('cctv/notification', image)

Hope this helps.