Detect if a camera RTSP stream is down as a trigger for automation

Simple one really - and yes, I know the real fix is to replace the cheapo cameras I have.

I want to detect if a camera has stopped, and reboot it if it has. I’m using the Remote SSH addon to remotely execute a reboot on the cameras, but i’m struggling to find a way to detect if the camera is just a black screen as it is in HA when the stream goes down. I’m using Frigate as well as the stock generic camera integration.

Look into the Uptime Kuma add on. You can trigger automations if a monitored camera goes down.
Install the add on, and then the HACS integration

frigate has a camera fps sensor which is set to 0 when the camera is down

3 Likes

Sadly you can’t do UDP with this :frowning:

Awesome, thanks! A quick google didn’t provide me any examples - do you have something to hand? I’ve no idea how I would access that property of a camera.

Thanks in advance!

The fps entity is disabled by default. Enable it. You then get a sensor like this sensor.drive_camera_fps

What was suggested was automating based on the value of that sensor state going to 0 as your trigger.

1 Like

Well hot diggity damn. Thank you! I already use frigate so a very simple solution. Annoys me I didn’t think of it.

I actually had this issue on my list of things to figure out some day. I noticed when my NVR was down one day all the picture entity cards just kept the last image instead of going blank. Didn’t figure it out until it was night time and all the cameras were still daytime!

1 Like

Here’s a template binary sensor that will have the state of on if any frigate camera fps sensor has a state of 0. The entity_id attribute is a list of those cameras which you can iterate through to perform actions (such as rebooting, notifying etc) on.

template:
  - binary_sensor:
      - name: "Camera Stream Alert"
        unique_id: camera_stream_alert
        icon: mdi:cctv-off
        device_class: problem
        delay_on: 15
        state: >
          {% set entities = state_attr(this.entity_id,'entity_id') %}
          {{ entities|count > 0 if entities != none else none }}
        attributes:
          entity_id: >
            {{ states.sensor
                |selectattr('entity_id','in',integration_entities('frigate'))
                |selectattr('entity_id','search','_camera_fps')
                |selectattr('state','eq','0')
                |map(attribute="object_id")
                |map('regex_replace', find='(.*)_camera_fps', replace='camera.\\1')
                |list }}

Here’s how I list offline cameras for my alert message.

      - {{ expand(state_attr('binary_sensor.camera_stream_alert','entity_id'))
            |map(attribute='name')|join('\n- ') }}

There should be enough there for you to figure out how to do what you need to do.

4 Likes

Thanks a lot! That looks really helpful!

I am, however - still stuck on where to enable the fps entity. I’ve looked through the Frigate config docs but can’t find a mention of such a sensor.

It is in the integration.

sorry man - i’m sure this is a me thing, but I got nothing about fps in the Frigate UI settings, the HA addon config, frigate.yml, or anywhere else I can see. I’m running Frigate within HA in case that makes a difference.

The frigate integration. Home Assistant Integration | Frigate

1 Like

doh - got it! thank you!

1 Like

this is cool AF. had been pondering on the best way to do this and you nailed it right here. took me 5 mins to set up, just put what you have in the template as a binary sensor, and created an alert using the new binary sensor as the trigger and alert message. thanks so much!

Sorry, I didn’t manage to make i t work.
Sould I replace the ‘entity_id’ with each cameras name and repeat the whole ‘entity_id’ paragraph under ‘attributes’?

If you have frigate installed you should be able to copy and paste the template as is into your configuration. What is not working? Are you getting an error?

You are right, sensor and list message are working perfectly!
The mistake was mine, I haven’t enabled the diagnostic fps switches for each of the cameras.
Thank you!