Blue Iris motion alerts to notification with image in Home Assistant

I discovered that Blue Iris has URLs that you can access directly to pull images from. This means I no longer need to use a camera entity and I can pass the image from the BI URL directly into the notification.
Just thought I’d share my solution for this.

Each camera is configured like this for alerts:
MQTT Topic: BlueIris/&CAM/alert
Post/payload: {“id”:"&ALERT_DB",“object”:"&MEMO",“camera”:"&CAM",“name”:"&NAME"}

I then created an automation in Home Assistant that grabs the variables, attaches the image and sends the notification. Bonus is that clicking on the notification will open the BI URL to a clip of the alert as well.

- id: 'blueiris_notification'
  alias: 'Blue Iris Notification'
  description: ''
  trigger:
    - platform: mqtt
      topic: BlueIris/+/alert
      variables:
        camera_name: "{{ trigger.topic.split('/')[1] }}"
  condition: []
  action:
    - service: notify.mobile_app_pixel_7_pro
      data:
        message: >-
          A {{ trigger.payload_json.object.split(":")[0].strip('%') }} was detected on the {{ trigger.payload_json.name }} camera.
        data:
          ttl: 0
          priority: high
          clickAction: "http://<BI_IP>:81/ui3.htm?maximize=1&tab=alerts&cam={{ trigger.payload_json.camera }}&rec={{ trigger.payload_json.id }}"
          image: "http://<BI_IP>:81/alerts/{{ trigger.payload_json.id }}&fulljpeg"
          actions:
            - action: "URI"
              title: "Live View"
              uri: http://<BI_IP>:81/ui3.htm?maximize=1&cam={{ trigger.payload_json.camera }}
            - action: "URI"
              title: "View Clip"
              uri: http://<BI_IP>:81/ui3.htm?maximize=1&tab=alerts&cam={{ trigger.payload_json.camera }}&rec={{ trigger.payload_json.id }}
  mode: queued
5 Likes

@wjbeckett, thanks for sharing this!!

However, I’m not getting the alerts in the app (Android), and I’ve been pulling my hair out trying to figure out why I’m seeing this error in the trace (I’m on HA 2022.10.5):

Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'

I cut-and-pasted your BI payload, and your automation (with the necessary changes to the “notify” mobile app and added the necessary “<BI_IP>” settings, of course).

I’ve verified the BI is sending the MQTT string using MQTT.fx, so that is all working:

{“id”:"@182539285",“object”:"person:63%",“camera”:"sidegate",“name”:"Side Gate"}

I’ve done a search for this error, but only found an issue on Github regarding Shelly autodiscovery, not really anything more.

Any ideas?

SOLVED: Somehow the quotes around the names (e.g. “id”) were backquotes. I fixed those to normal quotation marks, and it is now working. Found this by using an online JSON validation tool, which kept failing until my mental lightbulb went off. Apparently ‘payload_json’ is only an attribute if there is valid JSON. Thanks again for sharing this!

1 Like

Error in my log, camera works and the notification work however my log file gets filled up. I’ve truncated the message. My mqtt.yaml file is
camera:

  • name: Garage_Inside Alert
    unique_id: camera.garage_in_alert
    image_encoding: b64
    topic: BI/alert/Gar_Inside

  • name: Driveway Night Alert
    unique_id: camera.driveway_night_in_alert
    image_encoding: b64
    topic: BI/alert/Driveway_Night

  • name: Lower Patio Alert
    unique_id: camera.lower_patio_in_alert
    image_encoding: b64
    topic: BI/alert/Lower_Patio

  • name: Front Door Alert
    unique_id: camera.front_door_in_alert
    image_encoding: b64
    topic: BI/alert/Front_Door

Logger: homeassistant.util.logging
Source: util/logging.py:168
First occurred: 12:24:30 PM (2 occurrences)
Last logged: 12:24:54 PM

Exception in message_received when handling msg on ‘BI/alert/Gar_Inside’: ‘b’{"_events":{},"_eventsCount":0,“bitmap”:{“width”:1280,“height”:720,“exifBuffer”:{“0”:0,“1”:77,“2”:77,“3”:0,“4”:42,“5”:0,“6”…255,13,1,1,255,16,0,3,255,16,0,1,255]}},"_background":0,"_originalMime":“image/jpeg”,"_exif":{“startMarker”:{“offset”:0},“tags”:{“Software”:“Blue Iris”,“DateTimeOriginal”:1653853537},“imageSize”:{“height”:720,“width”:1280},“app1Offset”:24},"_rgba":true}’’ Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 44, in wrapper msg_callback(msg) File “/usr/src/homeassistant/homeassistant/components/mqtt/camera.py”, line 150, in message_received self._last_image = b64decode(msg.payload) File “/usr/local/lib/python3.10/base64.py”, line 87, in b64decode return binascii.a2b_base64(s) binascii.Error: Incorrect padding

Really loving this! But I wonder how to make it work with sending picture to telegram instead of to mobile phone.

I got it working to send message with camera id but cant figure out how to get the photo also.

Would be really glad if anyone has advise on this.

I just used this in yaml for automation:

alias: Blue Iris camera alert test
description: ""
trigger:
  - platform: mqtt
    topic: BI/+/alert-image-b64
    variables:
      camera_name: "{{ trigger.topic.split('/')[1] }}"
condition: []
action:
  - service: notify.telegram_main
    data:
      message: "Person detected: {{ camera_name }}"
      data:
        entity_id: camera.{{ camera_name }}_alert
mode: queued

I read that on telegram integration page there is this example. But how to get the photo attached?

...
action:
  service: notify.NOTIFIER_NAME
  data:
    title: Send an images
    message: "That's an example that sends an image."
    data:
      photo:
        - url: http://192.168.1.28/camera.jpg
          username: admin
          password: secret
        - file: /tmp/picture.jpg
          caption: Picture Title xy
        - url: http://somebla.ie/video.png
          caption: i.e., for a Title

Did you create an MQTT camera as I described in this message?

For me, that’s how I get the images to my notification on my iPhone. Basically that MQTT camera holds the alert image, and the notification I send to the iPhone sends the image with entity_id: camera.{{ camera_name }}_alert

I don’t use Telegram, and I don’t know if their integration supports sending an image straight from a camera. The example you posted shows getting the image from a URL or file. There are some other replies on this thread where people are storing the images to something like /tmp/picture.jpg which may be what you need to do to make it work for Telegram.

1 Like

I’m not able to figure this out.

Im on HAOS 2022.11.2
I’m not able to set
encoding: b64

what am I doing wrong?

It looks like you’re using the VSC add-on? Perhaps it is a bug with the editor flagging it as incorrect, or it is out-of-date?

I can confirm that my cameras setup like this are working… I have the configs in a little different ordering, here’s an example:

mqtt:
  camera:
      - topic: BlueIris/sidegate/alert-image
        name: Sidegate Alert
        unique_id: camera.bi_sidegate
        encoding: b64

@wjbeckett, this is fantastic! Love the Live View and View Clip in the notification. Only because I prefer having my notifications in ‘Node-Red’, do you know how to convert your automation into node red?, struggling to get the variables into the notify output.

thanks! yeah I am. I didn’t think it would let me save it, but it did, and it works…kind of. I get the alert now, but get an error instead of the picture about “HLS stream unavailable”. Not sure what that’s all about.

Edit:
I ended up upgrading from BI4 to BI5.
Now seems like everything is working as far as actually sending the image. I just need to do some tweaking on the HA side to get it how I want it.

Glad you got it working. Yes, BI5 adds a lot of new features!

I found that this setup works for me on my Android phone.

BI sends two MQTT messages on alert:

Topic: BlueIris/&CAM/alert

{ "id":"&ALERT_DB", "object":"&MEMO", "camera":"&CAM", "name":"&NAME" }

Topic: BlueIris/&CAM/alert-image

&ALERT_JPEG

This is how I’m sending the image, all of my cameras are named with a standard “camera.[BI short cam name]_alert:”, e.g. “camera.sidegate_alert”.

image: '/api/camera_proxy/camera.{{ trigger.payload_json.camera }}_alert

I get the picture whether on my home WiFi or away from home on cellular data (4G/5G). I do subscribe to Nabu Casa, which is how I think this works when off-net (?) as neither HA or BI is exposed to the Internet.

Here’s the full automation I use (based on @wjbeckett previous post), which in my case I only want to trigger between 1 hour after sunset and sunrise. I also use a specific notification channel “BlueIris” so that I can set a different notification sound for BI alerts on the HA Companion app (an annoying one that will wake me up if AI detects a person in the back yard late at night, for example):

- id: blueiris_notification
  alias: Blue Iris Notification
  description: ''
  trigger:
  - platform: mqtt
    topic: BlueIris/+/alert
  condition:
  - condition: sun
    after: sunset
    after_offset: 01:00:00
    before: sunrise
  action:
  - delay: 1
  - service: notify.mobile_app_[DEVICE NAME]
    data:
      message: A {{ trigger.payload_json.object.split(":")[0].strip('%') }} was detected
        on the {{ trigger.payload_json.name }} camera.
      data:
        channel: BlueIris
        ttl: 0
        priority: high
        clickAction: http://[NVR IP]:81/ui3.htm?maximize=1&tab=alerts&cam={{
          trigger.payload_json.camera }}&rec={{ trigger.payload_json.id }}
        image: '/api/camera_proxy/camera.{{ trigger.payload_json.camera }}_alert'

        actions:
        - action: URI
          title: Live View
          uri: http://[NVR IP]:81/ui3.htm?maximize=1&cam={{ trigger.payload_json.camera
            }}
        - action: URI
          title: View Clip
          uri: http://[NVR IP]:81/ui3.htm?maximize=1&tab=alerts&cam={{ trigger.payload_json.camera
            }}&rec={{ trigger.payload_json.id }}
  mode: queued

Hope this helps.

1 Like

Just sharing this automation in case others find it useful, I’ve got deepstack setup and a hotspot zone marking the path to my front door. This automation then checks if there’s a person in my drive and then sends a TTS message to my phone.

Usually this fires just before my doorbell automation which actually does send a camera snapshot to my phone with another TTS - I shared that somewhere earlier in the thread :slight_smile:

(no image notification in this one but if you want that you can copy-paste one of the example action sequences in this thread)

BI sends an entirely separate MQTT alert from the main camera image alert with the AI info.

BI Alert example:
MQTT Topic: BI/hotspot_info/camera1
Payload: { "alert_db": "&ALERT_DB", "alert_date": "&ALERT_TIME", "json": &JSON, "memo": "&MEMO", "type": "&TYPE" }

HASS Automation example:

alias: "[CameraAlert] Camera1"
description: ""
trigger:
  - platform: mqtt
    topic: BI/hotspot_info/camera1
condition:
  - condition: state
    entity_id: input_boolean.motionalert_front_drive
    state: "on"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set cam_data = 'json' in trigger.payload_json and trigger.payload_json.json %}
              {% set ai_data = (cam_data and (cam_data|length)) and cam_data|first %}
              {% if ai_data and 'found' in ai_data and 'success' in ai_data.found and ai_data.found.success and 'predictions' in ai_data.found %}
              {% if "person" in (ai_data.found.predictions | map(attribute='label', default="empty") | list) %}
              {{ true }}
              {% else %}
              {{ false }}
              {% endif %}
              {% else %}
              {{ false }}
              {% endif %}
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: >-
                      {% set memo_data = 'memo' in trigger.payload_json and trigger.payload_json.memo %}
                      {{ true if memo_data and 'person' in memo_data else false }}
                sequence:
                  - repeat:
                      count: "1"
                      sequence:
                        - service: notify.mobile_app_phone1
                          data:
                            message: TTS
                            title: Someone's coming sir
                            data:
                              media_stream: alarm_stream_max
                              importance: max
                              tts_text: Someone's coming sir
                        - delay:
                            hours: 0
                            minutes: 0
                            seconds: 10
                            milliseconds: 0
            default:
              - repeat:
                  count: "1"
                  sequence:
                    - service: notify.mobile_app_phone1
                      data:
                        message: TTS
                        title: Someone might be coming sir
                        data:
                          media_stream: alarm_stream_max
                          importance: max
                          tts_text: Someone might be coming sir
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 10
                        milliseconds: 0
mode: single

thanks!
yeah im doing something similar as @wjbeckett as well. Except I’m sending the image and a caption to telegram

service: telegram_bot.send_photo
data:
  url: http://<bi_ip>:<bi_port>/alerts/{{ trigger.payload_json.id }}&fulljpeg
  caption: >-
    A {{ trigger.payload_json.object.split(":")[0].strip('%') }} was detected on
    the {{ trigger.payload_json.name }} camera.

The other part im trying to figure out would be how to attach a 5 second clip. The old way I was alerting was straight from BI, and attaching 3 pictures and a video clip to an email. I would like to try to replicate that with homeassistant.

1 Like

I ended up getting videos sending to telegram working for anyone else that might be interested.

TL;DR - On alert, send a video to an FTP server that HA has access to (could use HA FTP add-on), use HA folder_watcher add-on to watch when the file has been modified, then use telegram_bot to send the video.

BI supports sending a video file via FTP on alert. The bummer is BI doesn’t support SFTP (although they do support FTPS). I didn’t love the idea of sending the video over plain text FTP (and couldn’t get FTPS working with the BI FTP add-on), so I spun up an FTP server on the BI host, and configure BI to use that local FTP server. Then I network share the FTP directory (on the BI host), and mount that share on my HA host at /bialerts.

Folder Watcher Config
Then I configured HA folder_watcher add-on (configuration.yml) to watch that directory:

folder_watcher:
  - folder: /bialerts
    patterns:
      - "*.mp4"

You will also need to add this to configuration.yml as well:

homeassistant:
  allowlist_external_dirs:
    - "/media"

Camera Alert Config
Then in BI i setup the alert like this:


this will create an mp4 file in the directory with the short name of the camera.

Automation Config
Then I setup my automation like this:

  - wait_for_trigger:
      - platform: event
        event_type: folder_watcher
        event_data:
          event_type: modified
          path: /bialerts/{{ trigger.payload_json.camera }}.mp4
      - platform: event
        event_type: folder_watcher
        event_data:
          event_type: created
          path: /bialerts/{{ trigger.payload_json.camera }}.mp4
    timeout:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: telegram_bot.send_video
    data:
      file: /bialerts/{{ trigger.payload_json.camera }}.mp4

This will watch for if an mp4 file is created or modified with the camera short name, with a timeout of 60 seconds, in my testing it takes my setup around 10 seconds to complete, but you might have to adjust this timeout as needed.

Thoughts
I dont have a lot of cameras, and dont get a lot of alerts, but so far correct video has been attached.

This could be simplified IF BI supported SFTP, OR if you dont care about sending video files over FTP, or setup FTPS server/got HA FTP addon working with FTPS, to be fair I didn’t spend much time on getting FTPS working.

I migrated my deepstack AI alert automation to a blueprint which is now here: BlueIris Deepstack AI Alert Confirmation

Feedback is welcome :wink: I tried to make it configurable for my needs and hopefully others too!

Just tried this with the latest HA and couldn’t get the mqtt camera to load the image until I realized 2 things:

  1. I have to actually trigger the camera instead of just using the test button on the “On Alerts” config dialog to generate an image
  2. the mqtt camera docs state that the camera should be set up with “image_encoding: b64”, not “encoding: b64” so my camera def looks like this:
mqtt:
  camera:
    - topic: BlueIris/gate/alert-image
      name: Gate Alert
      unique_id: camera.gate_alert
      image_encoding: b64
1 Like

Thanks for the update about needing image_encoding: b64 That must have changed since when I set this up, and I was just starting to investigate why my alert images stopped working.

I noticed that this changed when I upgraded to core 2022.12. I suddenly stopped getting images in my HA companion app alerts. I was using encoding: b64 and it was working fine under 2022.11.

Changed those to image_encoding: b64 and all was well again.

Thanks for pointing this out guys. I try to be diligent and read the release notes and breaking changes every single month but it’s easy to miss! Does seem mad to me how quickly hass deprecates and removes features but 🤷

@jbouwh thanks for this Christmas present :wink:

Hi there,

Thank you for sharing this.

The notification with the blue iris link worked perfectly for me, but I didn’t receive the image preview. My automotion is giving me the following error:

Stopped because an error was encountered at December 29, 2022 at 12:30:52 PM (runtime: 1.02 seconds)
Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘payload_json’

In this topic I can see that other people also got this error and it has something to do with backquotes, but I don’t understand what I did wrong.

this is how i set my blue iris topics config:

BlueIris/&CAM/alert

{ “id”:"&ALERT_DB", “object”:"&MEMO", “camera”:"&CAM", “name”:"&NAME" }

BlueIris/&CAM/alert-image

&ALERT_JPEG

@royle Use MQTT Explorer to inspect each alert event and check if the data is valid JSON.

Valid JSON should look like what’s in screenshot below. I suspect there is something in one of your variables that is breaking the JSON.

For example, don’t use &ALERT_JPEG temporarily to see if your ‘dict object’ error goes away.

1 Like