MQTT camera: extract value_json from MQTT topic

Whenever Blue Iris detects a person using Deepstack, I have it send an MQTT message with a state and an image of the alert:

The &ALERT_JPEG is a base-64 encoded jpeg of the last alert.
When I listen to the topic in the MQTT integration in HA, I see the image come in:

A binary sensor has the option to select which json value to look for:

binary_sensor:
  - platform: mqtt
    name: "mqtt bi cam_garage person"
    state_topic: "BlueIris/cam_garage/PersonDetected"
    value_template: "{{ value_json.state }}" <--------- check the "state" value
    payload_on: "ON"
    payload_off: "OFF"
    device_class: motion

I need something similar for the MQTT camera, to extract the “image” value.
Right now, I still just want to show the item, later I will use the image in a notification sent to my phone.
The MQTT camera doesn’t seem to have a value_template option.
I’ve tried using json_attributes_topic and json_attributes_template, to no avail:

camera:
  - platform: mqtt
    name: "mqtt bi cam_garage alert snapshot"
    topic: "BlueIris/cam_garage/PersonDetected"
    json_attributes_topic: "BlueIris/cam_garage/PersonDetected"
    json_attributes_template: "{{ value_json.image }}"

Is there a way to tell the MQTT camera which value to look for?

+1

mqtt.camera misses the value_template

I am using INSTAR MQTTv5 camera which provides an MQTT topic for the base64 snapshot:

mqtt_camera

The implementation would be:

mqtt:
  camera:
    - topic: cameras/115/status/task/photoseries/snap/base64
      value_template: '{{ value_json.val }}'
      name: snapshot
      encoding: b64
      icon: mdi:image
      qos: 1

To extract the base64 string from it’s JSON expression {"val":"/9j/2wCEABALDA4MChAODQ4SE..."}. But this leads to an Error message:

Configuration invalid!: Invalid config for [mqtt]: [value_template] is an invalid option for [mqtt]. Check: mqtt->mqtt->camera->0->value_template. (See /config/configuration.yaml, line 21).

Or is there a way to solve this with the json_attributes_template ?

I’m having the same problem. Any ideas, alternate solutions?

OK, found something, but not much. I couldn’t make the MQTT camera work, but I found another way to connect the Instar camera to HA on the Instar wiki. Of course, this doesn’t really work as expected. There are some caveats, some more serious than others.

  • The generic camera cannot be set up using YAML, only the UI.
  • The configuration written there doesn’t work for my camera model. I could set up the RTSP stream but not the still image.
  • To make it work, the Stream integration needs to be set up in MQTT.
  • And the real problem: when I open the camera stream in HA, I can occasionally get an image, but continuous streaming does not work. Playing the RTSP stream using Mplayer from my desktop works, so the problem is on HA’s side, not the camera’s.

OK, I found out what’s the problem with the RTSP stream. My HA server has a Celeron CPU, which is just not powerful enough to play a 2K stream, while my desktop i7 plays it fine. If I use the 720p stream, it works fine.

After some more digging in the Instar documentation, I could also make the snapshots work.

So after all, the Instar camera can be made to work with HA, but not through MQTT, and it takes a lot of digging in the documentation to figure out how. In a way I don’t mind it, because HTTP and RTSP are probably more efficient in transferring images and videos than MQTT anyway.

This thread is a bit old but it was the best hit when I was looking into my issue (but didn’t have an answer): I want to provide metadata along with the image from Blue Iris to Home Assistant for a MQTT camera.

By digging into threads on ‘json_attributes_template’ I was able to find the answer and I wanted to post the complete solution here as someone else may need it in the future.

Here are the two Blue Iris actions that I have triggered by an alert:

One sends the b64-encoded image:

And the other uses Blue Iris macros to send metadata in an ‘attributes’ topic:

Here is the full text of my current ‘attributes’ payload. Note that &JSON does NOT need to be surrounded by quotation marks because it’s already valid JSON. The other macros need quotes.

{"type":"&TYPE", "memo":"&MEMO", "json":&JSON, "time":"&ALERT_TIME"}

Then in my Home Assistant configuration I define an MQTT camera. The json_attributes_template is configured to parse the JSON I’m sending from Blue Iris. Note that the suffix in template’s “value_json.data.[attribute_name]” is just an arbitrary attribute name that I want to be populated under the MQTT camera device in Home Assistant.

mqtt:
  camera:
    - topic: BlueIris/OutFront4/alert-image-b64
      name: OutFront4 Alert
      unique_id: camera.OutFront4_alert
      image_encoding: b64
      json_attributes_topic: BlueIris/OutFront4/attributes
      json_attributes_template:  >-
        {"type":"{{value_json.data.Type}}",
         "memo":"{{value_json.data.Memo}}",
         "json":"{{value_json.data.JSON}}",
         "time":"{{value_json.data.Time}}"}
1 Like

Thanks for posting this solution. Is this still working for you?

The image part works great from blueiris to HA via mqtt, but for the life of me, can’t get the JSON attributes working. I’m on Feb 2024 version of Home Assistant.

Nevermind, figured it out.

I had my cameras defined as this. Removing the trailing encoding: image_encoding got everything up and running:

  - topic: BI/kitchendb-ds/alert-image-b64
    json_attributes_topic: BI/kitchendb-ds/JSON
    json_attributes_template:  >-
      {"type":"{{value_json.data.Type}}",
       "memo":"{{value_json.data.Memo}}",
       "json":"{{value_json.data.JSON}}",
       "time":"{{value_json.data.Time}}",
       "dbid":"{{value_json.data.dbid}}"}
    name: Kitchen DB Alert Image
    unique_id: camera.kitchen_db_alert_image
    image_encoding: b64
    encoding: image_encoding