diff --git a/smartmontools/CHANGELOG b/smartmontools/CHANGELOG
index 7e01ffac4fb7e6dd57742c62bf8b7bb5719e7df0..9fa0bae84816a61725c811e8607dc6bc36e0bc07 100644
--- a/smartmontools/CHANGELOG
+++ b/smartmontools/CHANGELOG
@@ -43,6 +43,9 @@ NOTES FOR FUTURE RELEASES: see TODO file.
 
 <DEVELOPERS: ADDITIONS TO THE CHANGE LOG GO JUST BELOW HERE, PLEASE>
 
+  [CF] smartd: Fix directive '-l selftest' (ticket #36)
+       Regression was introduced with r2773.
+
   [CF] smartd: Don't disable attribute tracking if read thresholds fails.
        Windows: Don't return dummy thresholds if IOCTL_STORAGE_QUERY_PROPERTY
        or 3ware CLI is used to read SMART data.
diff --git a/smartmontools/NEWS b/smartmontools/NEWS
index ea4601fa31abfe5e700d346b592778f2d24db6cb..baff2562472998c6f6438e99c4cd2dc1a97335e3 100644
--- a/smartmontools/NEWS
+++ b/smartmontools/NEWS
@@ -13,6 +13,7 @@ Summary: smartmontools release 5.40
 - Drive database is in a separate source file 'drivedb.h'
   which can be downloaded from SVN.
 - Fix regression in smartctl option '-q, --quietmode'.
+- Fix regression in smartd directive '-l selftest'.
 
 Date 2009-12-09
 Summary: smartmontools release 5.39 (UNSTABLE/EXPERIMENTAL)
diff --git a/smartmontools/atacmds.cpp b/smartmontools/atacmds.cpp
index 745a4d55dcff787f7fd6ff95b38761cba6d2a313..1abc9f9e8b84dfa44b383a380ae60200afd04b30 100644
--- a/smartmontools/atacmds.cpp
+++ b/smartmontools/atacmds.cpp
@@ -2404,13 +2404,17 @@ int ataPrintSmartSelfTestlog(const ata_smart_selftestlog * data, bool allentries
       uint64_t lba48 = (log->lbafirstfailure < 0xffffffff ? log->lbafirstfailure : 0xffffffffffffULL);
 
       // Print entry
-      bool errorfound = ataPrintSmartSelfTestEntry(testno,
-        log->selftestnumber, log->selfteststatus, log->timestamp,
-        lba48, !allentries, noheaderprinted);
+      if (ataPrintSmartSelfTestEntry(testno,
+            log->selftestnumber, log->selfteststatus,
+            log->timestamp, lba48, !allentries, noheaderprinted)) {
 
-      // keep track of time of most recent error
-      if (errorfound && !hours)
-        hours=log->timestamp;
+        // Self-test showed an error
+        retval++;
+
+        // keep track of time of most recent error
+        if (!hours)
+          hours = log->timestamp;
+      }
     }
   }
   if (!allentries && retval)