MotionEye, home assistant, camera motion, automation

In MotionEye, under motion events, theres an option to send a webook.

I have rpi doing a number of things, one of hwich is a webserver. I wrote a simple handler for the HA api to make it easier to integrate with.
The webhook in MotionEye is just http://blah/ha?action=event&entity=camera_motion

then my handler grabs this, adds the auth header, and sends the relevant API request to HA.
You could, in theory, also turn on the legacy API for HA and do the same thing directly, but i decided for security reasons i didnt want to turn that on.

So just after I have worked this out I need to rework this out for NX Witness as one cant just copy/paste the commands. Not sad to see MotionEye go.

im trying to mess with nx witness…where do you add cameras on the media server?

i dont have any problems with connections to cameras on my motioneye, i even use a couple rstp streams over wireless

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