Repository: dgtlmoon/changedetection.io
Issue: Site monitoring sends always the same (and old) screenshot

**Describe the bug**
A site monitoring every 2 minutes has "Attach screenshot to notification (where possible)" enabled. Also it has values for "Trigger/wait for text" if could be useful to know. Every time a notification is triggered, I receive always an old screeenshot taken the first time I enabled the "Attach screenshot to notification (where possible)" option for this site.

**Version**
0.39.22

**Expected behavior**
I expect to get the lastest screenshot of the page that triggers my notification

**Desktop (please complete the following information):**
 - OS: Debian Bullseye on Orange Pi Zero 2
 - Playwhrite
 - Version: latest

Changed files:
changedetectionio/store.py, changedetectionio/model/Watch.py, changedetectionio/tests/test_notification.py

diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py
index 62eaf33..0b94aa6 100644
--- a/changedetectionio/model/Watch.py
+++ b/changedetectionio/model/Watch.py
@@ -252,20 +252,11 @@ class model(dict):
         return None
 
     def get_screenshot_as_jpeg(self):
-        """Best used in notifications due to its smaller size"""
-        png_fname = os.path.join(self.watch_data_dir, "last-screenshot.png")
-        jpg_fname = os.path.join(self.watch_data_dir, "last-screenshot.jpg")
-
-        if os.path.isfile(jpg_fname):
-            return jpg_fname
-
-        if os.path.isfile(png_fname) and not os.path.isfile(jpg_fname):
-            # Doesnt exist, so create the JPEG from the PNG
-            from PIL import Image
-            im1 = Image.open(png_fname)
-            im1.convert('RGB').save(jpg_fname, quality=int(os.getenv("NOTIFICATION_SCREENSHOT_JPG_QUALITY", 75)))
-            return jpg_fname
 
+        # Created by save_screenshot()
+        fname = os.path.join(self.watch_data_dir, "last-screenshot.jpg")
+        if os.path.isfile(fname):
+            return fname
 
         # False is not an option for AppRise, must be type None
         return None
diff --git a/changedetectionio/store.py b/changedetectionio/store.py
index 04d4fb3..adfba66 100644
--- a/changedetectionio/store.py
+++ b/changedetectionio/store.py
@@ -368,6 +368,12 @@ class ChangeDetectionStore:
             f.write(screenshot)
             f.close()
 
+        # Make a JPEG that's used in notifications (due to being a smaller size) available
+        from PIL import Image
+        im1 = Image.open(target_path)
+        im1.convert('RGB').save(target_path.replace('.png','.jpg'), quality=int(os.getenv("NOTIFICATION_SCREENSHOT_JPG_QUALITY", 75)))
+
+
     def save_error_text(self, watch_uuid, contents):
         if not self.data['watching'].get(watch_uuid):
             return
diff --git a/changedetectionio/tests/test_notification.py b/changedetectionio/tests/test_notification.py
index b71e547..e499996 100644
--- a/changedetectionio/tests/test_notification.py
+++ b/changedetectionio/tests/test_notification.py
@@ -71,13 +71,18 @@ def test_check_notification(client, live_server):
     time.sleep(3)
 
     # We write the PNG to disk, but a JPEG should appear in the notification
-    testimage_png = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='
     # Write the last screenshot png
+    testimage_png = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='
+    # This one is created when we save the screenshot from the webdriver/playwright session (converted from PNG)
+    testimage_jpg = '/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/wAALCAABAAEBAREA/8QAFAABAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAD8AKp//2Q=='
+
 
     uuid = extract_UUID_from_client(client)
     datastore = 'test-datastore'
     with open(os.path.join(datastore, str(uuid), 'last-screenshot.png'), 'wb') as f:
         f.write(base64.b64decode(testimage_png))
+    with open(os.path.join(datastore, str(uuid), 'last-screenshot.jpg'), 'wb') as f:
+        f.write(base64.b64decode(testimage_jpg))
 
     # Goto the edit page, add our ignore text
     # Add our URL to the import page
@@ -128,8 +133,6 @@ def test_check_notification(client, live_server):
     time.sleep(3)
     set_modified_response()
 
-    notification_submission = None
-
     # Trigger a check
     client.get(url_for("form_watch_checknow"), follow_redirects=True)
     time.sleep(3)