Monitoring a cat flap state - best way?

Hi all,

I’ve been working on a small project to monitor our cat flap - i.e. whether the cat has gone out or come in through it.

Using a couple of reed switches and magnets mounted on the flap, an ESP8266 reads the reed switches and sends a message over mqtt which Openhab (ver 2.4) picks up. The code on the ESP sends an “In” or “Out” state based on which direction the flap has moved. When the flap is in it’s normal position i.e. closed, there’s no state sent.

I’ve configured openhab to read the mqtt channel and put the state into a text string - so I get the state of whether the cat has gone out or come in (this is based on the assumption that the cat flap is the only way the cat can come and go).

Where I’m stuck is that I want to log this data into grafana, which I have set-up and working. I can’t decide on the best item type to use to enable me to: a) see whether the cat is in or out (and preferably the time it happened and, b) see some sort of graphical timeline of when the cat has come in and gone out historically.

Can anyone offer any ideas based on similar concepts - I can’t seem to see the wood for the trees on this!

I’ve note done it with Contacts, but you can easily do this with Switches. Contacts should work too but I don’t know that they work.

ON is stored in InfluxDB as a 1 and OFF as a 0. You can then chart that in Grafana and get a nice stair step graph showing when the cat is home or away.

Thanks rikoshak, that was kinda what I thought - but for some reason part of my brain said it wasn’t the greatest way. I think that maybe because I was using switches for PIRs I get some odd things happening in the graphs as they open and close so quickly

A Contact would be the most “natural” openHAB representation - a binary sensor that cannot accept commands.
You might prefer a Switch for easy implementation of a manual override/correction via UI.

I can see this all going wrong when the neighbourhood tomcat sneaks in at night :smiley:

If you maintained a “count” of cats inside, and coded for it not to go below zero (even when it thought it was zero and got a new “out”), there’d be a certain amount of self-resetting function to eventually account for cats coming and going through the window at times.

Rosska57, that’s not a problem as the catflap is keyed to the cat’s microchip so only she can use it. Basically my problem was that it was a relatively old cat flap and I didn’t want to pay £150 for a new version which required a hub and an a app to remotely control the cat flap and monitor the cat! It seemed far easier to retrofit a sensor on it to see which way the flap opened.

Of course, following Rossko57’s suggestion - I’ve now discovered that the cat likes to stick her head out of the flap - thus triggering the switch, but not actually go out :smile:

1 Like

You know as soon as I read this thread, I thought, this may be one of the most difficult home automation tasks in the world.

1 Like

Cat’s smarter than me - I hadn’t thought of that way to mess it up.

For extra complication and more spend, a PIR could provide info about whether a cat ended up in or out after flap activation. The cat will of course defeat that by sitting motionless for an hour, looking through the transparent flap.

Yep, she does that. And my PIRs are the pet friendly sort which ignore small animals :slight_smile:
When I was designing how the switches would work with the flap, I looked at using an accelerometer on the flap to measure it’s position, but the difficulty there would be the wires to the chip would restrict the flap (and it was just too much hassle for what should have been a simple task).

But thinking about it, it would be possible to mount a small PIR on the inside of the flap - but to be honest, that would involve taking it apart - again, to mount it. Though I might put that on the todo list - once I’ve solved the problem why the cat flap only works with the 12v power supply that came with it, and not the 12V power supply that I was using to drive the ESP8266 (through a regulator)

I was thinking more a cheap dedicated PIR mounted low down nearby and hooked to your ESP

It’d see ankles as well of course, but not associated with catflap action.