MotionEye, home assistant, camera motion, automation

Sure, here you go:
Go to Motioneye and open Motion notifications
Turn on call a web hook
on the Web Hook URL use this:

http://192.168.1.3:8123/api/services/script/camdetecon?api_password=1234

Note this ~script/camdetecon?api~
This should be the same name as your script!

Select the POST (query) on the HTTP Method field.

Make sure you change to you own homeassistant ip and use your own password. If you havent set a password for homeassistant web interface, then do it by following this guide here:

All the above are just half way for setting up the whole thing. What you just did was have Motioneye notify the homeassistant when there is motion detected. Now you are going to setup homeassistant to do some automations when motion is detected by motioneye.

So:
You need a script which will do some actions when motion is triggered:
open scripts.yaml

camdetecon:
  alias: Camera Motion Detection ON
  sequence:
  - data:
      data:
        method: alarm
      message: chime
    service: notify.jarvis_lenovo

I have a Lenovo tablet and I have lannouncer setup on it. This enables me with the cool feature to have a text announced (like: Sir, Someone is outside.) or have a chime sound. You can do whatever.

If interested, here is the lannouncer setup:

A tip:
Go to Motioneye and then go to Motion Detection and change the Motion Gap to 30 or maybe 60 seconds. That way you wont have constant notifications when someone is just ringing your doorbell waiting for you to open the door.

I hope everything is clear. Feel free to ask anything.

Edit:
I simplified the thing by just doing everything inside the script. If you want, you can have a boolean turn on and off by using the script. Then you can have an automation that will read the boolean state and do anything when the boolean turns on.
I just thought the script can do all I want, so there is no need for me to have a boolean and an automation.

8 Likes

How can motion eye to simply trigger a state change with detected motion? ā€¦I mean without the script and allā€¦ ideally MQTT seems like the way to go, I just canā€™t seem to make it work with the codes posted. What am I missing?

Iā€™ve tried your setup and it doesnā€™t seem to work for me since I donā€™t have a notify.jarvis_lenovo.

If HA can see the state change in developer tools, I can figure out how to make HA do automations, I just canā€™t seem to pass the detected motion to HAā€¦

Any help is appreciated.

Thanks

Notify.jarvis_lenovo is just a name I used for my tablet. If you read the lannouncer guide you will see the use for it. Scripts are like automations.
Motioneye can trigger the script.

It looks like you will feel much better if you SEE the state change in the developer options. Ok here is what you can do:
Open your configuration.yaml and add this:

input_boolean:
  cameramotion:
    initial: off

next step:
Open scripts.yaml and paste this:

camdetecon:
  alias: booleanopen
  sequence:
  - service: input_boolean.turn_on
    entity_id: input_boolean.cameramotion
  - delay: 00:00:30
  - service: input_boolean.turn_off
    entity_id: input_boolean.cameramotion

With that you will be able to have an entity where you can use in your automations. The entity you are looking for will be with the name cameramotion
Let me know how it goes.

2 Likes

Hmmm that didnā€™t work eitherā€¦
ā€¦maybe my issue is coming from the text input into motioneyeosā€™s webhook inputā€¦?

hereā€™s what have:
http://192.168.1.XX:8123/api/services/script/camdetecon?api_password=My_Password

HTTP Method: POST (query)

input_boolean.yaml
cameramotion:
initial: off

scripts.yaml
camdetecon:
alias: booleanopen
sequence:

  • service: input_boolean.turn_on
    entity_id: input_boolean.cameramotion
  • delay: 00:00:10
  • service: input_boolean.turn_off
    entity_id: input_boolean.cameramotion

am I forgetting something?

thanks for your help!

It looks ok to me. One question. You have a file named input_boolean.yaml?
If yes, then you need to define this file in the configuration.yaml, right?
Also, use three back-ticks ` to have the correct formatting.
do it like this:

input_boolean:
  cameramotion:
    initial: off

io

So please post your configuration with the correct formatting to see what is wrong. Also show me the part where you setup the input_boolean in the configuration.yaml file.
The setup I posted, should workā€¦

Did you make any progress? does it work?

im surprised you couldnt get it to work with my configuration its easy. I would also check motioneye settings to see if it registers motion, if not play with the motion settings such as mask and motion gap etc. If you have everything configured with mqtt how mine is shown, you wouldnt use a webhook. Also with mine the password is just letters and numbers no fancy characters. if you need help with my setup let me know ill walk you through it. i had it setup to show the cams on home assistant but i no longer use that since i just view it from motioneye browser gui.

Yeah I gave up given my luckā€¦
I would appreciate a detailed walkthrough.
Thanks for doing this!

Update

Authetication: Bearer YOUR TOKEN
to create token visit general settings in home assistant and scroll to bottom of page

Hassbian

First create camera in motioneyeos, once it is visible and motion is enabled copy and paste these commands into the area shown in image below:
curl
commands:`
ON

curl -X POST -H ā€œAuthetication: Bearer YOUR TOKENā€ \ -H ā€œContent-Type: application/jsonā€ \ -d ā€˜{ā€œstateā€: ā€œonā€, ā€œattributesā€: {ā€œfriendly_nameā€: ā€œupstairscamā€}}ā€™ \ http://remoteiptohomeassistant:8123/api/states/binary_sensor.upstairscam

OFF

curl -X POST -H ā€œAuthetication: Bearer YOUR TOKENā€ \ -H ā€œContent-Type: application/jsonā€ \ -d ā€˜{ā€œstateā€: ā€œoffā€, ā€œattributesā€: {ā€œfriendly_nameā€: ā€œupstairscamā€}}ā€™ \ http://remoteiptohomeassistant:8123/api/states/binary_sensor.upstairscam

NO SPECIAL CHARACTERS FOR PASSWORD ONLY UPPER AND LOWER CASE LETTERS ALONG WITH NUMBERS FOR EXAMPLE:
HAssBian101

create this in your hassbian homeassistant configuration.yaml
sudo su -s /bin/bash homeassistant
cd /home/homeassistant/.homeassistant
nano configuration.yaml
binary

TO CREATE YOUR OWN JUST CHANGE THE NAME IN THE FRIENDLY NAME TO THE SAME NAME YOU USE AS YOUR BINARY SENSOR along with camera name in motioneyeos as state topic..

you should now see this after restarting homeassistant:
home

Now add automation

7 Likes

hopefully that helps

how did u configure the motion trigger? I detects like ghost motion all the time :frowning:

i think you change the minimum motion frames.

Hey, @argykaraz

Thank you so much for the idea of using my favourite Home Automation platform and my least disliked CCTV Framework (liked would be an overstatement). I had never thought of using them together but hereā€™s what Iā€™ve done with your ideaā€¦

I have set the ā€˜commandā€™ to:

    mosquitto_pub -h 192.168.0.3 -t cameras/motion/pats -u john -P mypassword -m ON

and my ā€˜end commandā€™ to:

    mosquitto_pub -h 192.168.0.3 -t cameras/motion/pats -u john -P mypassword -m OFF

Screenshot%20from%202018-05-03%2019-55-25

I am not a fan of putting my password in things ie putting my mosquitto password in motioneye but needs must (a program that stores itā€™s own admin passwords etc in plain text).

In Home Assistant I have some binary_sensors set up eg

    platform: mqtt
    name: "motion (drive)"
    state_topic: "cameras/motion/drive"
    payload_on: "ON"
    payload_off: "OFF"
    device_class: "motion"

That obviously controls the binary_sensor and sets it up as a motion detector.
Screenshot%20from%202018-05-03%2019-13-39

After that was done I changed my image settings in Motioneye to make sure that it was creating an image when motion was detected.
Screenshot%20from%202018-05-03%2019-58-18

I then turn to Node-RED to do some jiggery-pokery. In Node-RED I now have thisā€¦

which is:

[{"id":"a44c8cc5.3106c","type":"mqtt in","z":"839343ea.c64e3","name":"Drive Camera","topic":"cameras/motion/drive","qos":"2","broker":"2c553bf7.b54c64","x":100,"y":100,"wires":[["351a3f7.b860ec"]]},{"id":"351a3f7.b860ec","type":"switch","z":"839343ea.c64e3","name":"Motion ON or OFF","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":100,"wires":[["963127ea.8d9638"],["cd591b63.543c88"]]},{"id":"cd591b63.543c88","type":"exec","z":"839343ea.c64e3","command":"find /CCTV/Drive -type f -iname '*.thumb' -printf \"%T@ %p\\n\" | sort -n | cut -d' ' -f 2- | tail -n 1","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Get the final video thumbnail","x":580,"y":140,"wires":[["48645b4c.3d1714"],[],[]]},{"id":"1410272c.30db59","type":"fs-ops-copy","z":"839343ea.c64e3","name":"Copy to the HA dir","sourcePath":"","sourcePathType":"str","sourceFilename":"payload","sourceFilenameType":"msg","destPath":"/home/john/.homeassistant/www/assets/images/cctv","destPathType":"str","destFilename":"drive.jpg","destFilenameType":"str","link":false,"overwrite":true,"x":1200,"y":80,"wires":[[]]},{"id":"963127ea.8d9638","type":"exec","z":"839343ea.c64e3","command":"find /CCTV/Drive -type f -iname '*.jpg' -printf \"%T@ %p\\n\" | sort -n | cut -d' ' -f 2- | tail -n 1","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Get the initial image","x":550,"y":60,"wires":[["48645b4c.3d1714"],[],[]]},{"id":"48645b4c.3d1714","type":"string","z":"839343ea.c64e3","name":"Get rid of the trailing newline","methods":[{"name":"trim","params":[]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":910,"y":80,"wires":[["1410272c.30db59"]]},{"id":"2c553bf7.b54c64","type":"mqtt-broker","z":"","name":"Main","broker":"192.168.0.3","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Let me know if need any help with the modules required etc.

So, that then copies the initial image to the www subdir of the HA filesystem and then the final thumbnail once the motion event has finished.

That means that I can then create a camera in HA withā€¦

    platform: local_file
    name: "drive_motion"
    file_path: "/home/homeassistant/.homeassistant/www/assets/images/cctv/drive.jpg"

What does that mean?

It basically means that I can delete the 5 or 6 cameras that I have on my HA overview and replace them with the up to date image. This should save on quite a few resources.

Watch out for a new video on this

Because of the advantages of this over having 5-6 cameras using the traditional method. I am pretty sure I will be doing a video on this on the HASSCASTS YouTube Channel tomorrow.
http://YouTube.com/hasscasts

OK: So if anybody has any questions about this then please donā€™t hesitate to give me a shout :slight_smile:
John

27 Likes

i think you have your password posted on the off topicā€¦

Thanks very much for pointing that out. What a moron! I canā€™t believe I changed it in the first one and not the second one :-/

I will now change my password but luckily I donā€™t have external access anyway; all my ports are closed etc :slight_smile:

Thanks again for pointing it out, it is very much appreciated.
John

1 Like

Wow! Very detailed and informative. I am glad I gave you that push you needed. Great job, but I am not using mqtt. It will sure be helpful for someone else!

Hi @HassCasts
I was working through your solution and when I open my motioneye notifications section I only have one field for ā€œRun a commandā€ and canā€™t figure out how to get the ā€œRun an End commandā€ field to show up. Is there a trick to getting the second field to show up? Thanks for any help you might be able to provide.

Nevermind @HassCasts I found my problem, Iā€™m running Motioneye in Docker and must have pulled down an older version. I grabbed another version and it has both fields available. Now to get this all working :smiley:

thank you very much for this guide. small question. Im running mosquitto in docker.
how do I run the command in docker as my base os doesnt have mosquitto.

Ok, so the Motioneye ccrisan/motioneye:dev-amd64 docker image does not have the mosquitto client. So this (straight mqtt) method wont work on docker yet.

@sycohexor method does work.

Thanks for your input on this. I have created both an input_boolean and a script to fire (and reset) the innput_boolean under hass.io 0.73.0 and I can test both successfullly using the following API calls using POSTMAN in Chrome without API passwords. I do have any internal calls whitelisted though under my HTTP configuration (trusted_networks:)

http://10.0.x.x:8123/api/services/input_boolean/turn_on?entity_id=input_boolean.motion_entrance (works)

http://10.0.x.x:8123/api/services/script/trigger_ss_entrance_boolean (works)

Iā€™m trying to call mine from Synology Surveillance Station however and this is not working despite the forum chatter saying that the Synology action rule wizard defaults to sending POST requests. More head scratching required :slight_smile: