Smart Litter Box (or Smart Cats)

There has to be more cat owners in here than me!
I wanted to share my configuration for my “smart litter boxes”. We live in a house, and our cats litter boxes are kind of hidden away and easy to forget. With this I get a notification on my phone after three visits. I have two sets of these files, one for each litter box.
Products used (per litter box): One Xiaomi human body sensor and one cheap 433mhz button from Ebay. You can of course use any kind of pir and even omit the button for using the gui if you want.
First comes the Litter box package with everything except the python scripts.

homeassistant:
  customize:
    sensor.downstairs_litter_box_visits:
      icon: mdi:poop
    binary_sensor.motion_sensor_158d0001a24ef1:
      friendly_name: Downstairs Litter Box
#-------------------------------------------

sensor:
  - platform: mqtt
    state_topic: "dummy/downstairslitterbox/count"
    name: Downstairs Litter Box Visits
    unit_of_measurement: "visits"

#-------------------------------------------

automation:
  - alias: Set Downstairs Litter Box Sensor To Zero On Start
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: python_script.dlitter_reset
    
  - alias: Count visits to downstairs litter box
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d0001a24ef1
        to: 'on'
    condition:
    - condition: template
      value_template: >
        {% if states.automation.count_visits_to_downstairs_litter_box.last_triggered is not none %}
          {% if as_timestamp(now()) | int   -  as_timestamp(states.automation.count_visits_to_downstairs_litter_box.attributes.last_triggered) | int > 300 %} true {% else %} false
          {% endif %}
        {% else %}
        false
        {% endif %}
    action:
      - service: python_script.dlitter_counter

  - alias: Send notification when downstairs litter box is used more than three times
    trigger:
      - platform: numeric_state
        entity_id: sensor.downstairs_litter_box_visits
        above: '2'
      - platform: state
        entity_id: device_tracker.isabellas_iphone_6s
        to: 'home'
        for:
          minutes: 10
    condition:
      condition: and
      conditions:
        - condition: time
          after: '07:00:00'
          before: '23:00:00'
        - condition: state
          entity_id: device_tracker.isabellas_iphone_6s
          state: 'home'
        - condition: numeric_state
          entity_id: sensor.downstairs_litter_box_visits
          above: '2'
    action:
      - service: notify.ios_isabellas_iphone_6s
        data:
          title: 'Time to clean!'
          message: 'More than three visits to downstairs litter box'

  - alias: Reset downstairs litter box visit counter
    trigger:
      - platform: state
        entity_id: binary_sensor.white2
    action:
      - service: python_script.dlitter_reset

And same code in pastebin for easier read: https://pastebin.com/t7DLPbg3

dlitter_reset.py:

hass.states.set('sensor.downstairs_litter_box_visits', 0)

And dlitter_counter.py:

counter = hass.states.get('sensor.downstairs_litter_box_visits')

if counter is None:
    value = 0
else:
    value = int(counter.state)

hass.states.set('sensor.downstairs_litter_box_visits', value + 1)

Happy to ask any questions (or show pictures of my cats :rofl: )! I also plan on keeping track of sudden changes in the litter box visits from stats later, could be used to detect if a cat is sick (sudden increase or decrease in litter box visits per day).

12 Likes

Very usefull, will implement this myself.
Thanks for sharing!

1 Like

Great idea - and I’d love to see a pic of your cats!

1 Like

I’m not sure if I understand why the first if statement is implemented? Can you elaborate?
The second statement makes sure it only counts movement as a next visit if there are at least 5 minutes between the movement detections, right?

1 Like

Who am I to deny you!
Here’s both of them:


And bonus coder cat “helping” me with home work:

3 Likes

Links are both of coder cat I think - but he/she is a beauty!

Edit: Sorry, I see both pics :slight_smile:

No no, I fixed the first link now! You were correct. The coder cat is a she, the other one’s a he. :slight_smile:

1 Like

The one with last_triggered is not none? It checks that it actually has been triggered before checking the time. And yes, five minutes between. That potentially could make it miss one visit here and there, but it’s useful to eliminate a long visit counting as several.

2 Likes

Interesting timing on this - our ageing cat had an “accident” yesterday but we think he was just being more persnickity then usual about his litter tray so we need to clean it more often - this idea could help us keep track of him :slight_smile:

This is he:

2 Likes

I’m guessing this is to avoid errors when the sensor is not yet present.

1 Like

Very cute! Yeah, our girl is very perticular about a clean litter box. Otherwise she protests by peeing in our bed. :persevere: So this is much useful.

1 Like

LOL - then this is very necessary for you :slight_smile: Thanks for the idea!

Thanks for clarifying.

sensor:
  - platform: mqtt
    state_topic: "dummy/downstairslitterbox/count"
    name: Downstairs Litter Box Visits
    unit_of_measurement: "visits"

Why using an MQTT sensor? It is not updated via mqtt right?

Nah, no mqtt involved, but it’s good way to make a dummy sensor. That is, a sensor that’s not actually connected to a device.

1 Like

Sorry for the noob question but in what directory do I put the py files?

No worries! First of all you need to enable python scripts by putting “python_script:” in your configuration.yaml. Then I just have them in a folder called “python_scripts” in the same directory as the config.

1 Like

@teachingbirds thanks, got it up & running now! :slight_smile:

I made a small addon which logs every visit to a speadsheet in my Google Drive account. Curious how much it triggers on an average day.

- alias: Count visits to litterbox
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00016db13b
      to: 'on'
  condition:
  - condition: template
    value_template: >
      {% if states.automation.count_visits_to_litterbox.last_triggered is not none %}
        {% if as_timestamp(now()) | int   -  as_timestamp(states.automation.count_visits_to_litterbox.attributes.last_triggered) | int > 300 %} true {% else %} false
        {% endif %}
      {% else %}
      false
      {% endif %}
  action:
    - service: python_script.litter_counter
    - service: ifttt.trigger
      data:
        event: litterbox
        value1: Cat detected

2 Likes

My cats litter tray is this thing:


So its not always easy to see when it’s getting yucky.
This is a great idea!
I think that the Xiaomi sensor would work just fine in there, and hopefully without false positives from passing humans!

1 Like

This is great. I can set the automation to text my daughters when it’s time to clean their assigned catbox!!! And here’s the required pic of our 2 cats, Bellatrix and Weasley :slight_smile:

2 Likes