Today I’ll write an artice about getting push notifications on motion detection by a zoneminder camera. After configuring the system you’ll get a notification including a screen capture from the camera right after motion has been detected by zoneminder.

Prerequisites:

  • a running and functional zoneminder server with at least one camera
  • a working Home Assistant installation, that is accessible over https from the outside
  • a node red installation
  • MQTT broker (can be the one included in Home Assistant

If the prerequisites are not met it’s worth installing them. If you want to play around with IoT and home automation these are must haves.

The final setup wil work as follows:

  1. ZM detects motion and a script notices this and sends MQTT message
  2. MQTT broker forwards message to subscriber, in this case Node-RED
  3. Node-RED does some logic to activate an alarm on MQTT
  4. The MQTT broker forwards this alarm to the subscriber, Home Assistant
  5. An Mome Assistance automation is triggerd and sends a notification to Googles Firebase platform
  6. Firebase pushes the notification to all subscibers

Woow, a long sequence to get a message on your android phone, chromebook or chrome browser! But don’t fear, this sequence is handled much faster than it took you to read trough it.

How to get Zoneminder alarms published on MQTT?

Unfortunately there is no build-in feature in zoneminder to publish to a MQTT broker. However some other guy wrote a perl script that handles the mqtt part. It can be found on github: https://github.com/pliablepixels/zmeventserver.

The installation instructions on Github are complete, clear and up-to-date. So there is no need to repeat them here.

Because I use password protection on my MQTT broker I needed to alter the script a bit. Whenever I’m in the modd I’ll clean up my code and fork the project with that feature added.

To verify if the alarms are published to your MQTT broker you can use MQTT Spy and subscribe to ‘zoneminder/#’. Trigger an alarm on zoneminder and see if it appears in MQTT Spy. If it’s listed you’re ready for the next step!

Add some logic to the Zoneminder alarm with NodeRED

The script on Zoneminder publishes a message when an alarm is triggered. And that’s it.

You can use that message to trigger the notification but what I want, is an alarm that can be reset manually or automatically so it can be triggered again.
In RodeNED I made a sequence that takes in the MQTT message from zoneminder and forwards it to another MQTT topic. That topic contains an ‘On’ or an ‘Off’ state. It can be manually set to off by changing the topic on the MQTT broker or automatically by NodeRED after a certain delay.
When the alarm would not be reset a new alarm will not trigger the automation on Home Assistant later on.

ZM topic ___|_|________|________
NR topic ___|—-|_____|–|_____

Show the Zoneminder alarm in Home Assistant

To visualise the Zoneminder alarm state in Home Assitant we’ll use a MQTT switch. The switch will be turned on by the NodeRED sequence and can be turned off in Home Assistant or automatically by NodeRED after a defined delay.

Add a sitch in your configuration.yaml file (or where you put your other switches)

1
2
3
4
switch:
- name
: camera-alarm
  platform
: mqtt
  topic
: /camera/alarm

Restart Home Assistance and prepare for a test!

Trigger the alarm in Zoneminder and see if it reaches the Home Assistant switch.
You can turn of the alarm manually trough the user interface or just wait until the delay defined in NodeRED is over.

Trigger a push notification on you Android Phone or Chrome browser

The next step is to trigger a push notification on you Android Phone or Chrome browser when the alarm is activated.
To send push notifications over HTML5 you’ll need to asssign your home assistant page to a google project. How this is done is explained on the ==html5 notification page==.
I struggled a bit to get it running because of missing dependencies for the notification platform. The commands below made it work for me:

  1. Shutdown Home Assistant.
  2. If you use a virtual environment, activate it.
  3. Run the following commands:
    1
    2
    3
    sudo apt-get install libpython-dev libffi-dev libssl-dev
    pip uninstall pywebpush
    pip install "https://github.com/web-push-libs/pywebpush/archive/e743dc92558fc62178d255c0018920d74fa778ed.zip#pywebpush==0.5.0"
  4. Finally start up Home Assistant again.

Now crerate an automation in Home Assistant.

1
2
3
4
5
6
7
8
9
- alias: "Notify: Alarm achterdeur"
  trigger
:
    - platform
: state
      entity_id
: switch.alarm_achterdeur
      to
: 'on'
      action
:
      - service
: notify.html5
        data_template
:
          message
: "Achterdeur alarm om {{ now ().hour }}:{{ now ().minute }}."

Add a camera snapshot to the notification

Now you’ve got the notification working you can make it a little bit more fancy. The notification platform is able to add an image to the notification. Adjust the automation above to the the one below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- alias: "Notify: Alarm achterdeur"
  trigger
:
  - platform
: state
    entity_id
: switch.alarm_achterdeur
    to
: 'on'
    action
:
      - service
: camera.snapshot
        data
:
          entity_id
: camera.achterdeur
          filename
: "/home/homeassistant/.homeassistant/www/cam_captures/achterdeur_{{
               states.switch.alarm_achterdeur.last_changed|replace(' ','')|replace('+','')|replace(':','') }}.jpg"

     - delay
:
       seconds
: 4
     - service
: notify.html5
       data_template
:
         message
: "Achterdeur alarm om {{ now ().hour }}:{{ now ().minute }}."
       data
:
         image
: "https://pena.moers.be:5321/local/cam_captures/achterdeur_{{ states.switch.alarm_achterdeur.last_changed|replace(' ','')|replace('+','')|replace(':','') }}.jpg"

Now take a walk in front of your camera and watch yourself appear in the notification image!

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.