This Python script birdnet_to_mqtt.py
does monitoring on the BirdNET-Pi detections from the syslog file and publishes the bird species detection data to an MQTT broker, which can be used by Home Assistant or other systems.
BirdNET-Pi is an tool for analyzing bird sounds using a Raspberry Pi. This project enhances its utility by integrating detected bird species data into Home Assistant through MQTT, enabling users to receive real-time alerts and perform automated actions based on bird detections.
- A working installation of BirdNET-Pi that logs detections to the syslog.
- MQTT Broker (like Mosquitto) setup within your network.
- Python 3.x installed on your system.
- Error handling on mqtt connection
- Password and Username's
- Works with newer paho-mqtt versions
First, ensure you have the script on your local system by cloning the repository:
git clone https://gist.github.com/JuanMeeske/08b839246a62ff38778f701fc1da5554
cd your-folder-name
sudo pip3 install dateparser
sudo pip3 install paho-mqtt
# MQTT Server Settings
mqtt_server = "192.168.xyz.xyz" #mqtt server address
mqtt_port = 1883 # Default port for MQTT
mqtt_keepalive = 60 # Keepalive interval in seconds
client_id = 'python-mqtt'
# MQTT Authentication Settings
mqtt_username = "mqtt_birdnet" # Replace 'your_username' with your actual MQTT username
mqtt_password = "<yourpasswordhere>" # Replace 'your_password' with your actual MQTT password
# mqtt topic where all heard birds will be published
mqtt_topic_all_birds = 'birdpi/all'
python birdnet_to_mqtt.py
python birdnet_to_mqtt.py
The script should output if connection was successful or not:
# python3 birdnet_to_mqtt.py
Connected to MQTT Broker!
Follow the steps in the gistfile1.txt
Actually I have adapted the code just for fun :-) here as code that could work :
In constants :
then add this additional code (to get confidence, and only send bird_data if confidence is acceptable) just before the bird_data :