MotionEye, home assistant, camera motion, automation

Sorry
I’m a newbi an I’m a little bit confused about setting motioneye hassio’s addon to make motion notification working…
I’ve tried hassCast setting (i routinely use mqtt via mosquitto broker addon to control switch sensor ecc.) with no luck… I think it’s not possible to send mqtt_publish command with motioneye addon on HASSIO.
Then i tried with a webhook…I’ve setup IFTTT integration, than I’ve copied the right URL (https://mydomain:8123/api/webhook/TOKENfromIFTTintegration/services/script/camdetecon
than i create a script like this MotionEye, home assistant, camera motion, automation but my script never run.
What am I doing wrong?

Has anyone experienced an issue in motion eyes where you can only send a command on motion without the option of a individual on command and a off command?

Hopefully this’ll help someone.

I’m using “…/api/services/homeassistant/turn_on” & “turn_off” to control an “enitity_id”, in my case an “input_boolean”. However, that could equally be a: script / switch / automation /…

So my ‘on’ command is:

curl -X POST http://192.168.*.*:8123/api/services/homeassistant/turn_on -H 'Authorization: Bearer [TOKEN]' -H "Content-Type: application/json" -d '{"entity_id": "input_boolean.motion_frontcam"}'

and ‘off’ command is:

curl -X POST http://192.168.*.*:8123/api/services/homeassistant/turn_off -H 'Authorization: Bearer [TOKEN]' -H "Content-Type: application/json" -d '{"entity_id": "input_boolean.motion_frontcam"}'
3 Likes

I tried this but it didn’t work for me, what does is:

for ‘on’:

curl -X POST -H "Authorization: Bearer [TOKEN]" -H "Content-Type: application/json" -d '{"state": "on", "attributes": {"friendly_name": "Motion Frontcam"}}' http://192.168.*.*:8123/api/states/binary_sensor.motion_frontcam

and for ‘off’:

curl -X POST -H "Authorization: Bearer [TOKEN]" -H "Content-Type: application/json" -d '{"state": "off", "attributes": {"friendly_name": "Motion Frontcam"}}' http://192.168.*.*:8123/api/states/binary_sensor.motion_frontcam

I found an other method of creating a binairy sensor by use of a php-script.
Name it for example motion.php and put it on your web-server in a directory phpscript.

http://SERVER-IP/phpscript/motion.php?sensor=camera0&name=Motion Camera 0&state=on

Will create a binary_sensor with the name “camera0” and friendly name “Motion Camera 0”
and sets its state to “on” and will switch to “off” after 10 seconds.
Here’s the php-script:

<?php
// usage
// http://SERVER-IP/phpscript/motion.php?sensor=camera0&name=Motion Camera 0&state=on
// Will create a binary_sensor with the name "camera0" and friendly name "Motion Camera 0"
// and toggles its state from off to on and off again
//
echo $_SERVER['HTTP_HOST'];
echo '<br>';
// Set variables from cmd parameters
$options = array(
  'sensor' => isset($_GET['sensor']) ? $_GET['sensor'] : false,
  'name' => isset($_GET['name']) ? $_GET['name'] : false,
  'state' => isset($_GET['state']) ? $_GET['state'] : false,
);

// Remove empty values
$options = array_filter($options);

// Quit if not exactly 3 get values were found
if (count($options) != 3) {
  echo 'invalid options';
  die;
}

$headr = array();
// $headr[]="x-ha-access: PASSWORD";
$headr[]="Authorization: Bearer TOKEN";
$headr[]="Content-Type: application/json";

// See https://home-assistant.io/components/binary_sensor.http/#examples
// for more information about binary_sensor

// Send POST ON command, this will create a sensor with then name "sensor" and friendly name "name" 
// and set the state to on

curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "http://$_SERVER[HTTP_HOST]:8123/api/states/binary_sensor.$options[sensor]",
  CURLOPT_POST => true,
  CURLOPT_HEADER => false,
  CURLOPT_HTTPHEADER => $headr,
  CURLOPT_POSTFIELDS => "{\"state\":\"$options[state]\", \"attributes\": {\"friendly_name\": \"$options[name]\"}}"
));
curl_exec($ch);
curl_close($ch);

echo '<br>';
// Sleep for 10 seconds
sleep(10);

// Send POST OF command
curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "http://$_SERVER[HTTP_HOST]:8123/api/states/binary_sensor.$options[sensor]",
  CURLOPT_POST => true,
  CURLOPT_HEADER => false,
  CURLOPT_HTTPHEADER => $headr,
  CURLOPT_POSTFIELDS => "{\"state\":\"off\", \"attributes\": {\"friendly_name\": \"$options[name]\"}}"
));
curl_exec($ch);
curl_close($ch);
echo '<br>';

?>

Replace TOKEN, in the php-script, with your generated long lived token.

These are the ‘Commands’ to use in MotionEye’s Motion Detection settings to send ‘state’ commands back to Home Assistant.

  • In this case, they send API state commands to ‘turn on’ & ‘turn off’ an ‘input_boolean’ named 'motion_frontcam’.

Ah sorry, thought i was beeing helpfull.
Ignore these posts please…

Not at all… All, info / scripts / codes ARE helpful.

Never be afraid to share man.

1 Like

well this works!

but I was wondering if someone has a working url for mqtt instead of input boolean.

I tried to adapt yours but without success

1 Like

I am not an mqtt expert, but I guess you CAN use mqtt publish command instead of the curl command. Just replace Run a command with the same command you would use if you ran mqtt_publish in a terminal. I am not sure about the commands though, but I think it should work. Give it a try!

well I studied on mqtt service page and tried to adapt a command

curl -X POST http://192.168.2.100:8123/api/services/mqtt/publish -H ‘Authorization: Bearer xxxxxxxxxxxxxxx’ -H “Content-Type: application/json” -d ‘{“payload”: “MOTION”, “topic”: “cameras/motion/dafang”, “retain”: “True”}’

but it doesn’t work

Will this curl command work on Hassio?

Why not use the solution discussed in post 34? MotionEye, home assistant, camera motion, automation

Indeed. That is the solution I used and it works great!!!

HTTPS

I know this is a late reply lol, but I used the curl command with the no check certificate like with wget and it worked by adding -k to the beginning of the arguments. Curl -k for example, if anyone’s having issues with trailing video on cam from motion check you capture frames before motion, I had mine at 500 then dropped it to 100 or lower and didn’t have the issue any longer.

It doesnt matter now, as I am using long-lived tokens.
See this earlier post:

So, if you were using a webhook to call the script, now this is doable using a command_on and command_off. You just have to create a long-lived token.

However, your info can be useful! Thank you for sharing!

1 Like

yeah i think everyones probably changed the authentication method by now or using mosquitto, anyways nice hearing from you again!

1 Like

I know this post is all over the place so here’s a summary for people of what I did. I combined different things.

I am looking to make a motion trigger when my left outdoor camera detects motion. For the sake of this reply, it’s called “leftmotion”

I use !includes, so my configuration.yaml looks like this (not all is relevant here)

# includes
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
binary_sensor: !include binary_sensor.yaml
sensor: !include sensor.yaml
device_tracker: !include device_tracker.yaml
camera: !include camera.yaml
media_player: !include media_player.yaml
zone: !include zones.yaml
input_boolean: !include inputboolean.yaml
panel_custom: !include panel_custom.yaml
scene: !include scenes.yaml

In my input_boolean.yaml, I added this.

leftmotion:
  name: leftmotion
  initial: off
  icon: mdi:MotionSensor

In my binary_sensor.yaml, I put this. This means when the input it turned on, it automatically triggers the binary_sensor.leftmotion we created above.

- platform: template
  sensors:
    leftmotion:
      friendly_name: left motion
      value_template: "{{ states.input_boolean.leftmotion.state == 'on' }}"
      device_class: motion

In my scripts.yaml, I added this.
This will be triggered by Motioneye. It will turn the boolean on (which turns the binary_sensor on) then turn it off in 30 seconds.

leftmotion:
  alias: leftmotion
  sequence:
  - service: input_boolean.turn_on
    entity_id: input_boolean.leftmotion
  - delay: 00:00:30
  - service: input_boolean.turn_off
    entity_id: input_boolean.leftmotion

Check your config.
Restart Home Assistant.

Modify this for your needs.
**If you use SSL you need to change your IP to reflect that. **
In my case, it becomes https://MYSUBDOMAIN.DUCKDNS.org:8123/api/ etc etc

curl -X POST -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" -d '{"entity_id": "script.leftmotion"}' http://yourip:8123/api/services/script/turn_on

Get your token here: (only replace the word TOKEN and leave the rest)

Add your curl command here.

No script option
If you choose NOT to run a script, you can follow the rules above but instead.
use this for RUN A COMMAND:
(note this calls for the friendly name in the first part, and the sensor name at the end)

curl -X POST -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" -d '{"state": "on", "attributes": {"friendly_name": "left motion"}}' http://yourip:8123/api/states/binary_sensor.leftmotion

and in “RUN AN END COMMAND” use:

curl -X POST -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" -d '{"state": "off", "attributes": {"friendly_name": "left motion"}}' http://yourip:8123/api/states/binary_sensor.leftmotion

You can use an SSH terminal and run just the curl commands to test it out to make sure it triggers your sensors if you run into issues.

The result, for me, is that I now have a visual in my floorplan that shows me when motion has been triggered beside my house since no one should be there.

9 Likes

Hey guys!
If you want a motion sensor that won’t disappear on reboot, and you don’t want the input_boolean, you can use a MQTT Binary Sensor.

binary_sensor:
  - platform: mqtt
    state_topic: "home/livingroom/camera/motion"
    name: Living Room Camera Motion
    device_class: motion

EDIT

Then on motioneye, just use the “RUN A COMMAND” with:

mosquitto_pub -h localhost -u MY_MQTT_USER -P MY_MQTT_PASSWORD -t home/livingroom/camera/motion -m "ON" -r

Just change MY_MQTT_USER and MY_MQTT_PASSWORD with your credentials

and change the payload to OFF for the RUN AN END COMMAND!

4 Likes

I’ve tried MQTT a few times and I just can not get it. I need MQTT for dummies.