New ZoneMinder API

Absolutely. Super simple do with pyzm. Here is the full code that lists only events detected with objects from all monitors for the last 1 day. You can easily integrate this with a card.

import pyzm.api as zmapi

api_options = {
    'apiurl': 'https://yourserver/zm/api',
    'portalurl': 'https://yourserver/zm',
    'user': 'admin',
    'password': 'password',
    'logger': None # use none if you don't want to log to ZM
}

zmapi = zmapi.ZMApi(options=api_options)

event_filter = {
    'from': '1 day ago', # this will use localtimezone, use 'tz' for other timezones
    'object_only':True,
}
events = zmapi.events(event_filter)
for e in events.list():
  print ('Monitor:{} Event:{} Notes:{} Detected Frame URL:{}'.format(e.monitor_id(),e.id(),e.notes(), e.get_image_url(fid='objdetect')))
2 Likes

Is there an easy way of creating a sensor based on the mqtt data from zoneminder… I’m thinking a person detected sensor that simply shows the time and date the last time a person was detected… it could work like motion sensors showing no detection by default and detected when a person is seen. I’m new to mqtt so feel a bit lost here

Sure is, look at the mqtt sensor config and the mqtt payload from a zm event. I don’t have access to my exact code to share

I provided an example of a sensor that does what you request earlier in this discussion.

The mqtt topics are enabled via configuring the ES (event server). The topic is based on the monitor id e.g. 1 in my example. I break out the event id field, you can use this as a trigger given zoneminder increments the event id as it generates motion events.

HTH

1 Like

I cracked it in the end by writing an app for the app daemon. I set up a virtual pir sensor and powered it using my code so it came “on” for 30 seconds every time a person is detected… I posted about it under the configuration topic on here

Hi

I installed dlandon’s docker zomeminder yesterday and I’ve enabled mqtt in the zmeventnotification.ini

I was wondering if there is an easy way to trigger a ‘force alarm’ event on a monitor to trigger a recording in ZM from home assistant?

I’d like to use home assistant sensors to decide when to record video, rather than using the motion detection in zoneminder.

Thanks
Mike

Edit - I found this but I’d like an easier method :smiley:

Edit 2 - I think this might be the way to go. --> https://gist.github.com/swilson/89096c289d07fdf7d5d92796354a7b19

This looks interesting, can you tell me where to put this file or point me to a doc? Thanks

I have dlandon’s ZM docker image running on the same server as my docker image of HA. I’ve enabled @pliablepixels ES and have created HA sensors for each of my cameras:

- platform: mqtt
  state_topic: "zoneminder/1" #driveway
  name: driveway_camera_object_detection
  value_template: "{{ value_json.eventid }}"

Although I see a lot of work and success with AppDaemon, I have not yet had to install it and am hoping to be able to avoid this. What I would like is to be able to push the picture from an event as a notification.

Reading the ES documentation, I found that the URL:
https://<IPADDRESS>:<PORT>/zm/index.php?view=image&eid=<EVENTID>&fid=objdetect
gives you the picture corresponding to the event_id pushed to the MQTT sensor. I have tried to create a simple bash script to either cURL or wget this image, but have had no success.

Is there an easy way to download the image without having to rely on the pyzim library? Is there no way with either cURL or wget to grab the image?

Yes here’s how I do it.

# Zoneminder Camera Processing

- alias: Front Door Object
  trigger:
    entity_id: sensor.front_door_event_id
    platform: state

  action:
    - service: downloader.download_file
      data_template:
        url: https://<ZM IP>:8443/zm/index.php?view=image&eid={{ states('sensor.front_door_event_id') }}&fid=objdetect
        filename: front_door_latest.jpg
        overwrite: true

    - service: downloader.download_file
      data_template:
        url: https://<ZM IP>:8443/zm/index.php?view=image&eid={{ states('sensor.front_door_event_id') }}&fid=objdetect
        filename: front_door_{{ now().strftime('%Y%m%d_%H%M%S') }}.jpg

# End Zoneminder Camera

Thanks! It brought me closer, but I get errors in my HA logs as ZM is using a self-signed certificate. It seems the downloader integration chokes on these…

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 362, in connect
    self.sock = ssl_wrap_socket(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/ssl_.py", line 397, in ssl_wrap_socket
    return context.wrap_socket(sock)
  File "/usr/local/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/local/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/usr/local/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1123)

I use my own CA and sign my own internal certs, so I have those added each HA container update.

Hadn’t thought of that. Do you automate this? Is there an easy way to do this?

The CA is manual as is the importing of the CA cert into the container :frowning:

I was able to edit the Downloader integration so that it did not balk at self-signed certificates. The file in question is /usr/src/homeassistant/homeassistant/components/downloader/__init__.py. Line 78, change req = requests.get(url, stream=True, timeout=10) to req = requests.get(url, stream=True, timeout=10, verfiy=False). When you restart HA, all will be good.

I was going to see if I could figure out how to put in a PR, however this did not solve my problem as the file downloaded from https://<ZMIP>:<ZMPORT>/zm/index.php?view=image&eid=<eventID>&fid=objdetect was the html login form, not an image. For whatever reason, HA was not authenticated against ZM.

Looks like I am going to have to go the AppDaemon route–thanks for your help!

I don’t have logins setup for my zoneminder instance.

1 Like

Well that was too obvious, thanks! I will look into how to go about submitting an official PR to the Downloader integration to ignore self-signed certs.

Can anyone help me with getting ES sending MQTT messages, I’m using the dlandon docker container, ZM is working correctly, the ES is also running but I’m not getting any MQTT messages from events so I’m guessing it’s my zmeventnotification.ini that’s wrong.

My docker container is set to use port 2080>9000
Do I need to change that in the ini to 2080 or leave it as 9000?

I have
push off
FCM off
MQTT on, server set to 192.168.1.3 (do I need the port)
No MQTT username or password
SSL set to off
Hooks set to off

I must be missing something or doing something wrong here.

Hi, my reply could be late, but still better than never :slight_smile:
As workaround (not a good from security point of view, since credentials will be part of URL) You can get the picture from ZM by providing credentials in the URL
https://<ZMIP>:<ZMPORT>/zm/index.php?view=image&eid=<eventID>&fid=objdetect& username=<ZMUser>&password=<ZMPass>&
I think it’s acceptable if HA and ZM servers are not available from outside.

I run hassio in a docker container and the dlandon ZM container. I was having similar issues of not receiving mqtt messages. The issue (I think) was the event server wasn’t able to connect to the MQTT broker.

Pretty sure I fixed it by adding the dlandon ZM container to the hassio docker network.

My current issue is i’m receiving the mqtt messages when an event happens but they’re very generic messages. They appear to come from the default zone setup in ZM and not any sort of object detection. Also none of the events in ZM have anything related to object detection.

In zmeventnotification.ini - i’m not using ssl. I have auth enabled, port 9000, fcm enable, mqtt enable,

I’m using the dlandon container with the following environment variables

    ports:
      - 8089:80
      - 9089:9000
    environment:
      - TZ=America/New_York
      - SHMEM=60%
      - PUID=99
      - PGID=100
      - INSTALL_HOOK=1
      - INSTALL_FACE=1
      - INSTALL_TINY_YOLOV3=0
      - INSTALL_YOLOV3=1
      - INSTALL_TINY_YOLOV4=0
      - INSTALL_YOLOV4=0
      - MULTI_PORT_START=0
      - MULTI_PORT_END=0

this is what one of the alerts look like.

{
  "hookvalue": "1",
  "state": "alarm",
  "name": "Front Door Sub:(7419) Motion All",
  "monitor": "3",
  "detection": [],
  "eventtype": "event_start",
  "eventid": "7419"
}

Any ideas what i’m missing that would prevent object detection from working?