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.
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.
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.
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 alwaysidle, even when obviously recording (because the file will be updated, which requires to camera to record).
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.
Since a few months it appears that while recording the file has *.tmp extension and when done the file gets renamed to the desired filename. So you can watch for a ‘moved’ event type and the proper extension like i described it here: