# Buggy code
def initialize_values(self):
    """Set PID parameters"""
    pid = db_retrieve_table_daemon(PID, device_id=self.pid_id)
    self.is_activated = pid.is_activated
    self.is_held = pid.is_held
    self.is_paused = pid.is_paused
    self.pid_type = pid.pid_type
    self.measurement = pid.measurement
    self.method_id = pid.method_id
    self.direction = pid.direction
    self.raise_relay_id = pid.raise_relay_id
    self.raise_min_duration = pid.raise_min_duration
    self.raise_max_duration = pid.raise_max_duration
    self.raise_min_off_duration = pid.raise_min_off_duration
    self.lower_relay_id = pid.lower_relay_id
    self.lower_min_duration = pid.lower_min_duration
    self.lower_max_duration = pid.lower_max_duration
    self.lower_min_off_duration = pid.lower_min_off_duration
    self.Kp = pid.p
    self.Ki = pid.i
    self.Kd = pid.d
    self.integrator_min = pid.integrator_min
    self.integrator_max = pid.integrator_max
    self.period = pid.period
    self.max_measure_age = pid.max_measure_age
    self.default_set_point = pid.setpoint
    self.set_point = pid.setpoint

    sensor = db_retrieve_table_daemon(Sensor, device_id=pid.sensor_id)
    self.sensor_unique_id = sensor.unique_id
    self.sensor_duration = sensor.period

    return "success"


# 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 initialize_values(self):
    """Set PID parameters"""
    pid = db_retrieve_table_daemon(PID, device_id=self.pid_id)
    self.is_activated = pid.is_activated
    self.is_held = pid.is_held
    self.is_paused = pid.is_paused
    self.pid_type = pid.pid_type
    self.method_id = pid.method_id
    self.direction = pid.direction
    self.raise_relay_id = pid.raise_relay_id
    self.raise_min_duration = pid.raise_min_duration
    self.raise_max_duration = pid.raise_max_duration
    self.raise_min_off_duration = pid.raise_min_off_duration
    self.lower_relay_id = pid.lower_relay_id
    self.lower_min_duration = pid.lower_min_duration
    self.lower_max_duration = pid.lower_max_duration
    self.lower_min_off_duration = pid.lower_min_off_duration
    self.Kp = pid.p
    self.Ki = pid.i
    self.Kd = pid.d
    self.integrator_min = pid.integrator_min
    self.integrator_max = pid.integrator_max
    self.period = pid.period
    self.max_measure_age = pid.max_measure_age
    self.default_set_point = pid.setpoint
    self.set_point = pid.setpoint

    sensor_unique_id = pid.measurement.split(",")[0]
    self.measurement = pid.measurement.split(",")[1]

    sensor = db_retrieve_table_daemon(Sensor, unique_id=sensor_unique_id)
    self.sensor_unique_id = sensor.unique_id
    self.sensor_duration = sensor.period

    return "success"