Motioneye integration

Hi everybody,

I’m so excited of everything I’ve done today! Home Assistant is a great tool for automation, and has endless possibilities.

I want to document here the integration I made between Home-Assistant and Motioneye.

The goal is the following: turn on and off motion detection with Motioneye from Home Assistant, and get an entity indicating when a motion was detected on camera:

image

I’ll show the example with 1 camera, but it’s easily adaptable to many cameras.

Here are the steps to achieve this:

Step 1: Create your cameras in Motioneye

Here, the important setting is “Motion Gap”: that’s the time the sensor will keep on “Detected” after the motion has finished.

Step 2: Create boolean inputs for each camera, for when motion are detected

For each camera, I will have a boolean input to indicate if a motion was detected or not. This field should not be modified manually, since it will be managed by Motioneye in a later step.

input_boolean:
  camera1_motion_detected:
    name: Camera1 - Motion detected
    icon: mdi:eye

Step 3: Create webhooks, to manipulate this boolean

I create two automations, manipulated by webhooks. Those two hooks will change the input declared above:

automation:
- alias: "Motioneye - Camera1 - Motion started"
  trigger:
    platform: webhook
    webhook_id: motioneye-camera1-started
  action:
    service: input_boolean.turn_on
    data:
      entity_id: input_boolean.camera1_motion_detected
- alias: "Motioneye - Camera1 - Motion ended"
  trigger:
    platform: webhook
    webhook_id: motioneye-camera1-ended
  action:
    service: input_boolean.turn_off
    data:
      entity_id: input_boolean.camera1_motion_detected

Step 4: Declare those webhooks in MotionEye

In the configuration of MotionEye, in the section “Motion notification”, I configure the following commands:

# Run a command (when motiton detected)
curl -XPOST http://home-assistant:8123/api/webhook/motioneye-camera1-started

# Run An End Command
curl -XPOST http://home-assistant:8123/api/webhook/motioneye-camera1-ended

CHECKPOINT

Here, I have configured an input boolean that will be manipulated by MotionEye.

Let’s create a binary sensor from this input now.

Step 5: Declare a template sensor

A template sensor is based on other entities state, so you can quickly create this

binary_sensor:
  - platform: template
    camera1_motion:
      friendly_name: "Camera - Mouvement"
      device_class: motion
      value_template: "{{ is_state('input_boolean.camera1_motion_detected', 'on') }}"

Now, let’s imagine you have tons of cameras. You can easily “sum them up” by doing:

binary_sensor:
  - platform: template
    all_cameras_motion
      friendly_name: "All cameras - Motion"
      device_class: motion
      value_template: |
        {{
            is_state('input_boolean.camera1_motion_detected', 'on')
            or is_state('input_boolean.camera2_motion_detected', 'on')
            or is_state('input_boolean.camera3_motion_detected', 'on')
            ...
        }}

You could also create multiple sensors, depending on your needings.

CHECKPOINT

I now have a fancy icon, and can integrate our motion detection in other automations easily.

But, when I’m home, I don’t want the motion detection to be enabled.

Now, let’s see how it’s possible to make this motion detection enabled or disabled, from Home-Assistant to Motioneye.

Step 6: Enable motion API

Adapt configuration in /etc/motioneye/motion.conf:

# We will need this port, so remember it
webcontrol_port 7999

# Set to off if you want to access it from a different machine (BEWARE! This API is not secured!)
webcontrol_localhost off

After changing this configuration, restart MotionEye to apply changes.

Step 7: Create REST commands

Inside Home Assistant, in configuration, create the REST commands:

rest_command:
  camera_motion_start:
    url: 'http://motioneye:7999/1/detection/start'
  camera_motion_pause:
    url: 'http://motioneye:7999/1/detection/pause'

Those commands will be used later in an automation.

Step 8: Create an input to turn motion detection on and off

This input will be used to turn on and off the motion detection. For example, I have a scenario saying: when I leave home, activate the motion detection.

input_boolean:
  camera_motion_detection:
    name: Motion detection
    icon: mdi:eye

Step 9: Automations for value changing

Now, let’s combine the REST commands with the input, using two automations:

automation:
- alias: "Enable motion detection"
    trigger:
      - platform: state
        entity_id: input_boolean.camera_motion_detection
        to: 'on'
    action:
      service: rest_command.camera_motion_start
  - alias: "Disable motion detection"
    trigger:
      - platform: state
        entity_id: input_boolean.camera_motion_detection
        to: 'off'
    action:
      service: rest_command.camera_motion_pause

CONCLUSION

This is a perfect example of integration/communication between Home Assistant and MotionEye, showing all the possibilities you have with those tools.

If you see any possible improvement, or have any comment to make, don’t hesitate!

32 Likes

Very nice! Just The kind of step-by-step abstract that I need to get started. Thanks!

That is so awesome! Thanks for taking all the time and sharing!

Only problem for me seems to be that I don’t have access to /etc/motioneye/motion.conf. I guess it is because I’m running HassOS directly on my NUC?

Ho, sorry, I’m using some docker images, not HassOS.

It seems you can enter the container in root using Portainer : Protip: How to get shell in actual homeassistant (or addon) container when using Hassio

I can’t really help here, since I don’t have experience on this.

If you are using the motion addon you can just change the web access setting in the addon config. This is the same as changing the webcontrol value in the motion.conf

2 Likes

Beautiful - so that solves the issue then. Thanks a lot for sharing!

No problem. I’m always getting great advice from these pages, its great to be able to help someone for once

1 Like

Hi Alexandre,

Thank you for the detailed guide. Some questions below:

  1. Step 2: Create boolean inputs for each camera, for when motion are detected
    In which file do you create this?

  2. Step 3: Create webhooks, to manipulate this boolean
    In which file do you create this? when I open my automations.yaml, there is a line stating the id of the record first like: - id: ‘1584914490976’

  3. Step 5: Declare a template sensor
    The same as above

Actually can you please inform me of on which file are you adding every conf?

configuration.yaml

configuration.yaml

1 Like

Hi I’m getting this error:

Invalid config for [binary_sensor.template]: [camera1_motion] is an invalid option for [binary_sensor.template]. Check: binary_sensor.template->camera1_motion. (See ?, line ?).

It seems that this code is not working:

  - platform: template
    camera1_motion:
      friendly_name: "Camera - Mouvement"
      device_class: motion
      value_template: "{{ is_state('input_boolean.camera1_motion_detected', 'on') }}"
      

Look at the docs for the template sensor :wink:

binary_sensor:
  - platform: template
    sensors:
      camera1_motion:
        friendly_name: "Camera - Mouvement"
        device_class: motion
        value_template: "{{ is_state('input_boolean.camera1_motion_detected', 'on') }}"

You were missing the sensors: line

2 Likes

Thanks. I tried that too, here is the error message:

Invalid config for [binary_sensor.template]: expected dictionary for dictionary value @ data['sensors']. Got None
extra keys not allowed @ data['camera1_motion']. Got OrderedDict([('friendly_name', 'Camera - Mouvement'), ('device_class', 'motion'), ('value_template', "{{ is_state('input_boolean.camera1_motion_detected', 'on') }}")]). (See ?, line ?). 

Have you tried the template in dev-tools to see if it gives the expected result?

How do I do step 6?

With an editor.

I can’t find the conf file anywhere.

How did you install motioneye?

From the add on store

See this post Motioneye integration

Hi, I got no motioneye folder there … :confused: