Record a video based on Switch value


I have IP Camera and I can record the rstp stream with avconv.

avconv -rtsp_transport tcp -i rtsp://username:password@ -r 30 -vcodec copy -an -t 5 /alarm/video/frontcamera’date +%Y-%m-%d-%H-%M-%S’.mp4

This command will record a 5 sec clip from the camera.

I’m calling this from rule when motion sensor says that there is movement and we are not at home.

I would like to change this so that camera will record when ever I want as long as it is feasible.

I was thinking to have a Switch FrontCameraRecord and when ever this switch was on there would be avconv running and recording. And when FrontCameraRecord goes off I would stop recording.

Is this doable?


Is there for example way to make avconv a service and then I would start and stop the service from openhab based on switch?

Hey @Ari_Hagman,
I thing this really depends on the behavior of avconv. Currently you have the “-t 5” parameter which I guess are your 5 seconds.
What would happen if you ran avconv without this parameter and then killed the process with Ctrl+c? Do you get a valid video file?

Now try the same thing with first executing in the background and then kill using SIGTERM

If you were successful so far, start getting this procedure into your openhab rule.

Ah and to answer your second question, the described procedure is how a service would accomplish something like this. So yes, you could turn the whole thing into a service and only call this one from openHAB.


I will try to make service out of this.


This is the way I solved this

Created a service /init/etc/camera-front-record.conf, with following:

start on filesystem
exec avconv -rtsp_transport tcp -i rtsp://user:password@ -c copy -map 0:0 /alarm/video/front-camera’date +%Y-%m-%d-%H-%M-%S’.mp4

With sudo visudo added a line (So that openhab can start and stop a service)

openhab ALL = NOPASSWD: /usr/sbin/service camera-front-record *

Added an item

Switch  CameraFrontRecord

Added a rule

rule "Camera Front Start Recoring"
   Item CameraFrontRecord changed to ON

rule "Camera Front Stop Recoring"
  Item CameraFrontRecord changed to OFF

And in my Motion detection rule

rule "Motion Front"
  Item MotionFront changed to OPEN

rule "Motion Front"
  Item MotionFront changed to CLOSED

Next thing to solve is how to show these recording from openhab UI.

1 Like