Detecting motion from camera and triggering automation - using enable_motion_detection service

Description of camera component states that there is enable_motion_detection service.

It’s so confusingly described, that I honestly don’t know how to use it :slight_smile: I can open services section on HA configuration page, and then enable the service, pointing to my camera as an entity ID. Yet, it does nothing. The state of camera entity never changes from ‘idle’, even if there is motion. I was hoping maybe it will enable an attribute for that camera, but that didn’t happen. I’m wondering, what’s the point of this service? How exactly should it be used?

Currently I have generic IP MJPEG camera up and running with HA. I’d like to trigger an automation whenever HA detects motion from that camera.

3 Likes

goin the States section and you should see a sensor.name_of_your_camera

Thanks for reply! Unfortunately, such sensor doesn’t appear. But I noticed now that firing enable_motion_detection throws an error in the log:

2018-06-03 14:03:13 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall camera.enable_motion_detection: entity_id=['camera.kamera_wejsciowa']>
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1007, in _event_to_service_call
    await service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/camera/__init__.py", line 165, in async_handle_camera_service
    yield from camera.async_enable_motion_detection()
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/camera/__init__.py", line 342, in enable_motion_detection
    raise NotImplementedError()
NotImplementedError

after further reading, it seems that enable_motion_detection is just a service, so it just turns motion detection on or off if your camera has it, but there’s no documentation on how to have this as a sensor

1 Like

Yeah, documentation was rather unclear, hence my question.

In that case the way to do it is to either have PIR sensor in conjunction with the camera, or some software on the camera itself capable of communication with HA whenever it detects motion (either through enable_motion_detection service, which isn’t well documented, or in some other way)

Luckily, my generic MJPEG IP camera is powered by motion (Linux software), which has a function called on_motion_detected to run desired script when… motion is detected. This way I can make it post MQTT messages and setup MQTT Binary Sensor in HA that will listen for them.

It would be great to have some image processing component for HA capable of detecting motion from selected camera entity. Similar to Facebox component, that was recently added, but a lot simpler - just for detecting motion. That way this function would be a bit easier to setup.

Thanks for help!

1 Like

if you run motioneye, then yes, use the built in motion detection and create a sensor using mqtt

1 Like

In the navigation on the left, go to “Developer Tools”, click, “States”, under “entity”, locate your camera and select it. Under “Set Attributes” add the following text:

motion_enabled: true

Lastly, click “Set State”.

You will now see that attribute under the camera set to true. You can also go to the template tab and enter this:

"{{ states.YOUR-CAMERA-NAME.attributes.motion_enabled }}"

You will see to the side if motion is enabled

2 Likes

thats great. how to use this to create an automation for when motion is detected?

1 Like

did you get any luck whit this?
I followed a guide to setup the software “motion” on a rpi whit a rpi camera module, I get the stream into home assistant but I have no idea how to trigger a recording on motion detection.

1 Like

hi, did you get this to work?