Mouse Trap Mk I
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):

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:
- I could see what was going on.
- 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:

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: continuous_alarm
9 on_turn_off:
10 then:
11 - script.stop: continuous_alarm
12 - script.stop: alarm_beep
13 - platform: gpio
14 pin: GPIO14
15 id: GPIO14
16
17script:
18 - id: alarm_beep
19 parameters:
20 on_time: int
21 off_time: int
22 num_repeats: int
23 then:
24 - repeat:
25 count: !lambda return num_repeats;
26 then:
27 - switch.turn_on: GPIO14
28 - delay: !lambda return on_time;
29 - switch.turn_off: GPIO14
30 - delay: !lambda return off_time;
31
32 - id: continuous_alarm
33 then:
34 - script.execute:
35 id: alarm_beep
36 on_time: 100
37 off_time: 1900
38 num_repeats: 30
39 - script.wait: alarm_beep
40 - script.execute:
41 id: alarm_beep
42 on_time: 300
43 off_time: 700
44 num_repeats: 60
45 - script.wait: alarm_beep
46 - script.execute:
47 id: alarm_beep
48 on_time: 200
49 off_time: 200
50 num_repeats: 10000
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:

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):

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.