Trigger camera.record, send recording when finished

Tags: #<Tag:0x00007f325c87c7e8> #<Tag:0x00007f325c87c6a8>

Hi everybody,

I have multiple cameras that I mainly include in notifications (if front gate opens, send telegram message with snapshot from camera x). I want to create an automation to trigger camera.record, then send that recording (instead of a picture). Is there a way to determine when the file has actually been written?

In other words: let’s say I want to record 120 seconds, then send this recording. How can I make sure that this file is sent, not a previous recording? I thought of delay: "00:02:00", but there must be a better way.

My incomplete package looks like this:

binary_sensor:
  - platform: tod
    name: Nachtmodus
    after: "20:45"
    before: 07:45"

automation:
  trigger: 
    - platform: state
      entity_id: binary_sensor.draussen_gartentor_contact
      to: "on"
  condition:
    condition: state
    entity_id: binary_sensor.nachtmodus
    state: "on"
  action:
    - service: camera.record
      entity_id: camera.cam_einfahrt
      filename: /config/downloads/cams/einfahrt.mp4

What do I need to add so that I will always get the most recent recording (instead of a previous recording, or an incomplete file)? At first I thought I could use something like filename: '/tmp/{{ entity_id }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4', but the strftime value will be different for the file to what I could use in my service: notify.all part of the code, as those wouldn’t be executed at the very same second.

I thought of creating a second automation to observe when camera.cam_einfahrt's status changes from recording to idle; however, for some reason, even when I call the service to record a file (via developer tools), the camera will be set to idle, which is weird. I checked after the set period of time: the file will be recorded, so syntax etc. was correct, but the camera entity did not report that it was actually recording.

Thank you for your ideas :slight_smile:

I don’t know if the service_executed will trigger when the recording is finished? Worth looking at https://www.home-assistant.io/docs/configuration/events/#event-service_executed

Thank you. I read through the link (and https://www.home-assistant.io/docs/configuration/state_object/), but unfortunately, I do not understand how to include this in my automation.

My current approach is creating a script for this, where I have a delay value of "{{ duration + (duration / 2) }}", which ought to work. I had duration + 10 previously, but when I ran an 120s test recording, it sent a video of only 59 seconds (instead of 1:59, which is the length of the actual file when I play it from the folder it had been saved to).

The camera entity I created is an ffmpeg camera. As far as I can tell, I cannot do anything with it, for example camera.turn_on/off, camera.enable_motion_detection, etc. will not change the state of the entity.

Another attempt was to use the file and file_paths sensors, but those don’t update fast enough to work in an automation. It is not possible to change the interval in which they re-check the file/folder, and even if so, it’d have to constantly be scanning in order to trigger somewhat close to when the recording stopped.

Observe your events. Is service_executed triggered when the recording starts or when it concludes?

I feel stupid asking you to walk me through this, but I just can’t even see this. My events get “spammed” so much, mainly by all my tasmota entities, that there is no way to just look at the events and find one particular thing. I can hardly read two lines until there is a new update in the event log. Can I perhaps pipe the output to a file somehow (similar to home-assistant.log)?

I tried as best as I could to trigger the camera from my smartphone while watching the log in the browser - but either there was no event triggered whatsoever, or I didn’t see it because other events appeared too soon after it had been triggered.

Developer tools|events|scroll down to “listen for events”|type service_executed in the text box|click “start listening”

Then open a terminal and ssh into your home assistant machine and type

watch -n 1 ls -l   /config/downloads/cams/einfahrt.mp4

You will then see the output change every second.

Trigger the automation. Compare the output of the event listener to the growth of the file.

Thank you. I did just what you wrote.

The file will eventually change (date and size). There is no output regarding the camera (or any camera) or anything containing “record” in the event log.

Also, the camera state is always idle, even when obviously recording (because the file will be updated, which requires to camera to record).

I then created this automation:

automation:
  - alias: "Camera Event"
    trigger:
      platform: event
      event_type: state_changed
      event_data:
        entity_id: camera.einfahrt
    action:
      - service: notify.me
        data:
          title: ok
          message: ok

The automation itself works (when I trigger it manually, I get the message). Nothing happens when the camera starts (or finishes) recording. I just got the message twice randomly now, which makes this even more weird.

There is no output in event log when the automation above gets triggered.