MotionEye, home assistant, camera motion, automation

Why use curl when you can use mosquitto_pub and just use MQTT? Way easier.

Because MQTT isnt working (well not for me)

I managed to get it working via MQTT. Using this command.

Motion On
curl -X POST \ -H “Authorization: Bearer YOURTOKEN” -H “Content-Type: application/json” \ -d ‘{“payload”: “{“status” :“MOTION”}”, “topic”: “cameras/motion/picam”, “retain”: “True”}’ \ http://192.168..**:8123/api/services/mqtt/publish

Motion On
curl -X POST \ -H “Authorization: Bearer YOURTOKEN” -H “Content-Type: application/json” \ -d ‘{“payload”: “{“status” :“NO MOTION”}”, “topic”: “cameras/motion/picam”, “retain”: “True”}’ \ http://192.168..**:8123/api/services/mqtt/publish

Sensor YAML:

  • platform: mqtt
    name: “PiCam Motion”
    state_topic: “cameras/motion/picam”
    payload_on: “MOTION”
    payload_off: “NO MOTION”

HA is complaining about the payload on/off but ill sort that out later, its still working.

Anyone that has mosquitto_pub can use these:

mosquitto_pub -h <MQTT IP> -p <port> -t cameras/motion/frontdoor -u <username> -P <password> -m ON

mosquitto_pub -h <MQTT IP> -p <port> -t cameras/motion/frontdoor -u <username> -P <password> -m OFF

1 Like

Hi all

actually I have motionEye installed outside Hassio on another VM on my Intel NUC.

I’ve created a python script like this to run on motion detection.

curl -s -X POST https://api.telegram.org/botxxxxxx/sendPhoto -F chat_id=xxxxxx -F photo=@/var/lib/motioneye/Camera1/Alarm.jpg -F caption="Motion Vialetto!"
mosquitto_pub -h 192.168.x.x -u user -P password -t topic/Vialetto/motion -m "ON"

that I activate here

58

and this is how is defined the Still Images section

57

then in Nodered after receiving the ON payload on that topic after 15secs I send the OFF payload to manage a binary_sensor in HA

In this way I receive in a Telegram BOT a picture with the motion.

What I would like to receive (or to save locally, is a small video of the motion.
Is there a way to do it?

Thanks

2 Likes

I just found the solution, but this imply to have both MonionEye and NodeRed as Hassio Addon.

1 Like

I just wanted to add something here:
Ive started using MotionEye addon JUST to capture motion events in HA.
I have an NVR recording already, which is doing stuff on motion events, but my NVR doesnt have the ability to hit a webhook or run a command on these events. MotionEye does.

Hello HassCasts,

Thanks for the great description, have set up MotionEye and HA as described by you and it works, yeah …:+1::checkered_flag:

Ive had no luck with MotionEye. It keeps losing the connection to the camera.

1 Like

Are you able to share you config?

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…