These will indispensable to you as you leverage AppDaemon and expand this little script.
Once we have access to that, we need to setup the main topic for MQTT from BirdNET-Pi and finally, what event we are
listening for that will trigger the functions in the rest of the script. `self.birdnet_mqtt = "birdnet"` is the definition
for the MQTT topic. Let's breakdown the last line of the class.
Here's a breakdown of each of the items in that last line. You can find the official documentation [here](https://appdaemon.readthedocs.io/en/latest/MQTT_API_REFERENCE.html#appdaemon.plugins.mqtt.mqttapi.Mqtt.listen_event).
*`self.mqttapi.listen_event` - this is what we use in AppDaemon to listen for an MQTT event in order to trigger a function.
*`self.birdnet_message` - the name of the function you'd like to trigger
*`"MQTT_MESSAGE"` - The default event in AppDaemon's MQTT API plugin. This is used because MQTT doesn't keep a state in this
plugin.
*`topic=self.birdnet_mqtt` - The topic that will be received to trigger the function. Defined on the previous line.
In other words, what we are telling AppDaemon is the following: "When AppDaemon's MQTT API plugin receives a message with the
topic of 'birdnet', run the function `birdnet_message`."
### birdnet_message Function
#### Part 1: Variables Management
Now we get into our first function of the script. The first portion of this script is splitting up the payload that we
defined from the BirdNET-Pi UI into individual variables that we can better manage later on. If you test this script out by
adding `print()` statements at various points, you'll notice that the payload is received with the following json formatting:
```json
{
"payload": {
"data": "data"
}
}
```
As such, we need to look _inside_ the payload to begin grabbing the data. The `pre_split` variable is now just looking at the
data inside the payload and the rest of the variables take all the date into the payload, split it by the comma, and then
grab the string by their index. If you remember what [we did above]({{<ref "birdnet_homeassistant.md#birdnet-pi-notification-setup-mqtt" >}}) above, you'll see that we have the various BirdNET information at each of the indexes in the AppDaemon script - 0 through 5.
#### Part 2: Re-Publishing MQTT Payloads
This next section is shooting all the variables we just defined back via MQTT. The reason why we do it this way is because we
need HomeAssistant to grab each of these variables as individual sensors. BirdNET doesn't give us that capability - it's a
single message with all the information in one. [Here is the documentation from AppDaemon](## BirdNET-PI Notification Setup - MQTT
) on `mqtt_publish`. Later on, I'll show you how to ensure that HomeAssistant takes those topic payloads and adds them as
entities in your HA setup.
#### Part 3: Wikipedia Sensor
The next eight lines are a fairly straightforward [API call to Wikipedia](https://en.wikipedia.org/api/rest_v1/). We start
out by passing the `science_name` into the URL. The rest of the flags that we are passing into the URL comes from Wikipedia's
#### Part 4: Generate Picture for Detection (Optional)
This part is optional but I noticed that BirdNET-Pi was already grabbing a Flickr Picture for it's front end, so I took the
code from the BirdNET code base and adjusted it a bit for my needs. This will work very similarly to the Wikipedia API call,
the main difference here being that you need an API key for Flickr. You can find more [information here](https://www.flickr.com/services/api/misc.api_keys.html).
Given Flickr's fairly robust API, by passing in the detected bird's common name, we get amazing results from the community of
various pictures of the same species of bird. Ever since I've set this up, I've not seen a mislabeled picture in my
dashboard!
The most confusion portion of this section is the `image_url` as you'll notice a bunch of `data["value"]` strings at various
portions of the URL. The short answer to this is in the previous line with the `data` variable. A successful query has Flickr
returning a large payload of information. We're specifically using [this](https://www.flickr.com/services/api/flickr.photos.search.html)
Flickr API endpoint. While you can pass a lot of variables for your needs, if you scroll down, you can see that the example
response contains multiple photos in a single response. We're passing `per_page=5` to limit some of those response items.
Left out of that response, though, is a one-stop-shop for a URL to that photo. Thankfully, Flickr can help us put together a
URL from the data in the response.
_Note: Full Transparency that I only learned about this after reading through BirdNET-Pi's code base. Full credit goes to
[mcguirepr89](https://github.com/mcguirepr89). For additional reference, here is Flickr's [official page on construction