Skip to main content

Shazam-to-MQTT bridge with Home Assistant auto-discovery

Project description

shazam2mqtt

Because vinyl, tapes, and CDs don't have an API.

shazam2mqtt is a Dockerised bridge that listens to your room, recognises whatever music is playing via Shazam, and publishes the result to MQTT — including Home Assistant auto-discovery.

Why shazam2mqtt?

Streaming services expose what you're playing natively. Physical media doesn't.

If you consume music on:

  • Vinyl
  • Cassette tapes
  • CDs
  • Radio
  • ...or anything else that doesn't have a Spotify API endpoint

...then Home Assistant has no idea what's spinning. shazam2mqtt fixes that by placing a microphone near your speakers, fingerprinting the audio with Shazam, and pushing the track metadata straight into HA.

Your dashboard can now show:

  • What's currently playing
  • The artist and track name
  • Album artwork (Picture Entity card)
  • A link to the song on Apple Music, Spotify, Deezer
  • Lyrics, release metadata, and related videos
  • Whether the room is silent or loud

What it does

  1. Noise-gated listening — continuously monitors ambient audio using an RMS threshold (NOISE_GATE_DB).
  2. Smart capture — once audio is sustained for ~3 s, it records a 10-second clip.
  3. Shazam identification — fingerprints the clip via xazam + shazamio_core.
  4. MQTT publishing — pushes the track title, artist, and metadata to MQTT topics.
  5. Home Assistant discovery — automatically registers 9 entities under one device (Now Playing, Artist, Track, Confidence, Matched, Status, Noise Level, Apple Music URL, Artwork URL) via the MQTT integration.
  6. Rich JSON attributes — the Now Playing sensor carries lyrics, genres, Spotify/Deezer links, album metadata, and related YouTube videos as attributes.

Quick Start (Docker Compose)

  1. Clone / copy this directory.
  2. Create a .env file (or export variables):
DEVICE_NAME=living_room
MQTT_HOST=192.168.1.200
MQTT_USER=mqtt
MQTT_PASS=secret
NOISE_GATE_DB=-40
LISTEN_DURATION=10
COOLDOWN_SECONDS=60
SAME_SONG_COOLDOWN_SECONDS=300
  1. Run:
docker compose up --build

Home Assistant will auto-discover the device under Settings → Devices & Services → MQTT.

Environment Variables

Variable Default Description
DEVICE_NAME shazam Prefix for the HA entity ID (living_room_shazam_now_playing)
MQTT_HOST localhost MQTT broker host
MQTT_PORT 1883 MQTT broker port
MQTT_USER (none) MQTT username
MQTT_PASS (none) MQTT password
NOISE_GATE_DB -40 dBFS threshold. Audio quieter than this is considered silence.
LISTEN_DURATION 10 Seconds of audio to capture for Shazam recognition
COOLDOWN_SECONDS 60 Minimum seconds between recognition attempts
SAME_SONG_COOLDOWN_SECONDS 300 Extra cooldown if the same song repeats
HA_DISCOVERY_PREFIX homeassistant Home Assistant MQTT discovery prefix
HA_ENABLED true Set to false to disable HA discovery messages

Architecture

Mic ──► Noise Gate (RMS) ──► 10 s Capture ──► Shazam API ──► MQTT ──► Home Assistant
                    │
                    └── "listen_now" command from HA
  • Idle loop reads 1-second audio chunks and checks RMS energy.
  • Trigger requires 3 consecutive "loud" chunks (~3 s hysteresis) to avoid reacting to pops.
  • State machine prevents overlapping captures and rate-limits the Shazam API.
  • Command topic shazam2mqtt/<device_name>/command accepts listen_now for manual triggers.

MQTT Topics

Topic Type Payload example
shazam2mqtt/<name>/now_playing state Nothing Else Matters — Metallica
shazam2mqtt/<name>/status status playing / unknown / silence
shazam2mqtt/<name>/matched binary ON / OFF
shazam2mqtt/<name>/track sensor Nothing Else Matters
shazam2mqtt/<name>/artist sensor Metallica
shazam2mqtt/<name>/confidence sensor 4
shazam2mqtt/<name>/apple_music_url sensor https://music.apple.com/...
shazam2mqtt/<name>/artwork_url sensor https://is1-ssl.mzstatic.com/...
shazam2mqtt/<name>/noise_level sensor -40.0
shazam2mqtt/<name>/command command in listen_now

Audio Backends

ALSA (default)

The docker-compose.yml passes /dev/snd into the container. This works on most headless Linux hosts.

PulseAudio / PipeWire

If your host uses PulseAudio, comment out the devices: block and uncomment the volumes: and PULSE_SERVER lines in docker-compose.yml.

Non-Docker Usage

pip install -e .
export DEVICE_NAME=my_room
export MQTT_HOST=192.168.1.200
python -m shazam2mqtt

Troubleshooting

  • "No audio device found" — make sure the container has access to /dev/snd or the PulseAudio socket.
  • "No matches" constantly — lower NOISE_GATE_DB (e.g. -50) or move the mic closer to the speakers.
  • Entity not appearing in HA — check that the MQTT integration is enabled in Home Assistant and that HA_ENABLED=true.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shazam2mqtt-0.1.1a3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shazam2mqtt-0.1.1a3-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file shazam2mqtt-0.1.1a3.tar.gz.

File metadata

  • Download URL: shazam2mqtt-0.1.1a3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for shazam2mqtt-0.1.1a3.tar.gz
Algorithm Hash digest
SHA256 14f031bb82617f2173504edb84f02e45fe1edb2bc0d3bbc655481d817d78cf57
MD5 fbdaa6f206f2424bf2b1776dc6a14dd2
BLAKE2b-256 473b1d946d85e8ed555bc899eb3e412d3f6d25eb9f841dc1e1f22d38ecbd4fb9

See more details on using hashes here.

File details

Details for the file shazam2mqtt-0.1.1a3-py3-none-any.whl.

File metadata

  • Download URL: shazam2mqtt-0.1.1a3-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for shazam2mqtt-0.1.1a3-py3-none-any.whl
Algorithm Hash digest
SHA256 d60b38e7f061a0c02e42ad6c4b51dfaff7b01e479ae750f94163c48449303387
MD5 0d967a714d8f468d7640349c8b8dc3d5
BLAKE2b-256 59445e5aae8a910699821f60c95d0477ac0cb8bb22aaed3f9e1b16e9cd414072

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page