Friday, January 19, 2018

Tuning motion.conf lightswitch on raspberry pi for hue lights

Recap on using an non-recording old CCTV to create a motion sensor using raspberry to trigger hue lights with apple homekit automation. Ok, thats a long way around things. Here's a flow

1. Network Camera > 2. motion (pi) on_event  > 3. virtual motion sensor (domoticz / homekit plugin) > 4. Apple home automation (motion on, no motion off)

One of the initial problems was that the light turning off will trigger the motion/light. the solution is to modify the 'lightswitch' parameter, which doesn't have a lot of references to what to set, so it's trial and error as each camera feed are always different..

Noted that I did first change the framerate to 20 prior for better quality. To tune the lightswitch setting, set yourself up around the time the motion light is supposed to come on and follow these 3 steps as a general guideline.

1) Set the event_gap to a small number for testing, e.g. 10 seconds so that you do not need to wait for a long time before testing the light changes and motion detection.

2) set some base parameters which make sense, 2 or 3 is ok for the 2 variables.

- set minimum_motion_frames 2
- set pre_capture 2

3) start playing around with the lightswitch value. to figure out how it works, set it to the max and min and see which one doesn't give a false positive. I started with 100 and 10 and played with everything inbetween until i realised that 10 worked best anyway for my hue light at the door.

Wednesday, January 03, 2018

Network camera as motion sensor for lights

Here's a guide to use an old network cctv feed as a motion sensor for lights.

Here's what we need

1. URL for jpg feed from the network camera

The URL varies for different brands and models.

Example URL: http:///cgi-bin/net_jpeg.cgi?ch=0 

2. Raspberry pi with domoticz (already integrated with lights, e.g. hue) 

On the raspberry pi, download Motion

$ sudo apt-get motion

Modify the /etc/motion/motion.conf file (Refer to the motion documentation)

I removed the following from the motion.conf create a new thread file for each video source

comment out in /etc/motion/motion.conf

; videodevice /dev/video0
; target_dir /var/lib/motion
; stream_port 8081

I also changed the webcontrol_port to 8000 as 8080 conflicts with the default domoticz port.

;webcontrol_port 8080
webcontrol_port 8000

I added a thread file for each video source

thread /etc/motion/camera.conf

The following is an example of a tread file called camera.conf. You need to create a new thread file for each camera

netcam_userpass : user:password
text_left Camera
netcam_url http://netcam_ip/cgi-bin/net_jpeg.cgi?ch=0
target_dir /mnt/nas/camera
stream_port 8001
on_event_start /usr/bin/curl -s "http://domoticz_ip:8080/json.htm?type=command&param=switchlight&idxswitch_id=&switchcmd=On"
on_event_end /usr/bin/curl -s "http://domoticz_ip:8080/json.htm?type=command&param=switchlight&idx=switch_id&switchcmd=Off"

The variables you need to replace are:

user:password: if needed
netcam_ip and URL
domoticz_ip: IP address of the rasberry pi running domoticz
switch_id is the switch id of your light that you want.
Make sure that the steam_port is not already used on the pi and for each camera, to change the stream port.


If you want to have a bit more smarts for the motion sensor rather than having to keep turning on and off the motion process at night, create a Motion detection dummy virtual device on domoticz and replace that lightswitch id with the motion sensor.

It is possible to use the apple homekit/homebridge to add the virtual motion sensor to apple home and created an automation to set it for only turn on the light at night and when someone is home.

dead pi

Well, I guess it has to happen at some point. the home automation raspberry pi has died. Much to do with the stupid Strontium mini SD card. ...