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:
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!