Note: This is generated from a transcript from one of my YouTube videos


The Persistent Cat Problem

The Persistent Cat Problem demonstration at 20.0s The Persistent Cat Problem demonstration at 49.0s We had a bit of a cat problem. Basically, cats were coming into our garden and using it as a litter box.

We were also lucky to have birds nest in a bird box on the side of our house. It has a little camera, allowing us to watch them build the nest, hatch the chicks, and then fledge. This was fantastic, but the cats are quite predatory and tried to hunt the birds in our garden. We’ve seen cats jump into trees to catch birds. I had set up Frigate to detect when a cat is in the garden based on camera input.

Frigate seemed to work pretty well at announcing when there’s a cat in the garden, though there’s an occasional false positive. There’s no intention to harm the cats at all. My dog, much like me, is fat and old, so there’s no chance of him actually catching the cats or doing anything to them.

If he caught up with a cat, he wouldn’t even really know what to do. However, we’re not always there to let the dog out, and the cats quickly learned that if they don’t hear the door open, there’s no danger. So, they carried on going, and this didn’t really solve the problem.

Early Attempts and Their Limitations

Early Attempts and Their Limitations demonstration at 99.0s The next attempt was to get a motion-activated sprinkler from Amazon. You plug a hose into it, and it has a built-in motion sensor. When it detects motion, it sets off a small spray of water for a few seconds.

This type of sprinkler works pretty well if you want to protect a specific area of the garden, like a flower bed, where people aren’t likely to walk. However, the problem is that these devices don’t differentiate between a person, a cat, or anything else that might trip the motion sensor. As soon as something moves in front of it, the sprinkler activates.

Cats are also quite smart; they quickly figure out that if they walk in front, they’ll get wet, but if they walk behind it or go to the toilet there, they’re in no danger of being sprayed. This led me to the third solution.

The Unpredictable Sprinkler Solution

The Unpredictable Sprinkler Solution demonstration at 170.0s The Unpredictable Sprinkler Solution demonstration at 182.0s I'm really happy with this third solution. We got another sprinkler, still connected to the hose, but it's one of those impact sprinklers. When pressure is applied, it drives a mechanism that clicks around, firing out jets of water. These sprinklers do a full 360-degree rotation and then reverse. I used a Sonoff ZigBee solenoid valve that connects directly into the hose, allowing me to turn the hose on and off.

The system is permanently rigged up, so as soon as the valve turns on, the sprinkler starts, and when it’s off, the sprinkler stops. This setup provides complete unpredictability regarding which direction the water will fire, which works really well for the cats.

The cats can’t learn its behavior because there are so many of them, and they tend to time-share the garden. Since the sprinkler activates randomly, there’s no consistency for each cat to predict its direction. To them, it seems to fire off in all random directions, meaning they scatter as soon as they hear it.

Frigate picks up the cats pretty quickly, and the system kicks in. The cats are now super anxious coming into the garden; they tiptoe around instead of strutting. The automation is definitely working really well.

Refining the Automation Logic

Refining the Automation Logic demonstration at 279.0s For any seasoned home automation developer, you realize there are always weird edge cases. One I encountered was a delivery person getting wet. I had already tuned the automation to detect a person and not set off the sprinkler. However, if the sprinkler was triggered before the person arrived, within that three-second window, it would still go off and potentially get them wet.

I’ve created a flowchart to simplify how the automation works, and I’ll also explain the YAML configuration. At the start of the flow, there are a few conditions.

If a person is detected, or the front door or garage door is open, it sets a variable indicating a human is present and starts a 30-second timer. Now, if a cat event is triggered while a human is detected, the automation does nothing and restarts the 30-second timer. However, if a cat event is triggered and there isn’t a human, it proceeds to the next step.

If a cat event is triggered and no human is detected, it will start watering for three seconds by turning on the Sonoff ZigBee solenoid valve. During this process, if a person is detected, or the front door or garage is opened, a kill switch immediately stops the water. It then sets a flag indicating a human is present, starts the 30-second timer, and returns to the beginning of the flow. This kill switch works really well, ensuring delivery people don’t get sprayed if the sprinkler goes off.

If no one is detected, the flow runs its course, watering for three seconds, then stopping the water. It then waits in a condition to determine if the human presence variable needs to be set or not.

flowchart TD
    A[System Start] --> B[Initialize: human = false]
    B --> C{Person Detected OR Front Door Opened OR Garage Door Opened?}
    
    C -->|Yes| D[Set human = true]
    D --> E[Start/Reset 30s Timer]
    E --> F{Cat Event Triggered?}
    
    C -->|No| G{30s Timer Running?}
    G -->|Yes| H{30 seconds elapsed?}
    H -->|No| F
    H -->|Yes| I[Set human = false]
    I --> F
    
    G -->|No| F
    
    F -->|No| C
    F -->|Yes| J{human == true?}
    
    J -->|Yes| K[Do Nothing]
    K --> C
    
    J -->|No| L[Start Water for 3 seconds]
    L --> M[Water Running]
    
    M --> N{Person Detected OR Front Door Opened OR Garage Door Opened?}
    N -->|Yes| O[Stop Water]
    O --> P[Set human = true]
    P --> Q[Start/Reset 30s Timer]
    Q --> C
    
    N -->|No| R{3 seconds elapsed?}
    R -->|No| N
    R -->|Yes| S[Stop Water]
    S --> C
    
    style A fill:#1565c0
    style L fill:#c62828
    style O fill:#2e7d32
    style K fill:#ef6c00

These are the three automations that make it work:

Implementing the Automations in Home Assistant

The first main automation is designed to water the cats. It checks if a cat is detected in the front garden or by the doorbell, and then verifies if a specific condition is true.

The template checks the last_triggered state. It verifies if it’s none, meaning the automation hasn’t run recently, or if the current time minus the last_triggered time is greater than 1800 seconds, which is 30 minutes.

It then double-checks, as seen in the flow diagram, that no person is detected and the front door is closed. I also added a condition for rain: if the precipitation probability is above 85%, it’s likely raining, and cats won’t be out. In such cases, detections are often false positives, so the automation won’t trigger.

If all conditions are met, it turns on the Sonoff ZigBee solenoid valve for the hose and sends a notification confirming the sprinkler has activated and “watered the cat”.

The second automation is designed to stop watering the cats. It triggers when the Sonoff ZigBee solenoid valve is turned on, waits for three seconds, and then turns it off again. This ensures that no matter how it’s triggered, the valve will always turn off after three seconds. There was one instance where it turned on for an unclear reason, but this automation ensures it won’t stay on for longer than three seconds, preventing water waste.

The third automation is the kill switch, designed to prevent watering people. If the Sonoff valve is already on, it triggers on several events: a person at the door, a person in the front garden camera, or if the doorbell’s visitor status is on (meaning someone pressed the button). It’s also triggered if the front door or garage door is opened. These events initiate a 30-second timer.

The action for this kill switch automation is simple: it turns off the hose, sets the “front garden recent person” input Boolean (as seen in the flow diagram), waits for 30 seconds, and then disables that Boolean again. This input Boolean is directly controlled by the automations.

Automations

Water the cats

alias: Maintenance - water cats
description: ""
triggers:
  - type: occupied
    device_id: 9453eca3b18ed24921ee474d6a417ef7
    entity_id: b784e31b092a070859bba5a8ff8d2627
    domain: binary_sensor
    trigger: device
    enabled: true
  - type: occupied
    device_id: 5ece90f97ce8f5ccae5e618642f01d1a
    entity_id: e204f43b0a4fe1f2927337843693d526
    domain: binary_sensor
    trigger: device
    enabled: true
conditions:
  - condition: template
    value_template: >-
      {{ state_attr('automation.maintenance_water_cats', 'last_triggered') ==
      none or (now() - state_attr('automation.maintenance_water_cats',
      'last_triggered')).total_seconds() > 1800 }}
  - condition: state
    entity_id: input_boolean.front_garden_recent_person
    state: "off"
  - type: is_not_open
    condition: device
    device_id: 0e546256a057750bc79caecedb077bb9
    entity_id: ae8a12324b914b89cd351a031ce3198a
    domain: binary_sensor
  - condition: numeric_state
    entity_id: sensor.pirate_weather_precip_probability
    below: 85
actions:
  - type: turn_on
    device_id: e08748e04df8e572eb31e70760024f1c
    entity_id: 85568e5db507a35d03f7e66ff0dd32cf
    domain: switch
  - action: notify.mobile_app_pixel_9_pro
    metadata: {}
    data:
      title: Watered a cat
      message: The sprinkler was set off at {{ now() }}
    enabled: true
    alias: Notify Ben's phone
mode: single

Stop watering the cats

alias: Maintenance - stop watering cats
description: ""
triggers:
  - type: turned_on
    device_id: e08748e04df8e572eb31e70760024f1c
    entity_id: 85568e5db507a35d03f7e66ff0dd32cf
    domain: switch
    trigger: device
conditions: []
actions:
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - type: turn_off
    device_id: e08748e04df8e572eb31e70760024f1c
    entity_id: 85568e5db507a35d03f7e66ff0dd32cf
    domain: switch
mode: single

Don’t water people

alias: Maintenance - don't water people
description: ""
triggers:
  - type: occupied
    device_id: 5ece90f97ce8f5ccae5e618642f01d1a
    entity_id: d82a943e5c2ca83e2c8d155897524b39
    domain: binary_sensor
    trigger: device
  - type: turned_on
    device_id: 60f0f7dba756a82ed054ec8200829078
    entity_id: cabfef39d493a5949aa56b1e88b22cba
    domain: binary_sensor
    trigger: device
    enabled: false
  - type: occupied
    device_id: 9453eca3b18ed24921ee474d6a417ef7
    entity_id: 5a787e0f6349aa1a716f7a255c097b86
    domain: binary_sensor
    trigger: device
  - type: turned_on
    device_id: 589143cd32a722440904c68fcf411fba
    entity_id: ceeafbaffb6623a4dc097b468a04514b
    domain: binary_sensor
    trigger: device
    enabled: false
  - type: turned_on
    device_id: 60f0f7dba756a82ed054ec8200829078
    entity_id: bd8e8bfd2b39fb7832bbc6cdd5df0fb3
    domain: binary_sensor
    trigger: device
  - type: opened
    device_id: 0e546256a057750bc79caecedb077bb9
    entity_id: ae8a12324b914b89cd351a031ce3198a
    domain: binary_sensor
    trigger: device
  - type: opened
    device_id: 12e6c8f87130914908c82c0b7e9dbe03
    entity_id: e32893af60259300aad1a7dcd10eb5b5
    domain: binary_sensor
    trigger: device
conditions: []
actions:
  - type: turn_off
    device_id: e08748e04df8e572eb31e70760024f1c
    entity_id: 85568e5db507a35d03f7e66ff0dd32cf
    domain: switch
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.front_garden_recent_person
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - action: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.front_garden_recent_person
mode: restart

Addressing False Positives and Measuring Success

Addressing False Positives and Measuring Success demonstration at 664.0s Addressing False Positives and Measuring Success demonstration at 684.0s Addressing False Positives and Measuring Success demonstration at 692.0s
While the automation is pretty rock solid, we still encounter some false positives. For example, rain or water drips on the camera can sometimes trigger a false cat detection. Our cameras also use infrared lights for night vision, which attract insects. These insects, in turn, attract spiders that build webs around the cameras. When a gust of wind catches these webs in a particular way, it can create false positives, making Frigate think it's a cat.

Another concrete example of false positives occurred during Halloween. Some ghost decorations on the hedge, when caught by the wind, were sometimes mistaken for cats by Frigate. However, having the automation trigger every 30 minutes, even with false positives, isn’t a bad thing. It makes the system super unpredictable for the cats, and the primary goal is to keep them out of the garden, not necessarily to get them wet.

Ultimately, it works really well, and the proof is in the pudding: we’ve not had cats using our garden as a litter tray. We’ve also noticed many more birds returning to the garden and visiting our feeders. This is a truly awesome automation, and I’m very pleased with its effectiveness.

Future Enhancements with Frigate+

Future Enhancements with Frigate+ demonstration at 732.0s Even though the automation works really well, I still want to make some changes. I currently use the free version of Frigate, but I'm going to upgrade to the paid version. As I do this, I'll track the differences in features and how well the automation works with both the free and paid versions. If you're interested in a deep dive into that, I'll likely make a video about it, so make sure to subscribe to stay updated.

With the paid version of Frigate, I’m hoping to train a specific model to detect cats in my garden more accurately. Frigate uses a base model trained on general cat images, but I can tweak it with pictures of cats from my specific cameras to significantly reduce false positives. It also provides access to other models, like YOLOv9, which offer better detection. I’m really keen to try that out and will report back on how well Frigate+ works for this particular setup.

If you enjoyed the video, please leave a thumbs up; it makes a massive difference to the channel. I appreciate your time, and I’ll catch you in the next one.

Links:

This is the valve I’m using:

And these are the cameras I’m using:

Video

You can watch the full video on YouTube here:

Support me to keep making videos

Ko-Fi

If you like the work I’m doing, please drop a like on the video, or consider subscribing to the channel.

In case you’re in a particularly generous mood, you can fund my next cup of coffee over on Ko-Fi

The links from some of my videos are affiliate links, which means I get a small kickback at no extra cost to you. It just means that the affiliate knows the traffic came from me.

×