A baby sleep monitor that runs entirely on your own hardware. Point a camera you already own at the crib, and get sleep stretches, wake-ups, movement scoring and cry detection — without a subscription, an account, or a single frame of your child leaving the house.
┌──────────────┐ RTSP ┌────────────────────────────────┐
│ IP camera │──────────────▶│ your machine │
│ (any RTSP) │ │ │
└──────────────┘ │ tracker segmentation + flow │
│ audio YAMNet cry/snore │
│ web dashboard :8848 │
└──────────┬─────────────────────┘
│ optional
▼
MQTT / Home Assistant
There is no cloud component. There is nowhere to sign up. If your internet goes down, everything keeps working.
nightlight is not a medical device, and it is not a safety device.
It cannot detect that a baby has stopped breathing. It must never be used as an apnea monitor, a SIDS-prevention device, or any kind of alarm you would sleep more soundly because of.
It is a camera pointed at a crib, and it fails the way cameras fail:
- a blanket pulled over the chest hides the movement it measures
- a bumped camera or a rearranged crib invalidates its reference frames
- a dead network, a full disk, or a crashed process stops it silently
- the breathing number is a brightness oscillation in a video feed, not a measurement of airflow — it is an estimate, frequently wrong, and deliberately blank rather than approximate when the tracker is unsure
Use it the way you would use a sleep diary that fills itself in: to notice that last Tuesday was rough, or that bedtime has drifted later. Do not use it to decide whether to go and check on your child. Go and check on your child.
Follow your paediatrician's safe-sleep guidance. Nothing here replaces it.
Presence and movement. The tracker holds a reference photo of the empty crib and diffs each frame against it. The largest changed blob is the child; dense optical flow inside that silhouette becomes a 0–100 movement score. Scoring displacement rather than raw pixel change is what makes it survive infrared flicker and lighting drift.
Sleep state. Movement is smoothed into deep_sleep / light_sleep /
restless / awake, using sustained evidence rather than one threshold
crossing — 90 seconds of activity to call awake, 120 seconds of calm to call
asleep. These are movement descriptions. They are not clinical sleep stages
and are not comparable to what a sleep lab measures.
Orientation-aware regions. Movement is attributed to head / torso / legs along the body's own major axis, so it works whichever way the child is lying, including diagonally.
Breathing estimate. A dedicated sampler watches a slow brightness oscillation in the torso area and FFTs a 45-second window. Needs stillness; publishes nothing when it is not confident. See the safety notice above.
Cry, fuss and snore detection (optional). YAMNet classifies the camera's own audio locally. Detections merge into episodes and roll up into a nightly summary. One microphone covers a shared room, so it tracks an adult's snoring alongside the child's crying.
When it doesn't know, it says so. This is the part most worth trusting.
Four independent validity layers — segmentation quality, presence, geometry,
respiration — each fail closed. If the reference frame no longer matches the
room, the tracker reports unknown and stops accruing sleep rather than
quietly scoring the mattress as a sleeping baby. That specific failure is why
the layers exist: an earlier version logged "deep sleep" on a verifiably empty
crib for several minutes because a changed bedsheet made the whole mattress
light up as a silhouette.
| Camera | Anything that speaks RTSP. Infrared/night vision is essential — it is dark at night. Audio support only matters for the cry detection. |
| Machine | Any always-on Linux box. A Raspberry Pi 4 handles the tracker; add the audio monitor and you want ~2 modern cores. |
| Python | 3.9+ |
| Optional | MQTT broker (for Home Assistant), Ollama (automatic night reference), ffmpeg (audio) |
No GPU. The tracker runs one 720p frame per second and spends most of its time asleep.
Tested with TP-Link Tapo C-series cameras. Any RTSP camera should work — see docs/hardware.md for stream URLs and what to look for when buying one.
git clone https://github.com/adnanakil/nightlight.git
cd nightlight
sudo ./scripts/install.shThe installer creates a venv, a service user, and systemd units, then tells you the three things left to do. To do it by hand instead:
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-audio.txt # optional: cry/snore detection
pip install -r requirements-pose.txt # optional: pose landmarks
cp .env.example .env # then edit RTSP_URL1. Point it at your camera. Set RTSP_URL in .env. Use the sub /
low-resolution stream — 720p is plenty and costs a fraction of the CPU.
2. Get the crib region right. This matters more than every threshold in the config combined.
python -m nightlight.cli roiThat writes a preview with everything outside CRIB_ROI dimmed. The bright
box should contain the mattress and nothing else — no rails, no floor, no
adjacent bed, no burnt-in timestamp. Adjust CRIB_ROI in .env, re-run,
repeat. A box that includes the rails will eventually score a shifting blanket
or a passing adult as your child.
3. Capture the empty-crib references. With the crib empty:
python -m nightlight.cli captureYou need two — one in daylight, one in infrared — because the crib looks completely different under IR. The tracker captures whichever mode it is currently in, so run it once during the day and once at night.
The night one is awkward to catch by hand. If you run Ollama, let
nightlight.reference_capture wait for a dark room with an empty crib and
grab it for you.
Until both exist, the tracker reports unknown rather than guessing. That is
intended.
4. Run it.
python -m nightlight.tracker # the tracker
python -m nightlight.web # dashboard on :8848
python -m nightlight.audio # optional cry/snore detectionOr, with the installer's units: sudo systemctl enable --now nightlight-tracker nightlight-web
Check everything at once with python -m nightlight.cli check.
Almost every bad reading traces back to a stale reference. The tracker finds your child by subtracting a photo of the empty crib — so when the crib stops matching that photo, the subtraction returns nonsense.
Re-capture whenever the crib changes. A new sheet. A blanket added or removed. A toy left in. The camera nudged. Any of these can turn the whole mattress into one convincing baby-shaped blob.
The tracker tries hard to catch this itself: a contour covering more than half
the crib region is rejected outright, presence goes unknown, and the summary
sets reference_recapture_needed. If the dashboard says the reference looks
stale, believe it — compare both references against a live frame before
assuming the tracker is broken. It is usually right.
http://<host>:8848 — live state, movement, sleep stretches, night audio
summary, per-night history, and the annotated frame showing exactly what the
tracker sees. That last one is the debugging tool: if the green silhouette is
not on your child, nothing downstream means anything.
There is no authentication. It binds 0.0.0.0 so you can open it from a
phone on your own network. If that is not acceptable, set WEB_HOST=127.0.0.1
and put it behind a reverse proxy, or reach it over a VPN such as Tailscale.
Do not port-forward it.
Optional. Leave MQTT_BROKER empty and nightlight writes JSON files only.
| Topic | Payload |
|---|---|
home/baby/sleep/state |
deep_sleep / light_sleep / restless / awake / unknown (retained) |
home/baby/sleep/score |
movement score 0–100 (retained) |
home/baby/sleep/present |
true / false (retained) |
home/baby/sleep/breathing_rate |
breaths/min — empty when not valid (retained) |
home/baby/sleep/event |
JSON on state changes |
home/audio/baby/crying |
ON / OFF (retained) |
home/audio/baby/fussing |
ON / OFF (retained) |
home/audio/bedroom/snoring |
ON / OFF (retained) |
The breathing topic is actively cleared to an empty payload the moment the reading expires. Anything consuming it must treat empty as "no reading" — a retained stale number is exactly the failure mode that would matter most here.
Prefixes are configurable (MQTT_TOPIC_PREFIX, AUDIO_MQTT_TOPIC_PREFIX).
Everything is an environment variable; .env is read at startup. See
.env.example for the annotated set, and
docs/tuning.md for what to change after your first real
night of data.
The short version: get CRIB_ROI right, capture good references, and leave
the thresholds alone until you have a night of history to look at.
- No cloud, no account, no telemetry. Nothing calls home, because there is no home to call.
- Video is never written to disk except one annotated debug frame, which is overwritten in place, and the empty-crib references — which by definition contain an empty crib.
- Audio is never recorded. Three-second chunks go to a local classifier and are discarded. Only labels and timestamps are stored.
- The one internet fetch in the whole project is YAMNet downloading from TensorFlow Hub the first time the audio monitor starts, and it caches. See models/README.md for how to run fully offline.
.gitignoreexcludes.envand every image format, so credentials and frames of your home cannot be committed by accident.
- docs/hardware.md — cameras, stream URLs, placement
- docs/tuning.md — thresholds, and how to read your data
- docs/architecture.md — how the pipeline works and why
- docs/troubleshooting.md — when it goes wrong
python -m unittest discover -s testsSynthetic tests, no camera needed. They cover the orientation-aware body-axis split and the fail-closed validity layers — the parts where a silent wrong answer would be worse than no answer.
Issues and pull requests welcome. This grew out of one family's nursery, so the assumptions it does not know it is making are mostly about cameras and cribs other than ours — reports about both are especially useful.
Please do not add a feature that requires a cloud service.
MIT — see LICENSE, including the notice that this is not a medical device.