Camera.record Stream/record not ending/saving

Hi,

So I have somehow very similar problem - my camera does record a stream but after a random number of times the recording is triggered - eventually it throws up the same error and then fails to record anymore until I restart hassio. Then everything is fine for few recordings and it does the same again.

Looks a bit to me like a weird race condition…Not sure if that’s helpful, but maybe we can dig into it deeper or maybe someone has an idea what’s going on here?

The error I get can be seen below:

Tue Dec 10 2019 19:13:32 GMT+0100 (Central European Standard Time)
Error while executing automation automation.nagrywaj_osoby_z_kamery_podjazd_przod. Unknown error for call_service at pos 1: 
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 437, in action
    await script_obj.async_run(variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 190, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 274, in _handle_action
    await self._actions[_determine_action(action)](action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 357, in _async_call_service
    context=context,
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 97, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1236, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1261, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 213, in handle_service
    self._platforms.values(), func, call, service_name, required_features
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 348, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 372, in _handle_service_platform_call
    await func(entity, data)
  File "/usr/src/homeassistant/homeassistant/components/camera/__init__.py", line 712, in async_handle_record_service
    DOMAIN_STREAM, SERVICE_RECORD, data, blocking=True, context=call.context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1236, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1261, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/components/stream/__init__.py", line 132, in async_record
    await async_handle_record_service(hass, call)
  File "/usr/src/homeassistant/homeassistant/components/stream/__init__.py", line 237, in async_handle_record_service
    raise HomeAssistantError(f"Stream already recording to {recorder.video_path}!")
homeassistant.exceptions.HomeAssistantError: Stream already recording to /config/recording/20191209-083038_czlowiek_podjazd_przod.mp4!

As can be seen, the log from 7pm on Dec 10th reports a problem recording a file from Dec 9th apparently started at 8:30 AM!
The weird thing is, it recorded another video 2h later (as it should, because a person was detected) but it failed recording anything since then. I’ve tested that the trigger works and it indeed does - it’s just that the recording fails.

I thought that maybe because on the same binary sensor I’m using two different automations - one to send me a snapshot via telegram and the other to record. But they have different conditions, because I want to get snapshot only when my alarm is armed away, but I want to record all the time a human being is detected, regardless of the alarm status…

The relevant bits from my automation.xml below:

- id: '1569783718678'
  alias: "osoba podjazd przod"
  trigger:
  - entity_id: binary_sensor.osoba_podjazd_przod
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: alarm_control_panel.drewutnia
    state: armed_away
  action:
  - data:
      message: czlowiek na podjezdzie
      data:
        photo:
          - url: http://192.168.0.24:5000/podjazd_przod/best_person.jpg
            caption: "wykryto czlowieka na podjezdzie"
    service: notify.automatyczny

- id: '1569783718683'
  alias: "nagrywaj osoby z kamery podjazd przod"
  trigger:
  - entity_id: binary_sensor.osoba_podjazd_przod
    from: 'off'
    platform: state
    to: 'on'
  action:
  - service: camera.record
    data:
      entity_id: camera.podjazd_przod
      filename: "/config/recording/{{ now().strftime('%Y%m%d-%H%M%S') }}_czlowiek_podjazd_przod.mp4"
      duration: 30
      lookback: 5

…and the binary sensor is configured as such (I’m using frigate for human detection and it does work without any problems: https://github.com/blakeblackshear/frigate)
Also, the notification via telegram always works too, so it does seem to be a problem with the camera.record function…
I have a pretty standard IP camera for which I’m using a RTSP over TCP connection.

camera:
  - platform: generic
    name: podjazd_przod
    still_image_url: http://127.0.0.1:8123/local/mylocal.png
    stream_source: rtsp://192.168.1.11/H264

If anyone has any idea what’s happening here, please share :slight_smile:
Thanks in advance!