Generic IP Camera platform - starting and stopping internal SD card recording via HA event?

I have an IP camera watching over my front yard and an MQTT sensor on my front gates. I can use HA to grab jpg snapshots fine and send them to my mobile via Pushbullet when my MQTT gate sensor in tripped. Was not too hard to sort out, given that the generic IP camera platform configuration sort of leads your there by default.

Now the IP cameras own motion capture SD card recording system sucks, it records trees moving, clouds going past, and misses people being murdered across the street (ok I made that last bit up). Try as I might to find tuning settings that work well, it always fails in some lighting conditions or other…

What I’d like to know, is how to get HA to send a start recording and then a stop recording message after the gate is triggered (this is all I really need to record). This should reliably get video clips stored to SD when a real event happens (gate opened).
I can do it manually via the cameras web interface, so I would think it could be possible to get HA to do the same ?

The start and stop strings in the web browser are (where 123.123.123.123 replaces my actual internal camera IP)

http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=start&id=samba
and
http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=stop&id=samba

Generic IP camera platform has built in service for ‘snapshot’ and ‘motion capture’ on/off, but not a ‘recording’ start/stop service that I can see.

Anyone know if there is start/stop recording service for generic IP camera, or is this is something usually done by scripts or templates ?
(and if so I’m out of my depth…)
Thanks.

Does your camera require a login? I.e., you showed the URLs you used in a web browser, but did you also have to provide login credentials in the browser when you used those URLs?

I ask because it might be possible to use RESTful Commands (https://www.home-assistant.io/components/rest_command/) to start and stop recording. It comes down to the details. (E.g., when I tried to do this with my Amcrest camera, it didn’t work because the Amcrest camera uses digest authentication and the RESTful Command did not support that, or at least it didn’t back when I tried it.)

If the RESTful Command won’t work, you might also be able to use a Shell Command (https://www.home-assistant.io/components/shell_command/) which uses the curl command.

Yes it does, I left that out as I though it complicated the question somewhat. The Generic IP Cameras configuration.yaml has the camera username and password, and sends that when grabbing a snapshot. I think I can include that in the URL string too like http://blablabla.snapshot.jpg?user=[USERNAME]&pwd=[PASSWORD]
It may be that the recorder would take the login strings the same way.
I just have no clue how to send that via an automation…

You create the RESTful Command, then you call it from the automation (just like any other service.) E.g., if you created two RESTful Commands - say start_recording and stop_recording, then you call them like:

rest_command:
  start_recording:
    url: 'http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=start&id=samba'
    # Other parameters, like username and password, go here...
  stop_recording:
    url: 'http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=stop&id=samba'
    # Other parameters, like username and password, go here...
automation:
  - alias: Start and stop camera
    trigger:
      # Put your MQTT trigger here...
    action:
      - service: rest_command.start_recording
      - delay: 00:02
      - service: rest_command.stop_recording

Thanks I’ll give that a bash.
I had to go find out what REST means :slight_smile:

Yep, I did the same not too long ago. :slight_smile:

BTW, as I said, I’m using an Amcrest camera. I actually enhanced the implementation to allow direct turning on/off of recording, moving camera to preset locations, turning on/ff touring, getting status of motion detection, etc., etc. But I needed (ok, wanted) a lot of functionality, so that was best for me. For what you’re looking to do I think using the RESTful or Shell Commands is probably easiest.

@pnbruckner
Hi,
I’m having trouble getting the REST to work for me and I think it’s regarding how I need to pass the user and password using that (not as straight forward as I thought).
Could you post your REST command config block for the Amcrest and show me how you pass that after the start and stop recording URL’s (change it to something not your REAL password and user of course) ?
Thanks.
Edit: Oops sorry, you might have already said it might not work above… Hmmm.

Well, as I said, I couldn’t use the RESTful Command because the Amcrest requires digest authentication and the RESTful Command does not support that (and therefore I don’t have a config that uses that and works with my camera.) But I did have a config that used the Shell Command that used the curl command. If necessary I’d be happy to help you use that.

But, I suspect your camera doesn’t require digest authentication, and so the easier to use RESTful Command should probably work for you. Can you post what you’ve tried (with secrets replaced)? It might be easier that way for me to spot what might be wrong.

Here is what I have in configuration.yaml.
The two URL’s do work from Chrome, both get an OK response, and a new file appears on the SD card (so it’s probably not PW related I guess).
However from HA Services GUI tool, a call service either of the new services listed does not seem to get the camera to do anything (that I can see). No new video file after a start-delay-stop etc.

rest_command:
  start_recording:
    url: 'http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=start&id=samba&user=homeassistant&pwd=mysecretpass'
    # Other parameters, like username and password, go here...
  stop_recording:
    url: 'http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=stop&id=samba&user=homeassistant&pwd=mysecretpass'
    # Other parameters, like username and password, go here...

So, when you use those URLs in Chrome, does it pop up a sign in dialog, or did it maybe do that before and save the username & password? What happens (again, in Chrome) if you drop the &user=homeassistant&pwd=mysecretpass part of the URL?

What happens if you try this instead:

rest_command:
  start_recording:
    url: 'http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=start&id=samba'
    username: homeassistant
    password: mysecretpass
  stop_recording:
    url: 'http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=stop&id=samba'
    username: homeassistant
    password: mysecretpass

Yeah, tried to verify my result this morning by your method after turning on the PC, and it failed. When testing the URL’s I was using two browsers (IE was the other) and watching the camera GUI on IE while trying the URL’s via Chrome. It looks like I must have done a login on Chrome at some point and not realised (damn).
So yep I get a login dialogue box with
http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=start&id=samba
and also with
http://123.123.123.123/camera-cgi/admin/recorder.cgi?action=start&id=samba&user=homeassistant&pwd=mysecretpass
I’ll give the syntax you suggested a try in the config and see how that goes…

Yeah. The difference is, I believe the &user= and &pwd= URL parameters are not a generic thing that every web server supports. But when you use the REST Command’s username & password parameters, that uses a standard authentication mechanism (like what the browser does when it prompts for the login.) The trouble is, at least last time I checked, the REST Command uses aiohttp, and aiohttp only supports basic authentication, and not digest authentication. So, hopefully your camera uses basic authentication and this works for you! But, if not, don’t give up - you can still use a Shell Command instead (with curl.)

Some success, maybe complete success. Just tried the new syntax after a RPi restart for the configuration changes (which takes about 20 mins) and I can now see the camera interface go to record when using the services tool in HA. Now to create an automation and double check…

Yes, got it to trigger, record and stop via an automation (I use a spare 433Mhz doorbell button to test this sort of thing via MQTT from an RF hub).
I’ll not claim it’s full success though till I’ve seen a few new video files accumulate when I’ve logged out fully on the PC…

Yes it’s working OK now thanks to pnbruckner. I had to add a small time delay between my old snapshot fetching automation and starting the video recording, but once that was done I get a snapshot at higher resolution via PushBullet to my phone, then 20 seconds of video right after on the SD card.

1 Like