Skip to content

A live bird collage card for your Home Assistant dashboard.

Improve query accuracy for today's birds

Joe Weiss5c4763b44daf
diff --git a/scripts/notifications.py b/scripts/notifications.py
index 312b8a8c..71f55001 100644
--- a/scripts/notifications.py
+++ b/scripts/notifications.py
@@ -8,7 +8,6 @@ from datetime import datetime, timedelta
userDir = os.path.expanduser('~')
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
-THIS_RUN_PATH = userDir + '/BirdNET-Pi/scripts/thisrun.txt'
DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db'
def notify(body, title):
@@ -22,7 +21,8 @@ def notify(body, title):
)
def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=DB_PATH):
- # print(settings_dict)
+ print(sendAppriseNotifications)
+ print(settings_dict)
if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0:
title = settings_dict.get('APPRISE_NOTIFICATION_TITLE')
@@ -48,12 +48,17 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D
try:
con = sqlite3.connect(db_path)
cur = con.cursor()
- cur.execute("SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE date > (SELECT DATETIME('now', '-1 day')) GROUP BY Com_Name")
+ today = datetime.now().strftime("%Y-%m-%d")
+ cur.execute(f"SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE Date = date('{today}') GROUP BY Com_Name")
known_species = cur.fetchall()
- numberDetections = [d[1] for d in known_species if d[0] == comName.replace("'","")][0]
- if numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT:
- notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times today)",
- notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times today)",
+ detections = [d[1] for d in known_species if d[0] == comName.replace("'","")]
+ numberDetections = 0
+ if len(detections):
+ numberDetections = detections[0]
+ if numberDetections > 0 and numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT:
+ print("send the notification")
+ notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (first time today)"
+ notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (first time today)"
notify(notify_body, notify_title)
con.close()
except sqlite3.Error as e:
@@ -65,10 +70,14 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D
try:
con = sqlite3.connect(db_path)
cur = con.cursor()
- cur.execute("SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE date > (SELECT DATETIME('now', '-7 day')) GROUP BY Com_Name")
+ today = datetime.now().strftime("%Y-%m-%d")
+ cur.execute(f"SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE Date >= date('{today}', '-7 day') GROUP BY Com_Name")
known_species = cur.fetchall()
- numberDetections = [d[1] for d in known_species if d[0] == comName.replace("'","")][0]
- if numberDetections <= 5:
+ detections = [d[1] for d in known_species if d[0] == comName.replace("'","")]
+ numberDetections = 0
+ if len(detections):
+ numberDetections = detections[0]
+ if numberDetections > 0 and numberDetections <= 5:
notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times in last 7d)"
notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times in last 7d)"
notify(notify_body, notify_title)
diff --git a/scripts/server.py b/scripts/server.py
index 7b586352..c7e5ffe3 100755
--- a/scripts/server.py
+++ b/scripts/server.py
@@ -50,8 +50,6 @@ with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
audiofmt = "." + str(str(str([i for i in this_run if i.startswith('AUDIOFMT')]).split('=')[1]).split('\\')[0])
priv_thresh = float("." + str(str(str([i for i in this_run if i.startswith('PRIVACY_THRESHOLD')]).split('=')[1]).split('\\')[0])) / 10
-settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt')
-
def loadModel():
global INPUT_LAYER_INDEX
@@ -402,10 +400,10 @@ def handle_client(conn, addr):
time.sleep(2)
# Apprise of detection if not already alerted this run.
- if not str(entry[0]) in species_apprised_this_run:
+ if not entry[0] in species_apprised_this_run:
+ settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt')
sendAppriseNotifications(str(entry[0]), str(entry[1]), File_Name, settings_dict)
-
- species_apprised_this_run.append(str(entry[0]))
+ species_apprised_this_run.append(entry[0])
print(str(current_date) +
';' +
diff --git a/tests/test_apprise_notifications.py b/tests/test_apprise_notifications.py
index 54181137..b2c53007 100644
--- a/tests/test_apprise_notifications.py
+++ b/tests/test_apprise_notifications.py
@@ -22,7 +22,8 @@ def create_test_db(db_file):
VALUES(?,?) '''
cur = conn.cursor()
- cur.execute(sql, ["Great Crested Flycatcher", datetime.now()])
+ today = datetime.now().strftime("%Y-%m-%d") # SQLite stores date as YYYY-MM-DD
+ cur.execute(sql, ["Great Crested Flycatcher", today])
conn.commit()
except Error as e:
@@ -58,7 +59,7 @@ def test_notifications(mocker):
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher", "91", "filename", settings_dict, "test.db")
assert(notify_call.call_count == 1)
assert(
- notify_call.call_args_list[0][0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times today)"
+ notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)"
)
# Add new species notification.
@@ -67,7 +68,7 @@ def test_notifications(mocker):
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher", "91", "filename", settings_dict, "test.db")
assert(notify_call.call_count == 2)
assert(
- notify_call.call_args_list[0][0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times today)"
+ notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)"
)
assert(
notify_call.call_args_list[1][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times in last 7d)"