How to send camera_image to MQTT Payload?

I am using Blue Iris with some IP cameras around the home. I have some picture-glance cards with the following configuration, these are working properly:

  - type: picture-glance
    title: Front Door
    entities: []
    camera_image: camera.front_door

I am wondering how could I send the “camera_image: camera.front_door” to an MQTT Payload.

Any help would be highly appreciated.

My automation would be something like this, but I have no idea what to put into the payload. :

  - alias: "Doorbell Notifications"
    trigger:
      platform: state
      entity_id: binary_sensor.doorbell_button
      to: 'on'
    action:
      - service: mqtt.publish
        data:
          topic: 'zanzito/xxxdevice/photonotification'
          payload: camera_image: camera.front_door
          retain: 0

Thanks in advance!

This would require a custom component, which would either post a saved file over mqtt, or added a service to the camera integration. You can take code from https://github.com/robmarkcole/mqtt-camera-streamer

Thank you @robmarkcole! Probably I will write a shell script to save an image and send that image over MQTT. I will post it here once I am done with it.

1 Like

If you are saving the images locally you can use folder_watcher with an automation to process/post new images as they are saved

I almost solved the problem, the only remaining issue is to send the image to Zanzito via MQTT photonotification. I am always getting an “Error receiving image” message in the Zanzito log and not sure how to fix/debug it.

Anyone knows if it is the proper way to send an image to an MQTT topic? I refer to the end of my script below. Thanks in advance!

What I did:

  1. Created a shell command in configuration.yaml. This command is putting together the 3 captured camera snapshot into a short gif image:
shell_command:
  snapshot_frontdoor_gif: convert -loop 0 -delay 100 /home/homeassistant/.homeassistant/www/tmp/frontdoor1.png /home/homeassistant/.homeassistant/www/tmp/frontdoor2.png /home/homeassistant/.homeassistant/www/tmp/frontdoor3.png /home/homeassistant/.homeassistant/www/tmp/a.gif
  1. Created a script which I run if someone pushes the doorbell. The script creates the pictures, the gif and sends them to an MQTT topic:
### Making photos from the front door, used in doorbell automation

front_door_gif:
  alias: Front Door Gif
  sequence:
  - data:
      entity_id: camera.front_door
      filename: /home/homeassistant/.homeassistant/www/tmp/frontdoor1.png
    service: camera.snapshot
  - delay:
      milliseconds: 100
  - data:
      entity_id: camera.front_door
      filename: /home/homeassistant/.homeassistant/www/tmp/frontdoor2.png
    service: camera.snapshot
  - delay:
      milliseconds: 100
  - data:
      entity_id: camera.front_door
      filename: /home/homeassistant/.homeassistant/www/tmp/frontdoor3.png
    service: camera.snapshot
  - delay:
      milliseconds: 100
  - service: shell_command.snapshot_frontdoor_gif
  - delay: '00:00:03'
  - service: mqtt.publish
    data:
      topic: 'zanzito/MyPhone/photonotification'
      payload: /home/homeassistant/.homeassistant/www/tmp/a.gif
      qos: 1
      retain: 0

payload is expecting a string. It will not post a file. You will need to find a way to turn your git into a byte_array and post that as the oayload

So I figured out how you could post the camera image over mqtt. You would create a new service which is a small edit of the camera snapshot service (defined in async_handle_snapshot_service). The approximate code would be:

def pil_image_to_byte_array(image):
    imgByteArr = io.BytesIO()
    image.save(imgByteArr, "PNG")
    return imgByteArr.getvalue()

image = await camera.async_camera_image()
byte_array = pil_image_to_byte_array(image)
client.publish(MQTT_TOPIC_CAMERA, byte_array, qos=MQTT_QOS) # some internal service
1 Like

Thanks a lot @robmarkcole! You are awesome, I might going to give it a shot.

Actually, I ended up configuring HTML5 Push notifications and using that to send an image notification to my phone. So far it is working great. The only issue that I am creating a gif and it is showing only as an image (the first frame of the gif) in the push notification.

1 Like

Is this implemented as a python script? python scripts can access the camera