# Buggy code
def create_lcd_line(self, last_measurement_success, i):
    try:
        if last_measurement_success:
            # Determine if the LCD output will have a value unit
            measurement = ""
            if self.lcd_line[i]["measurement"] == "setpoint":
                pid = db_retrieve_table_daemon(PID, unique_id=self.lcd_line[i]["id"])
                measurement = pid.measurement
                self.lcd_line[i]["measurement_value"] = "{:.2f}".format(
                    self.lcd_line[i]["measurement_value"]
                )
            elif self.lcd_line[i]["measurement"] == "duration_sec":
                measurement = "duration_sec"
                self.lcd_line[i]["measurement_value"] = "{:.2f}".format(
                    self.lcd_line[i]["measurement_value"]
                )
            elif self.lcd_line[i]["measurement"] in MEASUREMENT_UNITS:
                measurement = self.lcd_line[i]["measurement"]

            # Produce the line that will be displayed on the LCD
            number_characters = self.lcd_x_characters
            if self.lcd_line[i]["measurement"] == "time":
                # Convert UTC timestamp to local timezone
                utc_dt = datetime.datetime.strptime(
                    self.lcd_line[i]["time"].split(".")[0], "%Y-%m-%dT%H:%M:%S"
                )
                utc_timestamp = calendar.timegm(utc_dt.timetuple())
                self.lcd_string_line[i] = str(
                    datetime.datetime.fromtimestamp(utc_timestamp)
                )
            elif measurement:
                value_length = len(str(self.lcd_line[i]["measurement_value"]))
                unit_length = len(MEASUREMENT_UNITS[measurement]["unit"])
                name_length = number_characters - value_length - unit_length - 2
                name_cropped = self.lcd_line[i]["name"].ljust(name_length)[:name_length]
                self.lcd_string_line[i] = "{name} {value} {unit}".format(
                    name=name_cropped,
                    value=self.lcd_line[i]["measurement_value"],
                    unit=MEASUREMENT_UNITS[measurement]["unit"],
                )
            else:
                value_length = len(str(self.lcd_line[i]["measurement_value"]))
                name_length = number_characters - value_length - 1
                name_cropped = self.lcd_line[i]["name"][:name_length]
                self.lcd_string_line[i] = "{name} {value}".format(
                    name=name_cropped, value=self.lcd_line[i]["measurement_value"]
                )
        else:
            self.lcd_string_line[i] = "ERROR: NO DATA"
    except Exception as except_msg:
        self.logger.exception("Error: {err}".format(err=except_msg))


# Traceback (IOError)
2017-06-07 11:03:18,941 - mycodo.sensors.raspi - ERROR - RaspberryPiCPUTemp.get_measurement() method raised IOError: [Errno 24] Too many open files: '/sys/class/thermal/thermal_zone0/temp'
2017-06-07 11:03:20,138 - mycodo.pid_1 - ERROR - Run Error: (sqlite3.OperationalError) unable to open database file

========= Remote Traceback (1) =========
Traceback (most recent call last):
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 329, in _dispatch_request
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 583, in _handle_call
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 201, in exposed_relay_off
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 553, in relay_off
File "/var/www/mycodo/mycodo/controller_relay.py", line 348, in relay_on_off
self.check_conditionals(relay_id, duration)
File "/var/www/mycodo/mycodo/controller_relay.py", line 397, in check_conditionals
for each_conditional in conditionals.all():

# Fixed code
def create_lcd_line(self, last_measurement_success, i):
    try:
        if last_measurement_success:
            # Determine if the LCD output will have a value unit
            measurement = ""
            if self.lcd_line[i]["measurement"] == "setpoint":
                pid = db_retrieve_table_daemon(PID, unique_id=self.lcd_line[i]["id"])
                measurement = pid.measurement.split(",")[1]
                self.lcd_line[i]["measurement_value"] = "{:.2f}".format(
                    self.lcd_line[i]["measurement_value"]
                )
            elif self.lcd_line[i]["measurement"] == "duration_sec":
                measurement = "duration_sec"
                self.lcd_line[i]["measurement_value"] = "{:.2f}".format(
                    self.lcd_line[i]["measurement_value"]
                )
            elif self.lcd_line[i]["measurement"] in self.list_sensors:
                measurement = self.lcd_line[i]["measurement"]

            # Produce the line that will be displayed on the LCD
            number_characters = self.lcd_x_characters
            if self.lcd_line[i]["measurement"] == "time":
                # Convert UTC timestamp to local timezone
                utc_dt = datetime.datetime.strptime(
                    self.lcd_line[i]["time"].split(".")[0], "%Y-%m-%dT%H:%M:%S"
                )
                utc_timestamp = calendar.timegm(utc_dt.timetuple())
                self.lcd_string_line[i] = str(
                    datetime.datetime.fromtimestamp(utc_timestamp)
                )
            elif measurement:
                value_length = len(str(self.lcd_line[i]["measurement_value"]))
                unit_length = len(self.list_sensors[measurement]["unit"])
                name_length = number_characters - value_length - unit_length - 2
                name_cropped = self.lcd_line[i]["name"].ljust(name_length)[:name_length]
                self.lcd_string_line[i] = "{name} {value} {unit}".format(
                    name=name_cropped,
                    value=self.lcd_line[i]["measurement_value"],
                    unit=self.list_sensors[measurement]["unit"],
                )
            else:
                value_length = len(str(self.lcd_line[i]["measurement_value"]))
                name_length = number_characters - value_length - 1
                name_cropped = self.lcd_line[i]["name"][:name_length]
                self.lcd_string_line[i] = "{name} {value}".format(
                    name=name_cropped, value=self.lcd_line[i]["measurement_value"]
                )
        else:
            self.lcd_string_line[i] = "ERROR: NO DATA"
    except Exception as except_msg:
        self.logger.exception("Error: {err}".format(err=except_msg))