So we thought we might have a mouse under the kitchen units…

This isn’t the first time we’ve had mice. And if you don’t think there are any in your house, well maybe it’s because you haven’t noticed them. They are very elusive, generally coming out in the middle of the night, and they are very shy!

I already had all the bits I needed to catch and re-home the mouse (you have to take it quite a long way - a mile or two - otherwise it will find it’s way back!). The central component is a humane mouse trap, which has a pressure sensitive pad and door that stops the mouse escaping. They typically cost about £10 (e.g. on Amazon). My advice - do a bit of research - don’t just get the cheapest one. They are very sensitive, but mice are very light, so the one I have had to be adjusted by putting a couple of coins on the pressure pad so that even the lightest mouse would trigger it.

The layout of the mouse trap components looked like this (not to scale):

Mouse Trap Layout

The light is always on. It doesn’t put the mouse off; if it was that easy, then you could just put LEDs where you wanted to keep the mice out! The LDR on the mouse detector is placed behind the door. When the door closes, light falls on the sensor and the value reported to Home Assistant goes up. I checked this by trial and error, and found a suitable threshold was 800. More on this later. The Raspberry Pi camera was there so that:

  1. I could see what was going on.
  2. My plan was to have some automatic detection (maybe using OpenCV). I haven’t done that yet.

There are three main components of the Home Assistant solution. These are shown below:

Mouse Trap Components

I used ESPHome. The code for the detector looked like this (after captive_portal):

 1sensor:
 2  - platform: adc
 3    pin: GPIO32
 4    id: MouseTrapDoor
 5    name: "Mouse Trap Door"
 6    raw: True
 7    unit_of_measurement: "lx"
 8    update_interval: 30s
 9    filters:
10      - round: 0
11      - sliding_window_moving_average:
12          window_size: 4
13          send_every: 1

The code for the alarm looks like this:

 1switch:
 2  - platform: gpio
 3    pin: GPIO16
 4    id: MouseAlarm
 5    name: "Mouse Alarm"
 6    on_turn_on:
 7      then:
 8        - script.execute: alarmbeep1
 9        - script.wait: alarmbeep1
10        - if:
11            condition:
12              switch.is_on: MouseAlarm
13            then:
14              - script.execute: alarmbeep2
15              - script.wait: alarmbeep2
16        - script.execute: alarmbeep99
17    on_turn_off: 
18      then:
19        - script.stop: alarmbeep1
20        - script.stop: alarmbeep2
21        - script.stop: alarmbeep99
22  - platform: gpio
23    pin: GPIO14
24    id: GPIO14
25
26script:
27  - id: alarmbeep1
28    then:
29      - repeat:
30          count: 60
31          then:
32            - switch.turn_on: GPIO14
33            - delay: 0.1s
34            - switch.turn_off: GPIO14
35            - delay: 1.9s
36  - id: alarmbeep2
37    then:
38      - repeat:
39          count: 60
40          then:
41            - switch.turn_on: GPIO14
42            - delay: 0.3s
43            - switch.turn_off: GPIO14
44            - delay: 0.7s
45  - id: alarmbeep99
46    then:
47      - while:
48          condition:
49            switch.is_on: MouseAlarm
50          then:
51            - repeat:
52                count: 11
53                then:
54                  - switch.turn_on: GPIO14
55                  - delay: 0.2s
56                  - switch.turn_off: GPIO14
57                  - delay: 0.2s
58            - repeat:
59                count: 7
60                then:
61                  - switch.turn_on: GPIO14
62                  - delay: 0.1s
63                  - switch.turn_off: GPIO14
64                  - delay: 0.1s
65            - repeat:
66                count: 5
67                then:
68                  - switch.turn_on: GPIO14
69                  - delay: 0.4s
70                  - switch.turn_off: GPIO14
71                  - delay: 0.4s

It’s a bit complicated (and needs to be simplified) but the idea is that it starts off quietly and then gets louder. That way, if it goes off in the middle of the night, it will wake me in a reasonable manner.

I wrote an automation to connect the detector up to the alarm:

 1alias: Mouse Caught
 2description: ""
 3triggers:
 4  - trigger: numeric_state
 5    entity_id:
 6      - sensor.mouse_trap_mouse_trap_door
 7    above: 800
 8conditions: []
 9actions:
10  - action: switch.turn_on
11    metadata: {}
12    target:
13      entity_id: switch.mouse_alarm_mouse_alarm
14    data: {}
15mode: single

This is it set up just before I put the kick boards back in:

Kitchen Photo

I then just had to wait… I could watch to see if anything was happening on the Raspberry Pi Camera (you can see the bits of cracker in the trap):

Raspberry Pi Camera Image

Once the alarm had gone off, I put the whole trap in a cardboard box and the mouse was taken on a car journey to it’s new home where it was released.